Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ggplot2 Wu Shaohuan.

Similar presentations


Presentation on theme: "Ggplot2 Wu Shaohuan."— Presentation transcript:

1 ggplot2 Wu Shaohuan

2 Packages we need install.packages(“ggplot2”) install.pacakges(“grid”)

3 Functions of R (for data scientists)
Data analysis Plotting the results of data analysis

4 Two plotting systems in R
The base plotting system Pre-installed (no need to install extra packages) Plotting function & parameters structure Different plotting functions may have different parameters ggplot2 A grammaticalized plotting system Different plots have the similar grammar structure Names of the parameters are relatively more user-friendly Anyone who can use both plotting systems?

5 An example to show the difference of two plotting systems
Scatter plot

6 Summary 1 (difference between base and ggplot2 plotting systems)
Function & parameter based vs. grammaticalized Names of the parameters in ggplot2 are more user-friendly

7 Summary 2 (3 key elements for ggplot2)
Data- a data frame with the variables to be plotted. Geometry-the kind of plot, scatter or histogram, or…? Aesthetic-the x and y coordinates of the plot. ggplot(Data, aes(x=A,y=B)) + geom_point() underscore

8 Key elements for ggplot2
Data Geometry Aesthetic underscore ggplot(Data, aes(x=A,y=B)) + geom_point()

9 Data Usually a data frame Vectors
A data frame containing the variables Vectors One vector Two vectors with the same length

10 Aesthetic x and y coordinates (names in a data frame)
Colour, size, shape A variable ggplot(data, aes(x=x,y=y)) + geom_point()

11 Geometry name description geom_point Scatterplot geom_bar Bar plot
geom_histogram Histogram geom_density Prabablity distribution plot geom_boxplot Box and whiskers plot geom_text Textual annotations in a plot geom_errorbar Error bars

12 A practice of the choice of geom_ functions
1, Plot the correlation between carat and price 2, Plot the average price of diamonds with each kind of cut/color/clarity, and then add error bars 3, Plot the distributions of carat/price 4, Plot the rough price distributions of each kind of cut 5, test if the prices between “fair” and “good” diamonds are significantly different 6, Write the P value in plot 4

13 Take-home message Data Aesthetic Geometry Usually a data frame
Can be vectors Aesthetic Mapping data to coordinate system(x, y) Distinguish between different groups of data(color, size, shape) Geometry Choose the right geometry

14 Add-on1, faceting (subgrouping and plot)
facet_grid

15 Add-on2, multiple plotting on one page
pushViewport(viewport(layout = grid.layout(2, 2))) vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y) p1<- p2<- p3<- p4<- print(p1, vp=vplayout(1,1)) print(p2, vp=vplayout(1,2)) print(p3, vp=vplayout(2,1)) print(p4, vp=vplayout(2,2)) p1 p2 p3 p4

16 Getting help ggplot2 is well documented at Google Try to post your problems to the community (stack overflow, mailing lists, etc.).

17 Polite questions To check whether the question has been asked.
Most people ignore repeated questions To give a brief and comprehensive description of your problem. Including a small sample of your data A precise description of your problem To tell what you have tried or what you think about the problem.

18 Happy plotting with ggplot2!


Download ppt "Ggplot2 Wu Shaohuan."

Similar presentations


Ads by Google