Getting started with ggplot2: scatter plot

Source: Internet
Author: User
Tags ggplot
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.