## ── Attaching packages ──────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ tibble  3.0.3     ✓ dplyr   1.0.2
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.5.0
## ✓ purrr   0.3.4
## ── Conflicts ─────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## Parsed with column specification:
## cols(
##   record_id = col_double(),
##   month = col_double(),
##   day = col_double(),
##   year = col_double(),
##   plot_id = col_double(),
##   species_id = col_character(),
##   sex = col_character(),
##   hindfoot_length = col_double(),
##   weight = col_double(),
##   genus = col_character(),
##   species = col_character(),
##   taxa = col_character(),
##   plot_type = col_character()
## )

Boxplots

ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
  geom_boxplot()

Add points

ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
  geom_boxplot(alpha = 0) +
  geom_jitter(alpha = 0.3, color = "tomato")

Put the boxes in front of the points…

ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
  geom_jitter(alpha = 0.3, color = "tomato") +
  geom_boxplot(alpha = 0)

Challenges

Try a violin plot

ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
  geom_jitter(alpha = 0.3, color = "tomato") +
  geom_violin(alpha = 0)

Without the points…

ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
  geom_violin(alpha = 0)

Change scale

ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
  geom_violin(alpha = 0) + 
  scale_y_log10()

Plot hindfoot length

ggplot(data = surveys_complete, mapping = aes(x = species_id, y = hindfoot_length)) +
  geom_jitter(alpha = 0.3, aes(color = plot_id)) +
  geom_boxplot(alpha = 0) 

Change plot_id from integer to factor so that it’s not a continuous variable

surveys_complete[,'plot_id'] <- factor(surveys_complete[,'plot_id'])
class(surveys_complete$plot_id)
## [1] "factor"

Plot again - not working ?? need to fix

#ggplot(data = surveys_complete, mapping = aes(x = species_id, y = hindfoot_length, color = plot_id)) +
#  geom_jitter() +
#  geom_boxplot() 

Hmm for some reason it doesn’t like the color line

Plotting Time Series Data

Counts per year per genus

yearly_counts <- surveys_complete %>%
  count(year, genus)
ggplot(data = yearly_counts, aes(x = year, y = n)) +
  geom_line()

ggplot(data = yearly_counts, aes(x = year, y = n, color = genus)) +
  geom_line()

Integrating pipe operator with ggplot

yearly_counts %>% 
  ggplot(mapping = aes(x = year, y = n, color = genus)) +
  geom_line()

Link data manipulation and visualization

yearly_counts_graph <- surveys_complete %>%
  count(year, genus) %>% 
  ggplot(mapping = aes(x = year, y = n, color = genus)) +
  geom_line()

yearly_counts_graph

Faceting

ggplot(data = yearly_counts, aes(x = year, y = n)) +
  geom_line() +
  facet_wrap(facets = vars(genus))

Split the sexes

yearly_sex_counts <- surveys_complete %>%
  count(year, genus, sex)
ggplot(data = yearly_sex_counts, mapping = aes(x = year, y = n, color = sex)) +
  geom_line() +
  facet_wrap(facets =  vars(genus))

Split sexes and genera

ggplot(data = yearly_sex_counts, 
       mapping = aes(x = year, y = n, color = sex)) +
  geom_line() +
  facet_grid(rows = vars(sex), cols =  vars(genus))

One column, facet by rows

ggplot(data = yearly_sex_counts, 
       mapping = aes(x = year, y = n, color = sex)) +
  geom_line() +
  facet_grid(rows = vars(genus))

One row, facet by column

ggplot(data = yearly_sex_counts, 
       mapping = aes(x = year, y = n, color = sex)) +
  geom_line() +
  facet_grid(cols = vars(genus))

ggplot2 themes

https://ggplot2.tidyverse.org/reference/ggtheme.html

library(ggthemes)

Change the background to white

ggplot(data = yearly_sex_counts, 
       mapping = aes(x = year, y = n, color = sex)) +
  geom_line() +
  facet_wrap(vars(genus)) +
  theme_bw()

Challenge

Create a plot that depicts how the average weight of each species changes through the years

yearly_weights_graph <- surveys_complete %>%
  group_by(year, genus) %>% 
  summarize(mean_weight = mean(weight)) %>% 
  ggplot(mapping = aes(x = year, y = mean_weight, color = genus)) +
  geom_line()
## `summarise()` regrouping output by 'year' (override with `.groups` argument)
yearly_weights_graph 

Customization

Add axis titles

ggplot(data = yearly_sex_counts, aes(x = year, y = n, color = sex)) +
  geom_line() +
  facet_wrap(vars(genus)) +
  labs(title = "Observed genera through time",
       x = "Year of observation",
       y = "Number of individuals") +
  theme_bw()

Increase axis titles font size

ggplot(data = yearly_sex_counts, mapping = aes(x = year, y = n, color = sex)) +
  geom_line() +
  facet_wrap(vars(genus)) +
  labs(title = "Observed genera through time",
       x = "Year of observation",
       y = "Number of individuals") +
  theme_bw() +
  theme(text=element_text(size = 16))

Change orientation of the labels & italicize

ggplot(data = yearly_sex_counts, mapping = aes(x = year, y = n, color = sex)) +
  geom_line() +
  facet_wrap(vars(genus)) +
  labs(title = "Observed genera through time",
       x = "Year of observation",
       y = "Number of individuals") +
  theme_bw() +
  theme(axis.text.x = element_text(colour = "grey20", size = 12, angle = 90, hjust = 0.5, vjust = 0.5),
        axis.text.y = element_text(colour = "grey20", size = 12),
        strip.text = element_text(face = "italic"),
        text = element_text(size = 16))

Save your changes as a theme

grey_theme <- theme(axis.text.x = element_text(colour="grey20", size = 12, 
                                               angle = 90, hjust = 0.5, 
                                               vjust = 0.5),
                    axis.text.y = element_text(colour = "grey20", size = 12),
                    text=element_text(size = 16))

ggplot(surveys_complete, aes(x = species_id, y = hindfoot_length)) +
  geom_boxplot() +
  grey_theme

Challenge

Change one of the graphs to make it pretty

We’ll look at species counts in the different plot types

class(surveys_complete$plot_type)
## [1] "character"
surveys_complete$plot_type<-as.factor(surveys_complete$plot_type)
levels(surveys_complete$plot_type)
## [1] "Control"                   "Long-term Krat Exclosure" 
## [3] "Rodent Exclosure"          "Short-term Krat Exclosure"
## [5] "Spectab exclosure"
yearly_plot_types<-surveys_complete %>% 
  count(year, genus, plot_type)

head(yearly_plot_types)
## # A tibble: 6 x 4
##    year genus       plot_type                     n
##   <dbl> <chr>       <fct>                     <int>
## 1  1977 Chaetodipus Control                       2
## 2  1977 Chaetodipus Long-term Krat Exclosure      1
## 3  1977 Dipodomys   Control                     105
## 4  1977 Dipodomys   Long-term Krat Exclosure     15
## 5  1977 Dipodomys   Rodent Exclosure             25
## 6  1977 Dipodomys   Short-term Krat Exclosure    54
ggplot(data = yearly_plot_types, mapping = aes(x = year, y = n, color = plot_type)) +
  geom_line(size = 1) + # thicker lines
  facet_wrap(vars(genus)) +
  labs(title = "Observed genera through time",
       x = "Year of observation",
       y = "Number of individuals") +
  theme_bw() +
  theme(axis.text.x = element_text(colour = "grey20", size = 12, angle = 90, hjust = 0.5, vjust = 0.5),
        axis.text.y = element_text(colour = "grey20", size = 12),
        strip.text = element_text(face = "italic"),
        text = element_text(size = 16)) + 
  scale_fill_discrete(name = "Plot Type") # why isn't this working? 

Arranging and exporting plots

Put multiple plots together

## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
spp_weight_boxplot <- ggplot(data = surveys_complete, 
                             aes(x = species_id, y = weight)) +
  geom_boxplot() +
  labs(x = "Species", 
       y = expression(log[10](Weight))) +
  scale_y_log10() + 
  labs()

spp_count_plot <- ggplot(data = yearly_counts, 
                         aes(x = year, y = n, color = genus)) +
  geom_line() + 
  labs(x = "Year", y = "Abundance")

grid.arrange(spp_weight_boxplot, spp_count_plot, ncol = 2, widths = c(4, 6))

Export

my_plot <- ggplot(data = yearly_sex_counts, 
                  aes(x = year, y = n, color = sex)) +
  geom_line() +
  facet_wrap(vars(genus)) +
  labs(title = "Observed genera through time",
       x = "Year of observation",
       y = "Number of individuals") +
  theme_bw() +
  theme(axis.text.x = element_text(colour = "grey20", size = 12, angle = 90,
                                   hjust = 0.5, vjust = 0.5),
        axis.text.y = element_text(colour = "grey20", size = 12),
        text = element_text(size = 16))

ggsave("name_of_file.png", my_plot, width = 15, height = 10)

This also works for grid.arrange() plots

combo_plot <- grid.arrange(spp_weight_boxplot, spp_count_plot, ncol = 2, 
                           widths = c(4, 6))

ggsave("combo_plot_abun_weight.png", combo_plot, width = 10, dpi = 300)
## Saving 10 x 5 in image