Download Data Visualization with ggplot2: Histograms, Density Plots, and Scatterplots and more Schemes and Mind Maps Computer Graphics in PDF only on Docsity! A Brief Introduction to Graphics with ggplot2 November 6, 2017 Introduction The ggplot2 package allows you to build very complex graphs layer by layer. Unlike graphs we construct using the base functions in R, ggplot2 takes care of details like legends and choice of plotting symbols automatically, although you can customize these choices if you wish. A handy cheatsheet which summarizes the commands available in ggplot2 can be downloaded here www.rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf. ggplot() Using the ggplot2 suite of functions you start a graphic using the ggplot() command. This command does not display anything until you add a ‘geom’ command; it just sets up the scaffolding for the plot. The syntax of ggplot() is ggplot(data = NULL, mapping = aes(x, y, <other aesthetics>)) The data argument is the dataframe containing the variables you want to graph. It must be an object of type dataframe. The x argument is set equal to the variable in your data you wish to be represented on the x-axis. The y argument will be the variable represented on the y-axis. You can map additional variables in your dataset to plot attributes like color or size of plotting symbol, by simply adding an argument like color=gender within aes(). You add (literally, using a + sign) a ’geom’ to ggplot() such as geom_histogram() or geom_dotplot() to choose the type of graph which will display the data. Let’s obtain a histogram of mpg in the mtcars dataframe. > library(ggplot2) > class(mtcars) #verify mtcars is a dataframe [1] "data.frame" > # sets up the plot, but does not produce a graph yet > p0 <- ggplot(data=mtcars, aes(x=mpg)) > #now get the graph > p0+geom_histogram() 1 0 1 2 3 4 5 10 15 20 25 30 35 mpg co un t Let’s customize the graph. There are different ‘themes’ which determine the setup of the plot area, i.e. whether gridlines are shown and the colors of gridlines and the background. See the cheatsheet, page 2, bottom right for a few choices. You can choose the fill and outline colors. > p0 <- ggplot(data=mtcars, aes(x=mpg)) > p0+geom_histogram(fill="yellow", color="red")+theme_minimal() #ugliest graph ever! 2 0.00 0.05 0.10 0.15 0.20 0.25 10 15 20 25 30 35 mpg de ns ity cyl 4 6 8 Miles per Gallon by Number of Cylinders Exercise: Using the ChickWeight data, produce separate smoothed density graphs of weights at time 21 by Diet. Adding Layers > p1 <- ggplot(data=mtcars,aes(x=mpg,y=disp,color=cyl)) > p1+geom_point()+geom_smooth(method='lm') > > # the default method in geom_smooth overfits the data, IMO > # pl+ geom_point()+geom_smooth() 5 100 200 300 400 500 10 15 20 25 30 35 mpg di sp cyl 4 6 8 If you want a multi-pane graph with the same graph repeated on subsets of you data, you can use the facets argument. You must input it as a formula. For example, in the mtcars dataset if you want to graph mpg vs. disp in separate columns for 4,6, and 8 cylinder cars, you’d use facets=.~cyl. Separate graphs for each cylinder and gear combination would use facets = cyl~gear; cylinders vary by row and gear varies by column. > mtcars$gear <- factor(mtcars$gear) > p2 <- ggplot(data=mtcars,aes(x=mpg,y=disp, color=gear)) > p2+facet_grid(.~cyl)+geom_point() > 6 4 6 8 10 15 20 25 30 3510 15 20 25 30 3510 15 20 25 30 35 100 200 300 400 mpg di sp gear 3 4 5 Exercise: Using the iris data, obtain a scatterplot of x=Petal.Length vs. y=Petal.Width. Facet by Species. A graph of longitudinal data (each subject is observed repeatedly over time) for the ChickWeight dataset. > my.data.summary <- plyr::ddply(ChickWeight, c('Time', 'Diet') , + plyr::summarise, mean = mean(weight), sd = sd(weight)) > p1 = ggplot(data=ChickWeight, aes(x=Time,y=weight,color=Diet)) > p2=p1+geom_line(aes(group=Chick)) > p3=p2+geom_line(data=my.data.summary, aes(x=Time, y=mean, color=Diet), linetype=3, size=2) > p3 > #add mean line by group > 7