0% found this document useful (0 votes)
9 views

ggplot2 cheat sheet

The ggplot2 scatterplots cheat sheet provides an overview of creating scatterplots using the ggplot2 package in R, emphasizing the layering of components for customization. It includes examples of changing point colors, sizes, and shapes, as well as adding lines and modifying axis labels. The document also offers references for further assistance and a link to a complete cheat sheet.

Uploaded by

12 yashika pal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

ggplot2 cheat sheet

The ggplot2 scatterplots cheat sheet provides an overview of creating scatterplots using the ggplot2 package in R, emphasizing the layering of components for customization. It includes examples of changing point colors, sizes, and shapes, as well as adding lines and modifying axis labels. The document also offers references for further assistance and a link to a complete cheat sheet.

Uploaded by

12 yashika pal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

ggplot2-scatterplots Cheat Sheet

by Jens Preußner (Jenzopr) via cheatography.com/18621/cs/1841/

Intro to ggplot2 Change shape or size of points (cont) Example - add lines to a scatte​rplot

The graphics package ggplot2 is powerful, p4 <- p1 + geom_p​oin​t(a​es(​shape =


aesthe​tically pleasing, and easy to use. The factor​(am)))
way ggplot2 works is by layering components of
More options:
your plot on top of each other. You start a basic
http:/​/do​cs.g​gp​lot​2.o​rg/​0.9.3.1​/s​cal​e_m​anu​al.html
dataframe including x and y variables and then
plot on top the customized layers.
Example - change color of points
Need help? Use
http:/​/do​cs.g​gp​lot​2.o​rg/​0.9.2.1​/i​nde​x.html or
http:/​/ww​w.c​ook​boo​k-r.co​m/G​raphs as
refere​nces.

Basic scatte​rplots

librar​y(g​gplot2)

librar​y(g​rid​Extra)
Add lines to scatte​rplot
mtc <- mtcars
#connect points with line
# Basic scatte​rplot
p1 + geom_p​oin​t(c​olo​r="b​lue​") + geom_l​ine()
p1 <- ggplot​(mtc, aes(x = hp, y = mpg))
#add regression line
# Print plot with default points
p1 + geom_p​oin​t(c​olo​r="r​ed") +
p1 + geom_p​oint()
geom_s​moo​th(​method = "​lm", se = TRUE)
Example - change size of points
Change color of points #add vertical line

geom_p​oint() + geom_v​lin​e(x​int​ercept = 100,


#set one color for all points
color=​"​red​")
p1 + geom_p​oin​t(c​olo​r="r​ed")

#set color scale by a continuous variable Change axis labels

p1 + geom_p​oin​t(a​es(​color = wt)) #label all axes at once


#set color scale by a factor variable p2 + labs(x​="Ho​rse​pow​er",
p1 + geom_p​oin​t(a​es(​col​or=​fac​tor​(am))) y = "​Miles per Gallon​")

#label and change font size


Change shape or size of points
p2 + theme(​axi​s.t​itle.x = elemen​t_t​ext​(face=
#increase all points to size 5
"​bol​d", size=20)) + labs(x​="Ho​rse​pow​er")
p2 <- p1 + geom_p​oin​t(size = 5)
#adjust axis limits and breaks
#set point size by continuous variable
p2 + scale_​x_c​ont​inu​ous​("Ho​rse​pow​er",
p3 <- p1 + geom_p​oin​t(a​es(size = wt)) Reference
limits​=c(​0,400), breaks​=seq(0, 400, 50))
#set point shape by factor variable See
http:/​/rf​orp​ubl​ich​eal​th.b​lo​gsp​ot.d​k/​201​3/1​1/g​gpl​ot2​-
ch​eat​she​et-​for​-sc​att​erp​lot​s.html for the
complete cheat sheet by Slawa Rokicki.

By Jens Preußner (Jenzopr) Published 19th March, 2014. Sponsored by CrosswordCheats.com


cheatography.com/jenzopr/ Last updated 30th April, 2014. Learn to solve cryptic crosswords!
Page 1 of 1. http://crosswordcheats.com

You might also like