Again, consider the fef
dataset
library (tidyverse)
fef <- read_csv ("../labs/datasets/FEF_trees.csv" )
fef
# A tibble: 88 × 18
watershed year plot species dbh_in height_ft stem_green_kg top_green_kg
<dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
1 3 1991 29 Acer rubrum 6 48 92.2 13.1
2 3 1991 33 Acer rubrum 6.9 48 102. 23.1
3 3 1991 35 Acer rubrum 6.4 48 124. 8.7
4 3 1991 39 Acer rubrum 6.5 49 91.7 39
5 3 1991 44 Acer rubrum 7.2 51 186. 8.9
6 3 1992 26 Acer rubrum 3.1 40 20.8 0.9
7 3 1992 26 Acer rubrum 2 30.5 5.6 0.9
8 3 1992 26 Acer rubrum 4.1 50 54.1 8.6
9 3 1992 48 Acer rubrum 2.4 28 10.2 0.7
10 3 1992 48 Acer rubrum 2.7 40.4 20.2 5
# ℹ 78 more rows
# ℹ 10 more variables: smbranch_green_kg <dbl>, lgbranch_green_kg <dbl>,
# allwoody_green_kg <dbl>, leaves_green_kg <dbl>, stem_dry_kg <dbl>,
# top_dry_kg <dbl>, smbranch_dry_kg <dbl>, lgbranch_dry_kg <dbl>,
# allwoody_dry_kg <dbl>, leaves_dry_kg <dbl>
Geometry: bar
ggplot (data = fef) +
geom_bar (mapping = aes (x = species))
Geometry: bar
ggplot (data = fef) +
geom_bar (mapping = aes (x = species))
Geometry: histogram
ggplot (data = fef) +
geom_histogram (mapping = aes (x = dbh_in))
Geometry: histogram
ggplot (data = fef) +
geom_histogram (mapping = aes (x = dbh_in),
bins = 10 )
Let’s create a plot
First, our canvas:
Let’s create a plot
Then, we specify the data:
Let’s create a plot
Then we specify some aesthetic mappings
ggplot (data = fef, mapping = aes (x = dbh_in, y = height_ft))
Let’s create a plot
We’ve now specified that we will map these aesthetics to “points”.
ggplot (data = fef, mapping = aes (x = dbh_in, y = height_ft)) +
geom_point ()
Let’s create a plot
You can also specify the aesthetic mapping in the geometry layer:
ggplot (data = fef) +
geom_point (mapping = aes (x = dbh_in, y = height_ft))
Let’s create a plot
We can look at a third variable by adding another aesthetic mapping:
ggplot (data = fef) +
geom_point (mapping = aes (x = dbh_in,
y = height_ft,
color = species))
Let’s create a plot
Color scales are different for continuous vs discrete data:
ggplot (data = fef) +
geom_point (mapping = aes (x = dbh_in,
y = height_ft,
color = stem_dry_kg))
Let’s create a plot
Rather than mapping stem_dry_kg
to color, we could map it to size:
ggplot (data = fef) +
geom_point (mapping = aes (x = dbh_in,
y = height_ft,
size = stem_dry_kg))
Let’s create a plot
This is different than setting size outside of the aesthetics:
ggplot (data = fef) +
geom_point (mapping = aes (x = dbh_in,
y = height_ft),
size = 5 )
Let’s create a plot
Let’s make this plot beautiful:
ggplot (data = fef) +
geom_point (mapping = aes (x = dbh_in,
y = height_ft,
size = stem_dry_kg))
Let’s create a plot
Let’s make this plot beautiful:
ggplot (data = fef) +
geom_point (mapping = aes (x = dbh_in,
y = height_ft,
color = stem_dry_kg))
Let’s create a plot
Let’s make this plot beautiful:
ggplot (data = fef) +
geom_point (mapping = aes (x = dbh_in,
y = height_ft,
color = stem_dry_kg)) +
scale_colour_distiller (type = "seq" , palette = 3 )
Let’s create a plot
Let’s make this plot beautiful:
ggplot (data = fef) +
geom_point (mapping = aes (x = dbh_in,
y = height_ft,
color = stem_dry_kg),
size = 2.5 ) +
scale_colour_distiller (type = "seq" , palette = 3 ) +
labs (x = "DBH (inches)" ,
y = "Height (feet)" ,
fill = "Dry Stem \n Weight (kg)" ,
title = "DBH, Height, and Stem weight \n in the Fernow Experimental Forest" )
Let’s create a plot
Let’s make this plot beautiful:
ggplot (data = fef) +
geom_point (mapping = aes (x = dbh_in,
y = height_ft,
color = stem_dry_kg),
size = 2.5 ,
shape = 21 ) +
scale_colour_distiller (type = "seq" , palette = 3 ) +
labs (x = "DBH (inches)" ,
y = "Height (feet)" ,
fill = "Dry Stem \n Weight (kg)" ,
title = "DBH, Height, and Stem weight \n in the Fernow Experimental Forest" )
Let’s create a plot
Let’s make this plot beautiful:
ggplot (data = fef) +
geom_point (mapping = aes (x = dbh_in,
y = height_ft,
fill = stem_dry_kg),
size = 2.5 ,
shape = 21 ) +
scale_fill_distiller (type = "seq" , palette = 3 ) +
labs (x = "DBH (inches)" ,
y = "Height (feet)" ,
fill = "Dry Stem \n Weight (kg)" ,
title = "DBH, Height, and Stem weight \n in the Fernow Experimental Forest" )
Let’s create a plot
Let’s make this plot beautiful:
ggplot (data = fef) +
geom_point (mapping = aes (x = dbh_in,
y = height_ft,
fill = stem_dry_kg),
size = 2.5 ,
shape = 21 ) +
scale_fill_distiller (type = "seq" , palette = 3 ) +
labs (x = "DBH (inches)" ,
y = "Height (feet)" ,
fill = "Dry Stem \n Weight (kg)" ,
title = "DBH, Height, and Stem weight \n in the Fernow Experimental Forest" ) +
theme_bw ()
Let’s create a plot
Let’s make this plot beautiful:
ggplot (data = fef) +
geom_point (mapping = aes (x = dbh_in,
y = height_ft,
fill = stem_dry_kg),
size = 2.5 ,
shape = 21 ) +
scale_fill_distiller (type = "seq" , palette = 3 ) +
labs (x = "DBH (inches)" ,
y = "Height (feet)" ,
fill = "Dry Stem \n Weight (kg)" ,
title = "DBH, Height, and Stem weight \n in the Fernow Experimental Forest" ) +
theme_bw () +
theme (plot.title = element_text (hjust = 0.5 ))
Next time
More plotting with ggplot2
!
more details on histograms and bar plots
careful considerations when making plots
spatial plotting