ggthemes
ggplot
part 6At the end of this exercise, you will be able to:
1. Adjust the aesthetics of plots in ggplot using themes
,
RColorBrewer
, and paletteer.
2. Use faceting to produce multi-panel plots.
library(tidyverse)
library(RColorBrewer)
library(paletteer)
library(janitor)
options(scipen=999) #cancels the use of scientific notation for the session
For this tutorial, we will use:
Homerange.
The data are from: Tamburello N, Cote IM, Dulvy NK (2015) Energy and the
scaling of animal space use. The American Naturalist 186(2):196-211. http://dx.doi.org/10.1086/682070.
homerange <- read_csv("data/Tamburelloetal_HomeRangeDatabase.csv", na = c("", "NA", "\\"))
There are many options to change the theme of your plots within ggplot. Have a look [here]https://ggplot2.tidyverse.org/reference/ggtheme.html) for a list of the themes.
Let’s start by building a simple barplot.
p <- homerange %>%
ggplot(aes(x=taxon, fill=trophic.guild))+
geom_bar(na.rm=T, position="dodge")
Have a look at the linedraw
theme; I am adding it as
another layer.
p + theme_linedraw()+
theme(axis.text.x = element_text(angle = 60, hjust=1))+
labs(title = "Observations by Taxon in Homerange Data",
x = NULL,
y= "n",
fill= "Trophic Guild")
log10.mass
and
log10.preymass
. Color the points by taxon
.
Store this plot as object q
.q <- homerange %>%
ggplot(aes(x=log10.mass, y=log10.preymass, color=taxon))+
geom_point(na.rm=T)
q
but add the classic theme.q + theme_classic()
There are lots of options to manipulate legends. Have a look here.
p+theme_linedraw()+
theme(legend.position = "bottom",
axis.text.x = element_text(angle = 60, hjust=1))+
labs(title = "Observations by Taxon in Homerange Data",
x = NULL,
y= "n",
fill= "Trophic Guild")
ggthemes
There are many packages that include additional themes, one of which is ggthemes. Some of these are nice because they are designed to mimic the look of popular publications.
#install.packages("ggthemes")
library(ggthemes)
Here is a list of the ggthemes
ls("package:ggthemes")[grepl("theme_", ls("package:ggthemes"))]
## [1] "theme_base" "theme_calc" "theme_clean"
## [4] "theme_economist" "theme_economist_white" "theme_excel"
## [7] "theme_excel_new" "theme_few" "theme_fivethirtyeight"
## [10] "theme_foundation" "theme_gdocs" "theme_hc"
## [13] "theme_igray" "theme_map" "theme_pander"
## [16] "theme_par" "theme_solarized" "theme_solarized_2"
## [19] "theme_solid" "theme_stata" "theme_tufte"
## [22] "theme_wsj"
p +
theme_stata()+
theme(legend.position = "bottom",
axis.text.x = element_text(angle = 60, hjust=1))+
labs(title = "Observations by Taxon in Homerange Data",
x = NULL,
y= "n",
fill= "Trophic Guild")
q
and try a theme of your choice.q+theme_clean()
The default colors used by ggplot are uninspiring. They don’t make plots pop out in presentations or publications, and you may want to use a customized palette to make things visually consistent.
Access the help for RColorBrewer
.
?RColorBrewer
The thing to notice is that there are three different color palettes:
1) sequential, 2) diverging, and 3) qualitative. Within each of these
there are several selections. You can bring up the colors by using
display.brewer.pal()
. Specify the number of colors that you
want and the palette name.
display.brewer.pal(5,"Accent") #qualitative palette
The R Color Brewer website is very helpful for getting an idea of the color palettes. To make things easy, use these two guidelines:
+scale_colour_brewer()
is for points
+scale_fill_brewer()
is for fills
Here I chose the Set2
palette. Take a moment and
experiment with other options.
p+scale_fill_brewer(palette = "Set2")+
theme(legend.position = "bottom",
axis.text.x = element_text(angle = 60, hjust=1))+
labs(title = "Observations by Taxon in Homerange Data",
x = NULL,
y= "n",
fill= "Trophic Guild")
same
q` plot, but test out one of R Color
Brewer palettes.q+theme_classic()+
scale_colour_brewer(palette = "Paired")
You can also use paleteer
to build a custom palette for
consistency. To access the paleteer
collection, I add it to
a new object.
colors <- paletteer::palettes_d_names
Now we can display the palettes. Assign the palette to
my_palette
and then build this base R bar plot. There are a
lot of options; paleteer
is a collection of popular
palettes. I really like the [ggsci
package] (https://cran.r-project.org/web/packages/ggsci/vignettes/ggsci.html)
my_palette <- paletteer_d("ggsci::nrc_npg")
barplot(rep(1,6), axes=FALSE, col=my_palette)
Now we just identify my_palette
as part of
scale_fill_manual()
p+scale_fill_manual(values=my_palette)+
theme(legend.position = "bottom",
axis.text.x = element_text(angle = 60, hjust=1))+
labs(title = "Observations by Taxon in Homerange Data",
x = NULL,
y= "n",
fill= "Trophic Guild")
same
qplot, but test out one of
paleteer`
themes.q+theme_classic()+
scale_colour_manual(values=my_palette)
Faceting is one of the amazing features of ggplot. It allows us to make multi-panel plots for easy comparison. Here is a boxplot that shows the range of log10.mass by taxon.
homerange %>%
ggplot(aes(x=taxon, y=log10.mass, fill=trophic.guild))+
geom_boxplot()+
theme(axis.text.x = element_text(angle = 60, hjust=1))
There are other categorical variables that might be interesting to
overlay. facet_wrap()
makes a ribbon of panels by a
specified categorical variable and allows you to control how you want
them arranged.
homerange %>%
ggplot(aes(x=taxon, y=log10.mass, fill=taxon))+
geom_boxplot()+
facet_wrap(~trophic.guild, ncol=2, labeller=label_both)+
theme(axis.text.x = element_text(angle = 60, hjust=1),
legend.position = "none",
strip.text = element_text(size = 12, face = "bold"))
facet_grid()
allows control over the faceted variable;
it can be arranged in rows or columns. rows~columns.
homerange %>%
ggplot(aes(x=taxon, y=log10.mass))+
geom_boxplot()+
facet_grid(trophic.guild~.)+
theme(axis.text.x = element_text(angle = 60, hjust=1))
homerange %>%
ggplot(aes(x=taxon, y=log10.mass))+
geom_boxplot()+
facet_grid(.~trophic.guild)+
theme(axis.text.x = element_text(angle = 60, hjust=1))
facet_grid()
will also allow the comparison of two
categorical variables, just remember a~b where a is rows and b is
columns.
homerange %>%
ggplot(aes(x=taxon, y=log10.mass))+
geom_boxplot()+
facet_grid(trophic.guild~thermoregulation)+
theme(axis.text.x = element_text(angle = 60, hjust=1))
log10.mass
by thermoregulation
.homerange %>%
ggplot(aes(x=log10.mass))+
geom_density(fill="steelblue", alpha=0.3)+
facet_wrap(~thermoregulation)
log10.mass
by locomotion
.homerange %>%
ggplot(aes(x=log10.hra))+
geom_density(fill="steelblue", alpha=0.3)+
facet_grid(.~locomotion)
Please review the learning goals and be sure to use the code here as a reference when completing the homework.
–>Home