Skip to content

Instantly share code, notes, and snippets.

@lucasnell
Created September 29, 2016 16:25
Show Gist options
  • Save lucasnell/1c454bc2d6ca8d42baa6f5350810acbd to your computer and use it in GitHub Desktop.
Save lucasnell/1c454bc2d6ca8d42baa6f5350810acbd to your computer and use it in GitHub Desktop.
Plot for Bio 152 quiz question
library(ggplot2)
library(dplyr)
library(grid)
sd_before <- 10
sd_after <- 3
mean_before <- 30
mean_after <- 10
df <- data_frame(trait = rep(seq(0, 60, length.out = 100), 2)) %>%
mutate(frequency = c(dnorm(trait[1:100], mean = mean_before, sd = sd_before),
dnorm(trait[101:200], mean = mean_after, sd = sd_after)),
g = factor(c(rep('before', 100), rep('after', 100)),
levels = c('before', 'after')))
p1 <- df %>%
filter(g == 'before') %>%
ggplot(aes(trait, frequency)) +
ggtitle('Before') +
geom_line(size = 1) +
xlab('beak width') +
scale_y_continuous(limits = c(0, 0.04),
breaks = seq(0, 0.04, length.out = 3),
labels = seq(0, 0.5, 0.25)) +
theme_classic() +
theme(legend.position = 'none')
p2 <- df %>%
filter(g == 'after') %>%
ggplot(aes(trait, frequency)) +
ggtitle('After') +
geom_line(size = 1, color = 'gray60') +
xlab('beak width') +
scale_y_continuous(limits = c(0, 0.133),
breaks = seq(0, 0.133, length.out = 3),
labels = seq(0, 0.5, 0.25)) +
theme_classic() +
theme(legend.position = 'none')
grid.newpage()
grid.draw(cbind(ggplotGrob(p1), ggplotGrob(p2), size = "last"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment