species | 2007 | 2008 | 2009 |
---|---|---|---|
Adelie | 3750 | NA | NA |
Adelie | 3800 | NA | NA |
Adelie | 3250 | NA | NA |
Adelie | NA | NA | NA |
Adelie | 3450 | NA | NA |
Adelie | 3650 | NA | NA |
Adelie | 3625 | NA | NA |
Adelie | 4675 | NA | NA |
Adelie | 3475 | NA | NA |
Adelie | 4250 | NA | NA |
After today’s lecture, you’ll be able to:
gglot2
is an R package for creating graphics.
Created by Hadley Wickham and is considered to be part of the tidyverse
.
Compose graphs by combining independent components: versatile!
If you learn the grammar then you will end up creating better graphics in less time.
Tallian, A., Mattisson, J., Samelius, G., Odden, J., Mishra, C., Linnell, J.D., Lkhagvajav, P. and Johansson, O., 2023. Wild versus domestic prey: Variation in the kill-site behavior of two large felids. Global Ecology and Conservation, e02650.
Semper-Pascual, A., Sheil, D., Beaudrot, L. et al. Occurrence dynamics of mammals in protected tropical forests respond to human presence and activities. Nat Ecol Evol 7, 1092-1103 (2023). https://doi.org/10.1038/s41559-023-02060-6
species | 2007 | 2008 | 2009 |
---|---|---|---|
Adelie | 3750 | NA | NA |
Adelie | 3800 | NA | NA |
Adelie | 3250 | NA | NA |
Adelie | NA | NA | NA |
Adelie | 3450 | NA | NA |
Adelie | 3650 | NA | NA |
Adelie | 3625 | NA | NA |
Adelie | 4675 | NA | NA |
Adelie | 3475 | NA | NA |
Adelie | 4250 | NA | NA |
species | year | island | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | sex |
---|---|---|---|---|---|---|---|
Adelie | 2007 | Torgersen | 39.1 | 18.7 | 181 | 3750 | male |
Adelie | 2007 | Torgersen | 39.5 | 17.4 | 186 | 3800 | female |
Adelie | 2007 | Torgersen | 40.3 | 18.0 | 195 | 3250 | female |
Adelie | 2007 | Torgersen | NA | NA | NA | NA | NA |
Adelie | 2007 | Torgersen | 36.7 | 19.3 | 193 | 3450 | female |
Adelie | 2007 | Torgersen | 39.3 | 20.6 | 190 | 3650 | male |
Adelie | 2007 | Torgersen | 38.9 | 17.8 | 181 | 3625 | female |
Adelie | 2007 | Torgersen | 39.2 | 19.6 | 195 | 4675 | male |
Adelie | 2007 | Torgersen | 34.1 | 18.1 | 193 | 3475 | NA |
Adelie | 2007 | Torgersen | 42.0 | 20.2 | 190 | 4250 | NA |
Long format data.
Each row is an observation point and each column is a variable.
Data wrangle BEFORE you graph: tidyr::pivot_longer()
ggplot()
: graphing space.
data
aes()
ggplot(data = data) +
geom_*(aes(x = x,
y = y,
color = color, # points, lines, error bars
shape = shape, # see pch numbers
fill = fill), # bars, columns, boxplots, violins
alpha=0.3, # transparency
shape = pch, # change the point shape; this is a number or vector of numbers
position = position_dodge() # bar plots are not stacked
)
color
, fill
, shape
(pch) take column names.aes()
: color
, fill
, shape
, alpha
(transparency, 0-1), position
, size
, or linewidth
.xlab('x-axis title')
or ylab('y-axis title')
or ggtitle('title)
labs(title, subtitle, caption, alt)
x_lims(c(0,1))
scale_*()
functions can modify:
Position via scale_x_*()
or scale_y_*()
Colors via scale_color_*()
and scale_fill_*()
Transparency via scale_alpha_*()
Sizes via scale_size_*()
Shapes via scale_shape_*()
*
can take the following forms:
Available packages with predefined palettes:
This is a comprehensive list of color palettes in r curated by Emil Hvitfeldt.
Generally, there are 3 types of palettes:
facet_wrap()
and facet_grid()
.Modifies the overall visual defaults of a plot.
theme()
and theme_()
.
theme_
will have predefined themes.theme
will help you customize and personalize the overall look of your plot.theme
and then customize it with theme_
.theme()
will include element_
functions to modify different areas.
Predefined ggplot2 themes: theme_classic()
, theme_gray()
, theme_bw()
, theme_linedraw()
, theme_light()
, theme_dark()
, theme_minimal()
, theme_void()
ggplot(penguins) +
geom_point(aes(x = body_mass_g,
y = flipper_length_mm,
color=island))+
theme(plot.background = element_rect(colour = 'green', fill = 'gray80'),
panel.background = element_rect(colour = 'orange', size = 3, fill = 'pink'),
panel.grid.major = element_line(color = 'blue', size = 2),
legend.position = 'bottom',
axis.title = element_text(size = 20))
This is my favorite theme cheatsheet
ggplot2
geomsGuest lecture for Data Management and Analyses for Environmental Science - March 2024