1. color and shape control
Data features can be expressed not only by coordinates, but also by different colors or shapes. Taking the MPG dataset as an example, the variables used include cty (driving distance in the city), hwy (highway driving distance), displ (displacement size), and year (production year)
1 library(ggplot2)2 p <- ggplot(mpg, aes(cty, hwy))3 p1 <- p + geom_point(aes(colour = factor(year),shape = factor(year), size = displ), alpha = 0.6, position = ‘jitter’)4 print(p1)
We will use a red circle to represent the models produced in 1999, and a blue triangle to represent the displacement in 2008, and set the transparency and jitter to avoid overlap between the sample points. We can see that there were many large-displacement models produced in 2008, resulting in high fuel consumption and a short distance per unit of fuel consumption.
2 Coordinate Control
Data points in the upper right corner are sparse. In this case, you can use logarithm transformation. To demonstrate how ggplot2 controls the coordinate of the image, we perform a logarithm transformation on both the X and Y axes, and then restrict the display of the coordinate on the X axis to only display the mean of the data on the X axis, and the coordinates of the double standard deviation.
1 cty.mean=with(mpg,mean(cty))2 cty.sd=with(mpg,sd(cty))3 p1 + scale_x_continuous(trans=’log’,breaks=c(cty.mean-cty.sd,cty.mean,cty.mean+cty.sd), labels=c(“high”, “mean”, “low”)) + scale_y_continuous(trans=’log’)
3 text description
Use the geom_text function to add text instructions to enhance the readability of the image.
p <- ggplot(mtcars, aes(x=wt, y=mpg,colour=factor(cyl),label=rownames(mtcars)))p + geom_text(hjust=0,vjust=-1,alpha=0.8)+ geom_point(size=3,aes(shape=factor(cyl)))
4. Matrix scatter chart
The matrix scatter chart function is also provided in the ggplot2 package.
1 plotmatrix(USArrests)+geom_smooth()
Reproduced in: http://r-ke.info/2012/02/18/ggplot2-intro-4.html