Skip to content

Instantly share code, notes, and snippets.

@slarge
Created January 22, 2020 21:02
Show Gist options
  • Save slarge/093187455341aa208eed1aabef908e6a to your computer and use it in GitHub Desktop.
Save slarge/093187455341aa208eed1aabef908e6a to your computer and use it in GitHub Desktop.
condition_plots.R
library(ggplot2)
library(dplyr)
library(patchwork)
library(forcats)
annualCondition <- read.csv("https://raw.githubusercontent.com/Laurels1/Condition/master/data/AnnualRelCond2018_GB.csv",
stringsAsFactors = FALSE)
speciesNames <- annualCondition %>%
dplyr::filter(sex == "F") %>%
group_by(Species) %>%
mutate(scaleCond = scale(MeanCond, scale = TRUE, center = TRUE))
xs = quantile(speciesNames$scaleCond, seq(0,1, length.out = 6), na.rm = TRUE)
speciesNames <- speciesNames %>%
mutate(category = cut(scaleCond, breaks = xs, labels = c( "Poor Condition",
"Bad",
"Neutral",
"Good",
"Good Condition")))
sortNames <- speciesNames %>%
filter(YEAR <= 2014) %>%
group_by(Species) %>%
summarize(total = sum(scaleCond)) %>%
arrange(total) %>%
mutate(Species = factor(Species, levels = unique(Species))) %>%
pull(Species)
speciesNames$Species <- factor(speciesNames$Species, levels = sortNames)
p1 <- ggplot(speciesNames, aes(x = YEAR, y = forcats::fct_rev(Species), fill = category)) +
geom_tile() +
coord_equal() +
scale_fill_brewer(palette = "RdBu", na.value = "black") +
theme_bw() +
theme(legend.position="bottom")
p2 <- ggplot(speciesNames, aes(x = YEAR, y = forcats::fct_rev(Species), fill = scaleCond)) +
geom_tile() +
coord_equal() +
scale_fill_viridis_c() +
theme_bw() +
theme(legend.position = "bottom")
p1 + p2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment