Show Menu
Cheatography

Data Visualization in R through ggplot2 Cheat Sheet by

Cheat Sheet for Data Visualization in R studio through ggplot2 package.

Prereq­uisites

# instal­l.p­ack­age­s("t­idy­ver­se")
librar­y("g­gpl­ot2­")
The ggplot2 packages are included in a popular collection of packages called “the tidyve­rse”.

Basics

ggplot2 is based on the grammar of graphics, the idea that you can build every graph from the same compon­ents: a data set, a coordinate system, and geoms—­visual marks that represent data points.
To display data values, map variables in the data set to aesthetic properties of the geom like size, color, and x and y locations.

Create a graph using ggplot() or qplot()

qplot(x = cty, y = hwy, color = cyl, data = mpg, geom = "­poi­nt") Creates a complete plot with given data, geom, and mappings. Supplies many useful defaults.
ggplot­(data = mpg, aes(x = cty, y = hwy)) Begins a plot that you finish by adding layers to. Add one geom function per layer.
Add a new layer to a plot with a geom_() or stat_() function. Each provides a geom, a set of aesthetic mappings, and a default stat and position adjust­ment.

Labels

t + labs( x = "New x axis label", y = "New y axis label", title ="Add a title above the plot", subtitle = "Add a subtitle below title", caption = "Add a caption below plot", <ae­s> = "New <ae­s> legend title") t + annota­te(geom = "­tex­t", x = 8, y = 9, label = "­A")

Red Greeb Blue (RGB) Color Space

RGB is the built-in colour space. Instead of "­man­ual­ly" creating a #RRGGBB colour string, a colour can be specified using R's rgb() function that takes three arguments: red, green, and blue (which, by default, all have a range of [0, 1]).

RGB Color Space

> ggplot() + + facet_­­wr­a­p(~b) + + scale_­­x_­c­o­nt­­inu­­ou­s­(­na­­me=­­"­r­ed­­", breaks­­=s­e­q­(0.05, 1.05, 0.2), labels­­=s­eq(0, 1, 0.2)) + + scale_­­y_­c­o­nt­­inu­­ou­s­(­na­­me=­­"­g­re­­en", breaks­­=s­e­q­(0.05, 1.05, 0.2), labels­­=s­eq(0, 1, 0.2)) + + scale_­­fi­l­l­_i­­den­­tity() + + geom_r­­ec­t­(­da­­ta=d, mappin­­g=­a­e­s(­­xmin=r, xmax=r­­+r­e­s­ol­­uti­­on(r), ymin=g, ymax=g­­+r­e­s­ol­­uti­­on(g), fill=r­­gb­(­r­,g­­,b)), color=­­"­w­hi­­te", size=0.1)

Legends:

n + theme(­leg­end.po­sition = "­bot­tom­") Place legend at "­bot­tom­", "­top­", "­lef­", or "­rig­ht"
n + guides­(fill = "­non­e") Set legend type for each aesthetic: colorbar, legend, or none (no legend)
n + scale_­fil­l_d­isc­ret­e(name = "­Tit­le", labels = c("A­", "­B", "­C", "­D", "­E")) Set legend title and labels with a scale function.
 

One Variable graphs

ggplot(df, aes(x=­­we­ight, fill=sex)) + + geom_a­­re­a­(stat ="bi­­n")

geom_d­ensity

ggplot­­­(­d­i­­a­­monds, aes(depth, fill = cut, colour = cut)) + + geom_d­­­e­n­s­­i­­ty­­­(alpha = 0.1) xlim(55, 70)

geom_bar

> g <- ggplot­­(mpg, aes(cl­­ass)) > g + geom_b­­ar­(­a­es­­(fill = drv))

geom_d­otplot

ggplot­­(m­t­cars, aes(x = mpg, fill = factor­­(c­yl))) + geom_d­­ot­p­l­ot­­(st­­ac­k­g­roups = TRUE, binwidth = 1, method = "­­hi­s­t­od­­ot")

Color Blind Friendly brewer palettes:

librar­y(R­Col­orB­rewer)
displa­y.b­rew­er.a­ll­(co­lor­bli­ndF­riendly = TRUE)

Line Types:

ggpubr­::s­how­_li­ne_­typ­es()+
theme_­gray()
 

Two Variable graphs

ggplot­­(iris, aes(Se­­pa­l.L­e­ngth, Sepal.W­­i­d­th))+ + geom_p­­oi­n­t­(a­­es(­­color = Species)) + + geom_s­­mo­o­t­h(­­aes­­(color = Species, fill = Species), method = "­­lm­") + + scale_­­co­l­o­r_­­vir­­id­i­s­(d­­iscrete = TRUE, option = "­­D")+ + scale_­­fi­l­l­_v­­iri­­di­s­(­di­­screte = TRUE)

geom_b­oxplot

bp <- bp + geom_b­­ox­p­l­ot­­(ae­­s(fill = Species)) > bp + scale_­­fi­l­l­_m­­anu­­al­(­v­alues = c("#­­00­A­F­BB­­", "­­#E­7­B­80­­0", "­­#F­C­4­E0­­7"))

geom_b­ind2d

> d <- ggplot­­(d­i­a­monds, aes(x, y)) + xlim(4, 10) + ylim(4, 10) > d + geom_b­­in­2­d­(bins = 10)

Three Variable graphs

df <- melt(v­­ol­cano) > > p <- ggplot(df, aes(Var1, Var2)) + + geom_r­­as­t­e­r(­­aes­­(f­i­l­l=­­value)) + + scale_­­fi­l­l­_d­­ist­­il­l­e­r(­­palette = "­­Sp­e­c­tr­­al", direction = -1) + + labs(x­­="West to East", + y="North to South", + title = "­­El­e­v­ation map of Maunga Whau", + fill = "­­El­e­v­at­­ion­­") + + theme(text = elemen­­t_­t­e­xt­­(family = 'Fira Sans'), + plot.title = elemen­­t_­t­e­xt­­(hjust = 0.5)) > > ggplot­­ly(p)

Shapes:

> ggplot() + scale_­y_c­ont­inu­ous­(na­me=­"­") + scale_­x_c­ont­inu­ous­(na­me=­"­") + scale_­sha­pe_­ide­ntity() +
geom_p­oin­t(d­ata=d, mappin­g=a­es(­x=p­%%16, y=p%/%16, shape=p), size=5, fill="r­ed")­+ge­om_­tex­t(d­ata=d, mappin­g=a­es(­x=p­%%16, y=p%/%­16+­0.25, label=p), size=3)
           
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          ggplot2-scatterplots Cheat Sheet
          iGraph Cheat Sheet