kable(head(mtcars,3))
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
library(ggplot2) p=ggplot(mtcars,aes(y=Mileage,x=Weight)) p+geom_point(size=4)As apparent from the plot, Mileage goes down with increase in Weight. Now lets put in another dimension in this and see how having automatic transmission affects mileage.
p=ggplot(mtcars,aes(y=Mileage,x=Weight,color=Transmission)) p+geom_point(size=4)We can see that most of the cars with automatic transmission tend to have higher mileage. One thing to note here is that most of the high weight cars tend to have manual transmission which might be the real underlying reason for cars with automatic transmission to have higher mileage. Ok, now lets add one more dimension to find out how number of gears change across these different vehicles.
p=ggplot(mtcars,aes(y=Mileage,x=Weight,color=Transmission,size=Gears)) p+geom_point()+scale_size_discrete(range = c(4,6))You can see number of gears dont really affect mileage as they tend to take all possible values across entire range of mileage, same goes for weight. But a curious thing to observe here is that cars with automatic transmission managed to have higher number of gears in comparison to manual transmission cars. In fact there seems to be a limit on the number of gears which can be in the manual transmission cars. Lets add one more dimension depciting number of cylinders in engines.
p=ggplot(mtcars,aes(y=Mileage,x=Weight,color=Transmission,size=Gears,shape=Cylinders)) p+geom_point()+scale_size_discrete(range = c(4,6))You can see that number of cylinders certainly seem to have an effect on mileage. Low mileage and high weight cars tend to have 8 cylinders in the engine where as high mileage and low weight cars tend to have 4 cylinders. If you have noticed , by now we have 5 dimensions in a 2-D plot. Lession here is that visualising multiple factors [ dimension ] is not really about making n-D [impossible!] plot. Its not really feasible to add more axes to your plot. What we can do however is to give more features to our “points”, which is exactly what we have done here. The additional 3 dimensions that we introduced, are by adding features like shape, size and color to our points. Thats what I wanted to convey, let your imagination [ and Mr hadley wickham :author of ggplot2] take you out of those dimensionality constraints! Happy Plotting in R!