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

Learning Ggplot2

Aprenda ggplot2

Uploaded by

Justo Fuentes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Learning Ggplot2

Aprenda ggplot2

Uploaded by

Justo Fuentes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

ggplot2

#ggplot2
Table of Contents
About 1

Chapter 1: Getting started with ggplot2 2

Remarks 2

Examples 2

How to install and run ggplot2 2

Basic example of ggplot2 2

Chapter 2: Customizing axes, titles, and legends 5

Introduction 5

Examples 5

Change legend title and increase keysize 5

Compare frequencies across groups and remove legend title 5

Place overlapping objects next to each other and change colours of axes texts 6

Fine tuning axes ticks, texts, and titles 7

Chapter 3: Plot a subset of data 9

Syntax 9

Examples 9

Using xlim / ylim 9

Inline Subsetting for categorical variables 9

Chapter 4: Plotting time series 10

Examples 10

Ploting time series 10

Credits 14
About
You can share this PDF with anyone you feel could benefit from it, downloaded the latest version
from: ggplot2

It is an unofficial and free ggplot2 ebook created for educational purposes. All the content is
extracted from Stack Overflow Documentation, which is written by many hardworking individuals at
Stack Overflow. It is neither affiliated with Stack Overflow nor official ggplot2.

The content is released under Creative Commons BY-SA, and the list of contributors to each
chapter are provided in the credits section at the end of this book. Images may be copyright of
their respective owners unless otherwise specified. All trademarks and registered trademarks are
the property of their respective company owners.

Use the content presented in this book at your own risk; it is not guaranteed to be correct nor
accurate, please send your feedback and corrections to [email protected]

https://riptutorial.com/ 1
Chapter 1: Getting started with ggplot2
Remarks
This section provides an overview of what ggplot2 is, and why a developer might want to use it.

It should also mention any large subjects within ggplot2, and link out to the related topics. Since
the Documentation for ggplot2 is new, you may need to create initial versions of those related
topics.

Examples
How to install and run ggplot2

To install and load the current stable version of ggplot2 for your R installation use:

# install from CRAN


install.packages("ggplot2")

To install the development version from github use

# install.packages("devtools")
devtools::install_github("hadley/ggplot2")

Load into your current R session, and make an example.

Basic example of ggplot2

We show a plot similar to the showed at Linear regression on the mtcars dataset. First with
defaults and the with some customization of the parameters.

#help("mtcars")
fit <- lm(mpg ~ wt, data = mtcars)
bs <- round(coef(fit), 3)
lmlab <- paste0("mpg = ", bs[1],
ifelse(sign(bs[2])==1, " + ", " - "), abs(bs[2]), " wt ")
#range(mtcars$wt)
library("ggplot2")

#with defaults
ggplot(aes(x=wt, y=mpg), data = mtcars) +
geom_point() +
geom_smooth(method = "lm", se=FALSE, formula = y ~ x)

https://riptutorial.com/ 2
#some customizations
ggplot(aes(x=wt, y=mpg,colour="mpg"), data = mtcars) +
geom_point(shape=21,size=4,fill = "blue",alpha=0.55, color="red") +
scale_x_continuous(breaks=seq(0,6, by=.5)) +
geom_smooth(method = "lm", se=FALSE, color="darkgreen", formula = y ~ x) +
geom_hline(yintercept=mean(mtcars$mpg), size=0.4, color="magenta") +
xlab("Weight (1000 lbs)") + ylab("Miles/(US) gallon") +
labs(title='Linear Regression Example',
subtitle=lmlab,
caption="Source: mtcars") +
annotate("text", x = 4.5, y = 21, label = "Mean of mpg") +
annotate("text", x = 4.8, y = 12, label = "Linear adjustment",color = "red") +
theme_bw()

https://riptutorial.com/ 3
See other examples at ggplot2

Read Getting started with ggplot2 online: https://riptutorial.com/ggplot2/topic/3324/getting-started-


with-ggplot2

https://riptutorial.com/ 4
Chapter 2: Customizing axes, titles, and
legends
Introduction
In this topic, we'll look to explain how to Customise axes, titles and legends whilst using the
ggplot2 library.

Examples
Change legend title and increase keysize

# load the library


library(ggplot2)

# create a blank canvas


g <- ggplot(data = diamonds)

g + geom_bar(aes(x = cut, fill = cut)) +


scale_fill_discrete(guide = guide_legend(title = "CUT",
keywidth = 2,
keyheight = 2))

Compare frequencies across groups and remove legend title

https://riptutorial.com/ 5
g + geom_bar(aes(x = cut, fill = color), position = "fill") +
guides(fill = guide_legend(title = NULL))

Place overlapping objects next to each other and change colours of axes
texts

g + geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge") +


theme(axis.text = element_text(colour = "red", size = 12))

https://riptutorial.com/ 6
Fine tuning axes ticks, texts, and titles

g + geom_histogram(aes(price, fill = cut), binwidth = 500) +


labs(x = "Price", y = "Number of diamonds",
title = "Distribution of prices \n across Cuts") +
theme(plot.title = element_text(colour = "red", face = "italic"),
axis.title.x = element_text(face="bold",
colour="darkgreen", size = 12),
axis.text.x = element_text(angle = 45, vjust = 0.5, size = 12),
axis.title.y = element_text(face="bold",
colour="darkblue", size = 12),
axis.text.y = element_text(size = 12, colour = "brown"))

https://riptutorial.com/ 7
Read Customizing axes, titles, and legends online:
https://riptutorial.com/ggplot2/topic/9117/customizing-axes--titles--and-legends

https://riptutorial.com/ 8
Chapter 3: Plot a subset of data
Syntax
• xlim(left.limit,right.limit)
• data.frame[data.frame$variable == "desired.variable",]

Examples
Using xlim / ylim

> library(ggplot2)
> ggplot(iris,aes(Sepal.Width)) + geom_density() + xlim(1,3.5)

Using xlim or ylim the plot is not cutted, ggplot subsets the data before calling the stat function
(stat_density in this case). You can see it in the warning message.

Warning message:
Removed 19 rows containing non-finite values (stat_density).

Inline Subsetting for categorical variables

ggplot(iris[iris$Species == "setosa",],aes(Sepal.Width)) +
geom_density()

Here, we are subsetting the dataframe before passing it to ggplot. It is a very useful tool derived
from the data frame data structure.

To make the code more readable, one can also use dplyr's filter:

library(dplyr)
iris %>% filter(Species == "setosa") %>% ggplot(aes(Sepal.Width)) +
geom_density()

Read Plot a subset of data online: https://riptutorial.com/ggplot2/topic/6585/plot-a-subset-of-data

https://riptutorial.com/ 9
Chapter 4: Plotting time series
Examples
Ploting time series

This example illustrates how to plot a time series of temperature from a csv data file.

library(ggplot2)

# Original data not provided, see subset/plotted data below


datos.orig<-read.csv("data.csv",header=TRUE) # read csv data

# Change dates to POSIXct


fecha<-as.data.frame(as.POSIXct(datos.orig$Fecha))

# Build a new and shorter data frame for temperature


datos.graf<-cbind.data.frame(fecha,datos.orig$Temp_Max,datos.orig$Temp_Min)
colnames(datos.graf)<-c("fecha","TMax","Tmin")

# Plot
ggplot() +
geom_line(data=datos.graf,aes(x=fecha, y=TMax),colour="red") +
geom_line(data=datos.graf,aes(x=fecha, y=Tmin),colour="blue") +
ylab("Temperature axis title") +
xlab(" ") +
scale_x_datetime(
expand=c(0,0), # avoid blank space around plot
date_breaks = "1 month", # main axis breaks
date_labels="%d/%m/%Y") + # axis labels date format
scale_y_continuous(expand=c(0,0), limits=c(-10,40)) +
theme(axis.text.x = element_text(angle = 60, hjust = 1)) +
ggtitle("Temperature main title") +
theme(plot.title = element_text(size=10, vjust=0.5, hjust=0.5))

giving this plot

https://riptutorial.com/ 10
Data used for the plot

dput(datos.graf)
structure(list(fecha = structure(c(1262300400, 1262386800, 1262473200,
1262559600, 1262646000, 1262732400, 1262818800, 1262905200, 1262991600,
1263078000, 1263164400, 1263250800, 1263337200, 1263423600, 1263510000,
1263596400, 1263682800, 1263769200, 1263855600, 1263942000, 1264028400,
1264114800, 1264201200, 1264287600, 1264374000, 1264460400, 1264546800,
1264633200, 1264719600, 1264806000, 1264892400, 1264978800, 1265065200,
1265151600, 1265238000, 1265324400, 1265410800, 1265497200, 1265583600,
1265670000, 1265756400, 1265842800, 1265929200, 1266015600, 1266102000,
1266188400, 1266274800, 1266361200, 1266447600, 1266534000, 1266620400,
1266706800, 1266793200, 1266879600, 1266966000, 1267052400, 1267138800,
1267225200, 1267311600, 1267398000, 1267484400, 1267570800, 1267657200,
1267743600, 1267830000, 1267916400, 1268002800, 1268089200, 1268175600,
1268262000, 1268348400, 1268434800, 1268521200, 1268607600, 1268694000,
1268780400, 1268866800, 1268953200, 1269039600, 1269126000, 1269212400,
1269298800, 1269385200, 1269471600, 1269558000, 1269644400, 1269730800,
1269813600, 1269900000, 1269986400, 1270072800, 1270159200, 1270245600,
1270332000, 1270418400, 1270504800, 1270591200, 1270677600, 1270764000,
1270850400, 1270936800, 1271023200, 1271109600, 1271196000, 1271282400,
1271368800, 1271455200, 1271541600, 1271628000, 1271714400, 1271800800,
1271887200, 1271973600, 1272060000, 1272146400, 1272232800, 1272319200,
1272405600, 1272492000, 1272578400, 1272664800, 1272751200, 1272837600,
1272924000, 1273010400, 1273096800, 1273183200, 1273269600, 1273356000,
1273442400, 1273528800, 1273615200, 1273701600, 1273788000, 1273874400,
1273960800, 1274047200, 1274133600, 1274220000, 1274306400, 1274392800,
1274479200, 1274565600, 1274652000, 1274738400, 1274824800, 1274911200,
1274997600, 1275084000, 1275170400, 1275256800, 1275343200, 1275429600,
1275516000, 1275602400, 1275688800, 1275775200, 1275861600, 1275948000,
1276034400, 1276120800, 1276207200, 1276293600, 1276380000, 1276466400,
1276552800, 1276639200, 1276725600, 1276812000, 1276898400, 1276984800,
1277071200, 1277157600, 1277244000, 1277330400, 1277416800, 1277503200,
1277589600, 1277676000, 1277762400, 1277848800, 1277935200, 1278021600,
1278108000, 1278194400, 1278280800, 1278367200, 1278453600, 1278540000,
1278626400, 1278712800, 1278799200, 1278885600, 1278972000, 1279058400,
1279144800, 1279231200, 1279317600, 1279404000, 1279490400, 1279576800,

https://riptutorial.com/ 11
1279663200, 1279749600, 1279836000, 1279922400, 1280008800, 1280095200,
1280181600, 1280268000, 1280354400, 1280440800, 1280527200, 1280613600,
1280700000, 1280786400, 1280872800, 1280959200, 1281045600, 1281132000,
1281218400, 1281304800, 1281391200, 1281477600, 1281564000, 1281650400,
1281736800, 1281823200, 1281909600, 1281996000, 1282082400, 1282168800,
1282255200, 1282341600, 1282428000, 1282514400, 1282600800, 1282687200,
1282773600, 1282860000, 1282946400, 1283032800, 1283119200, 1283205600,
1283292000, 1283378400, 1283464800, 1283551200, 1283637600, 1283724000,
1283810400, 1283896800, 1283983200, 1284069600, 1284156000, 1284242400,
1284328800, 1284415200, 1284501600, 1284588000, 1284674400, 1284760800,
1284847200, 1284933600, 1285020000, 1285106400, 1285192800, 1285279200,
1285365600, 1285452000, 1285538400, 1285624800, 1285711200, 1285797600,
1285884000, 1285970400, 1286056800, 1286143200, 1286229600, 1286316000,
1286402400, 1286488800, 1286575200, 1286661600, 1286748000, 1286834400,
1286920800, 1287007200, 1287093600, 1287180000, 1287266400, 1287352800,
1287439200, 1287525600, 1287612000, 1287698400, 1287784800, 1287871200,
1287957600, 1288044000, 1288130400, 1288216800, 1288303200, 1288389600,
1288476000, 1288566000, 1288652400, 1288738800, 1288825200, 1288911600,
1288998000, 1289084400, 1289170800, 1289257200, 1289343600, 1289430000,
1289516400, 1289602800, 1289689200, 1289775600, 1289862000, 1289948400,
1290034800, 1290121200, 1290207600, 1290294000, 1290380400, 1290466800,
1290553200, 1290639600, 1290726000, 1290812400, 1290898800, 1290985200,
1291071600, 1291158000, 1291244400, 1291330800, 1291417200, 1291503600,
1291590000, 1291676400, 1291762800, 1291849200, 1291935600, 1292022000,
1292108400, 1292194800, 1292281200, 1292367600, 1292454000, 1292540400,
1292626800, 1292713200, 1292799600, 1292886000, 1292972400, 1293058800,
1293145200, 1293231600, 1293318000, 1293404400, 1293490800, 1293577200,
1293663600, 1293750000), class = c("POSIXct", "POSIXt"), tzone = ""),
TMax = c(17.78, 16.47, 13.8, 11.34, 13.55, 12.33, 6.32, 4.43,
5.27, 9.78, 8.42, 9.59, 17.7, 17.3, 17.49, 13.76, 14.34,
12.34, 11.48, 18.13, 16.08, 15.22, 11.43, 14.19, 8.94, 11.27,
9.86, 12.18, 16.31, 16.26, 14.04, 12.4, 11.05, 11.91, 9.68,
20.32, 19.52, 15.22, 12.68, 12.33, 13.7, 7.93, 9.6, 7.67,
9.27, 6.3, 10.72, 17.38, 14.29, 14.34, 16.08, 12.21, 17.42,
18.87, 22.57, 16.61, 19.4, 15.18, 17.79, 20.08, 16.04, 11.11,
19.28, 14.35, 12.67, 11.13, 8.17, 12.62, 9.42, 10.72, 10.03,
12.87, 13.97, 14.01, 13.5, 13.85, 13.32, 14.56, 19.53, 17.24,
15.22, 16.12, 14.51, 16.38, 18.19, 20.64, 17.96, 15.98, 22.05,
17.76, 18.53, 15.9, 16.81, 20, 15.86, 15.91, 14.14, 17.83,
17.85, 18.86, 20.9, 13.74, 12.62, 13.97, 15.9, 16.98, 12.99,
16.65, 18.29, 20.52, 18.65, 17.01, 21.3, 19.91, 19.39, 22.16,
21.47, 21.25, 20.84, 20.08, 20.39, 20.57, 13.78, 12.69, 18.36,
17.23, 17.16, 19.08, 20.26, 21.81, 23.42, 19.13, 18.99, 17.95,
20.9, 21.35, 19.42, 19.13, 21.4, 21.8, 23.95, 21.59, 21.98,
22.69, 22.48, 24.33, 23.04, 24.42, 24.5, 27.92, 34.87, 25.89,
24.21, 24.2, 24.63, 24.95, 24.38, 24.95, 26.41, 23.89, 26.33,
26.27, 24.34, 24.96, 23.8, 22.51, 22.49, 24.41, 23.34, 28.63,
25.89, 23.34, 23.23, 23.83, 24.33, 24.55, 26.87, 28.28, 29.4,
28.9, 27.87, 29.29, 28.09, 29.31, 29.1, 29.98, 28.21, 28.28,
28.36, 29.4, 29.29, 29.51, 30.49, 30.45, 29.78, 30.24, 29.96,
31.12, 29.06, 29.24, 29.37, 30.07, 30.33, 29.14, 27.41, 28.92,
29.06, 28.71, 28.99, 29.82, 29.8, 29.05, 29.26, 29.29, 29.82,
30.16, 31.9, 27.43, 28.4, 29.33, 30.78, 29.72, 31.57, 29.96,
24.44, 26.18, 25.78, 26.57, 27.81, 28.25, 25.45, 27.91, 30.42,
29.24, 29.16, 29.57, 28.68, 29.78, 33.67, 31.02, 26.66, 29.42,
27.51, 27.3, 26.52, 26.77, 27.88, 28.28, 27.94, 31.63, 28.58,
29.09, 25.98, 26.16, 26.39, 26.92, 27.86, 26.55, 27, 27.1,
26.02, 24.63, 25.01, 26.4, 27.13, 25.05, 28.55, 24.55, 23.51,
20.91, 23.79, 23.95, 25.16, 25.71, 26.44, 24.76, 26.94, 24.11,
24.7, 24.08, 26.09, 25.71, 23.99, 22.26, 17.3, 22.54, 22.52,
21.1, 20.43, 19.92, 19.74, 19.19, 19.9, 20.08, 21.05, 21.46,

https://riptutorial.com/ 12
20.49, 18.02, 18.97, 19.42, 20.66, 18.7, 19.66, 21.7, 20.41,
22.06, 21.8, 21.91, 20.16, 19.19, 22.31, 17.58, 22.31, 20.43,
19.72, 20.52, 19.73, 19.02, 16.65, 18.82, 15.27, 18.01, 13.1,
17.27, 18.87, 16.63, 17.74, 16.38, 15.04, 13.83, 13.27, 14.47,
8.75, 13.13, 15.58, 13.86, 10.97, 12.44, 11.76, 20.13, 17.97,
21.33, 18.67, 14.47, 15.42, 14.31, 14.35, 12.71, 8.8, 9.45,
11.58, 12.96, 12.53, 13.77, 11.81, 13.25, 13.68, 11.97, 9.27,
11.79, 13.21, 13.63, 14.02, 14.61, 14.65), Tmin = c(8.98,
4.36, 2.88, 7.35, 8.04, 5.95, 2.87, 1.86, 1.98, 2.26, 1.1,
-0.26, 4.41, 6.26, 3.58, 2.94, 8.23, 9.99, 8.26, 5.48, 5.41,
2.92, 5.2, 7.21, 5.01, 3.8, 2.61, 0.24, 4.32, 6.46, 4.61,
4.66, 1.05, 0.14, 3.58, 6.72, 4.63, 7.52, 5.51, 6.73, 6.16,
2.31, 1.81, 2.23, 0.71, 1.4, 4.36, 7.5, 3.4, 7.28, 2.52,
1.72, 3.55, 5.6, 6.71, 11.23, 6.77, 8.41, 5.13, 4.32, 5.59,
6.64, 5.51, 8.57, 3.32, 2.93, 3.65, 3.81, 0.99, 0.87, 1.26,
2.71, 0.79, 0.53, 3.28, 2.38, 2.79, 4.75, 10.07, 12.15, 11.08,
7.91, 10.17, 10.55, 8.41, 7.64, 8.63, 7.35, 8.2, 6.62, 7.32,
4.79, 5.02, 9.43, 5.77, 8.48, 9.33, 8.15, 5.58, 5.64, 7.9,
9.65, 8.67, 9.39, 6.65, 8.48, 10.07, 10.09, 8.97, 9.02, 11.18,
10.71, 12.42, 9.58, 11.06, 10.13, 12.49, 10.59, 10.65, 11.87,
13.86, 12.34, 8.85, 9.52, 9.7, 9.84, 7.3, 7.77, 10.91, 9.66,
11.01, 10.12, 9.02, 8.99, 9.86, 11.28, 10.9, 10.04, 10.96,
10.26, 10.74, 10.92, 12.73, 10.66, 11.92, 12.37, 14.31, 14.51,
12.17, 19, 21.11, 19.56, 16.77, 14.38, 16.49, 15.34, 16.74,
16.32, 16.32, 17.81, 17.15, 15.33, 13.33, 14.62, 14.33, 12.3,
13.18, 13.14, 12.95, 15.8, 16.03, 13.96, 14.26, 15.28, 14.26,
14.67, 15.07, 15.36, 18.16, 16.07, 18.37, 17.24, 17.25, 19.28,
20.8, 19.15, 18.02, 18.29, 19.29, 19.06, 21.11, 19.2, 19.65,
20.3, 22, 22.49, 21.43, 23.19, 21.44, 21.5, 21.45, 19.77,
20.63, 19.59, 17.87, 20.88, 20.54, 18.77, 18.83, 20.79, 19.13,
21.5, 21.39, 22.06, 22.09, 20.86, 21.78, 17.13, 18.91, 18.55,
22.35, 22.39, 22.04, 20.96, 16.67, 16.31, 14.81, 17.23, 17.94,
18.79, 17.58, 16.81, 19.2, 17.84, 19.79, 20.62, 20.34, 20.35,
21.16, 21.01, 17.07, 16.58, 14.78, 20.23, 18.69, 17.76, 17.67,
18.5, 18.3, 18.83, 17.17, 16.07, 14.35, 14.85, 15.89, 16.2,
15.19, 15.84, 18.67, 17.14, 16.87, 13.98, 16.29, 16.82, 18.13,
17.97, 16.98, 15.68, 14.41, 10.73, 11.17, 9.99, 15.28, 15.51,
15.21, 14.49, 15.73, 11.68, 12.16, 17.96, 18.84, 15.35, 14.22,
11.94, 13.7, 14.55, 12.69, 10.95, 10.79, 10.28, 10.67, 6.96,
5.69, 8.35, 10.88, 11.14, 9.24, 10.46, 5.95, 4.72, 7.13,
7.72, 11.25, 13.43, 12.29, 14.97, 10.54, 9, 9.33, 7.36, 8.76,
7.74, 8.08, 10.15, 7.6, 8.29, 8.22, 7.85, 11.41, 8.22, 6.62,
7.46, 4.34, 2.77, 4.29, 8.71, 5.5, 5.02, 2.56, 3.74, 1.36,
1.02, 0.02, 5.78, 3.13, 1.84, 2.65, -0.39, 4.1, 6.62, 13.35,
12.57, 9.56, 9.16, 5.1, 4.15, 5.82, 3.99, 2.89, 0.44, -2.35,
1.26, 3.16, 2.62, 4.8, 5.33, 5.66, 6.99, 1.88, -2.21, -2.43,
-0.51, 1.76, 6.76, 6.82)), .Names = c("fecha", "TMax", "Tmin"
), row.names = c(NA, -365L), class = "data.frame")

Read Plotting time series online: https://riptutorial.com/ggplot2/topic/9035/plotting-time-series

https://riptutorial.com/ 13
Credits
S.
Chapters Contributors
No

Getting started with


1 CMichael, Community, James, rcs, Robert, theArun
ggplot2

Customizing axes,
2 Sumedh, theArun
titles, and legends

3 Plot a subset of data David Mas, slhck

4 Plotting time series pacomet

https://riptutorial.com/ 14

You might also like