Runhang Shu

Visualize Your Data(3)

Runhang Shu / 2019-03-24


z<-cars
plot(z$speed,z$dist,main="Firgue 1")
lines(lowess(x=z$speed,y=z$dist),col="red",lwd=2)

#make a smooth line of a scatter plot
#lwd regulate width of line

Now, I want dots size correspond to their values

z<-cars
plot(z$speed,z$dist,main="Firgue 2",cex=z$speed/3) 
#Using cex to regulate dot size 
lines(lowess(x=z$speed,y=z$dist),col="red",lwd=2)

Looks better!
Now, I want dot of speed over 18 become purple

z<-cars
z$speedcolor="black"
vec<-z$speed>18
z[vec,'speedcolor']="purple"
plot(z$speed,z$dist,main="Firgue 2",cex=z$speed/3
     ,col=z$speedcolor,pch=1,xlab="Speed(mph)"
     ,ylab = "Brake Distance(meter)")
lines(lowess(x=z$speed,y=z$dist),col=6,lwd=2)
legend(5,120,c('Overspeed','Not overspeed'),col=c('purple','black'),pch=1)

Usind legend function to explain the dots