Malie Lessard-Therrien and Etienne Low-Décarie
May 8, 2017
Beautiful and flexible
Day I
Your first ggplot plot
Grammar of graphics
Facets
Saving your graphs
Day II
Pretty graphs for presentation
Maps
if(!require(ggplot2)){install.packages("ggplot2")}
require(ggplot2)
head (iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
Explore your data using function “qplot”
Explain a pattern (or lack of) in your data using function “ggplot”
A basic scatter plot
qplot(data=iris,
x=Sepal.Length,
y=Sepal.Width)
qplot(data=iris,
x=Sepal.Length,
y=Sepal.Width,
xlab="Sepal length (mm)",
ylab="Sepal width (mm)")
produce a basic plot with built in data
data()
CO2
?CO2
WARNING: THERE ARE MULTIPLE CO2/co2 datasets (CASE SENSITIVE, use capitals)
2 principles:
3 essentials:
4 optionals:
set with the aes() function
set with the “geom_…” command
Ex:
A plot must have at least one geom; there is no upper limit. You can add a geom to a plot using the + operator
plot.object<-qplot() # to explore
or
plot.object<-ggplot() # to explain
plot.object<-plot.object+layer()
print(plot.object)
basic_graph<-qplot(data=iris,
x=Sepal.Length,
y=Sepal.Width,
xlab="Sepal length (mm)",
ylab="Sepal width (mm)")
print(basic_graph)
Note: aes() and geom_point()
basic_graph <- ggplot(data=iris,
aes(x=Sepal.Length,
y=Sepal.Width))+
xlab("Sepal length (mm)")+
ylab("Sepal width (mm)")+
geom_point()
print (basic_graph)
Scatter plot with colour and shape
basic_graph <- basic_graph+
aes(colour=Species,
shape=Species)
print(basic_graph)
Scatter plot with linear regression
Add a geom (eg. linear smooth)
basic_graph_lm <- basic_graph+
geom_smooth(method="lm", se=F)
print(basic_graph_lm)
Produce a colorful plot containing linear regressions with built in data
CO2
?CO2
data()
WARNING: THERE ARE MULTIPLE CO2/co2 datasets (CASE SENSITIVE, use capitals)
Categorical x-axis
basic_graph_cat <- ggplot(data=iris,
aes(x=Species,
y=Sepal.Width))+
xlab("Species")+
ylab("Sepal width (mm)")+
geom_point()
print (basic_graph_cat)
.
.
.
basic_graph_cat_bx <- basic_graph_cat+
geom_boxplot ()
print (basic_graph_cat_bx)
Categorical x-axis, geom_violin
Divide the data graphically
facet_grid(rows~columns)
iris_facets <- basic_graph +
facet_grid(. ~ Species)
print (iris_facets)
Create a colorful graph with the data you have used using geoms and facets
CO2
?CO2
data()
Using CO2 data
head (CO2)
Plant Type Treatment conc uptake
1 Qn1 Quebec nonchilled 95 16.0
2 Qn1 Quebec nonchilled 175 30.4
3 Qn1 Quebec nonchilled 250 34.8
4 Qn1 Quebec nonchilled 350 37.2
5 Qn1 Quebec nonchilled 500 35.3
6 Qn1 Quebec nonchilled 675 39.2
CO2_graph_facets<-ggplot(data=CO2,
aes(x=conc,
y=uptake,
colour=Treatment))+
geom_point()+
facet_grid(.~Type)
print(CO2_graph_facets)
Problems when adding the geom_line
print(CO2_graph_facets+geom_line())
Solution: specify groups
CO2_graph_fg<-CO2_graph_facets+geom_line(aes(group=Plant))
print(CO2_graph_fg)
pdf("./Plots/todays_plots.pdf")
print(basic_graph)
print(basic_graph_lm)
print(basic_graph_cat_bx)
print(iris_facets)
print(CO2_graph_facets)
graphics.off()
all other R-base save functions available:
bmp()
, jpeg()
, etc
ggsave: saves last plot and guesses format from file name
ggsave("./Plots/todays_plots.jpeg", iris_facets)
Pretty and meaningful graphs
Maps
Give new color to each iris species
Step 1: Create the reference graph
iris_color_graph <- ggplot (data=iris,
aes (x= Sepal.Length,
y=Sepal.Width,
color=Species))+
xlab("Sepal lenght (mm)")+
ylab("Sepal width (mm)")+
geom_point(size = 3)+
geom_smooth(method="lm", se=F)
print (iris_color_graph)
Give new color to each iris species
Step 2: Change color manually, using base color
iris_color_graph_manual <- iris_color_graph +
scale_color_manual(values=c("setosa"="red",
"versicolor"="pink",
"virginica"="purple"),
labels=c("Setosa",
"Versicolor",
"Virginica"))
print (iris_color_graph_manual)
Give new color to each iris species
Step 2: Change color manually, using hexadecimal codes
iris_color_graph_manual_hexa <- iris_color_graph +
scale_color_manual(values=c("setosa"="#6600CC",
"versicolor"="#990099",
"virginica"="#FF3399"),
labels=c("Setosa",
"Versicolor",
"Virginica"))
print (iris_color_graph_manual_hexa)
Change the shape of data points manually with the dataset you have used and/or your own data
see: http://www.cookbook-r.com/Graphs/Shapes_and_line_types/
CO2
?CO2
data()
DF <- data.frame(variable = LETTERS[1:10], value = sample(10, replace = TRUE))
bar_graph <- ggplot(data=DF,
aes(x=variable,
fill=variable,
y=value))+
geom_bar(stat="identity")
print(bar_graph)
Change looks of graph by changing coordinates
polar_graph <- bar_graph +
coord_polar()
print(polar_graph)
Change font size
iris_classic_fs <- iris_color_graph_manual_hexa +
theme(axis.title.x=element_text(size=30),
axis.text.x=element_text(size=25))+
theme(axis.title.y=element_text(size=30),
axis.text.y=element_text(size=25))
print (iris_classic_fs)
Set B&W using theme_classic ()
iris_classic <- iris_color_graph_manual_hexa+
theme_classic(base_size = 20)
print (iris_classic)
see https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html
if(!require(cowplot)){install.packages("cowplot")}
require(cowplot)
With cowplot, your graph background is B&W by default
iris_cowplot <- iris_color_graph_manual_hexa
print (iris_cowplot)
Create a B&W background graph with the dataset you have used and/or your own data
Change the font of the axis titles, see http://www.cookbook-r.com/Graphs/Fonts/
CO2
?CO2
data()
Label and align multiple graphs
fig.1 <- plot_grid(iris_color_graph_manual, iris_cowplot,
labels = c("a)", "b)"),
nrow = 2,
align = "v")
print (fig.1)
Label and align multiple graphs
fig.1 <- plot_grid(basic_graph_cat_bx, iris_cowplot,
labels = c("a)", "b)"),
nrow = 2,
align = "v")
print (fig.1)
Function “plot” in R-base for diagnostic plots
lm.iris <- lm(Petal.Length ~ Sepal.Length + Sepal.Width + Petal.Width,
data = iris)
plot(lm.iris)
Data visualization tools for statistical analysis results
ggfortify
provides autoplot
and
fortify
for common models
require(ggfortify)
autoplot(lm.iris)