R/figures/pe-source-code.R

out.dat1 <- out.full.136 %>%
  filter(month == "Nov") %>%
  mutate(lag.sjt = lag(log.seed),
         lag.N = lag(N),
         lag.R = lag.rat.mna) %>%
  select(-var) %>%
  droplevels()

# for each group I need to ....

#Nov filtered (data)
out.dat1 <- out.full.136 %>%
  filter(month == "Nov") %>%
  mutate(lag.sjt = lag(log.seed),
         lag.N = lag(N),
         lag.R = lag.rat.mna) %>%
  select(-var) %>%
  droplevels()

out.dat2 <-  out.full.136 %>%
  filter(month == "Nov") %>%
  mutate(lag.sjt = lag(log.seed),
         lag.N = lag(N),
         lag.R = lag.rat.mna) %>%
  group_by(Control, Valley, month) %>%
  summarise(
    M.seed = mean(lag.sjt, na.rm = TRUE),
    M.dens = mean(lag.N, na.rm = TRUE),
    M.rat = mean(lag.R, na.rm = TRUE),

    MAX.rat = max(lag.R, na.rm = TRUE),
    MAX.seed = max(lag.sjt, na.rm = TRUE),
    MAX.dens = max(lag.N, na.rm = TRUE),
    MAX.r = max(mean.r, na.rm = TRUE),

    min.seed = 0,
    min.dens = min(lag.N, na.rm = TRUE),
    min.rat = 0,
    min.r = min(mean.r, na.rm = TRUE)) %>%
  ungroup()

# #merge two datasets
dens.plots <- left_join(out.dat2, out.para1, by = c("Control", "Valley", "month"))

# predict estimates for seed lines
# seed.plot1 <- seed.plots %>%
#   mutate(pred.mean = b0 + (b.seed * M.seed) + (b.dens*M.dens) + (b.rat*M.rat),
#          pred.min = b0 + (b.seed * min.seed) + (b.dens*min.dens) + (b.rat*min.rat),
#          pred.max = b0 + (b.seed * MAX.seed) + (b.dens*MAX.dens) + (b.rat*MAX.rat))
dens.plot1 <- dens.plots %>%
  mutate(pred.mean = Intercept + (Seed * M.seed) + (Density*M.dens) + (Rats*M.rat),
         pred.min = Intercept + (Seed * M.seed) + (Density*M.dens) + (Rats*min.rat),
         pred.max = Intercept + (Seed * M.seed) + (Density*M.dens) + (Rats*MAX.rat))

# # r2
# # plot(pred~mean.r, data = seed.Nov.plot1)
#
#
# # data in tidyverse for ggplot2
out.dens2 <- dens.plot1 %>%
  gather(key = pred.fact, value = mean.r, pred.min:pred.max) %>%
  mutate(lag.R = ifelse(pred.fact == "pred.min", min.rat, MAX.rat)) %>%
  select(Valley, Control, mean.r, lag.R, month, pred.fact) %>%
  mutate(lag.R = ifelse(lag.R>0, lag.R, 0.1),
         treat = paste(Valley,Control))

pe.plot <- ggplot(data = out.dat1, aes(y = mean.r, x = lag.R)) +
  #error bars
  geom_errorbar(aes(ymin = lcl.r, ymax = ucl.r),
                lwd = 0.75,
                alpha = 0.2,
                position=position_dodge(width=30),
                width = 0) +
  #background points
  geom_point(aes(colour = Control,
                 shape = Valley,
                 fill = Conditions),
             stroke = 1.001, size = 2,
             alpha = 0.2) +
  geom_line(data = out.dens2,
            aes(y = mean.r, x = lag.R, group = treat),
            size = 0.75,
            alpha = 0.7) +
  geom_point(data = out.dens2, aes(y = mean.r, x = lag.R, shape = Valley, fill = Control, group = treat), size = 5)+
  #Aesthics
  scale_color_manual(name = "Rat removal",
                     values = c("black", "black", "black", "black")) +

  scale_shape_manual(name = "Ecosystem",
                     values = c(24, 21)) +

  scale_fill_manual(name = "Stoat control",
                    values = c("white", "black","white", "black")) +

  guides(fill = "none",
         col = guide_legend(override.aes = list(
           shape = c(15,0), alpha = 0.2
         )),
         shape = guide_legend(override.aes = list(
           shape = c(24, 21), alpha = 1, size = 4
         ))) +
  # xlab(expression(paste("Mouse"," ", "Density"," ","(",italic(N[jt]),")" ))) +
  xlab(expression(atop(paste("Minimum"," ", "number"," "),paste("of", " ", "rats"," ","(",italic(R[jt]),")"))) )+
  # xlab(expression(atop(paste("Minimum"," ", "number"," "),paste("of", " ", "rats"," ","(",italic(R[jt]),")"))) )+
  # xlab(expression(paste("Avaliable seed"," ", "(", italic(Seed[jt]), ")"))) +
  ylab(expression(atop(paste("Rate"," ", "of"," ",
                             "increase"),paste(" ", "of"," ",
                                               "mice"," ","(",italic(r[jt]),")"))) ) +
  geom_hline(yintercept = 0, lty = 2, size = 0.5) +
  #theme
  theme_tufte() +
  theme_bw() +
  theme(strip.background = element_blank(),
        strip.text.y = element_blank(),

        plot.title = element_text(hjust = 0, size=24, family = "Times", color="black", margin = margin(t = 10, b = 10)),
        plot.subtitle=element_text(size=16, face="italic", color="black"),

        legend.position = "right",
        legend.key = element_blank(),
        legend.background = element_rect(fill="white", size=1),
        legend.key.size=unit(1,"cm"),
        legend.text = element_text(colour = "black", size =16, family = "Times"),
        legend.title = element_text(colour = "black", size =16, family = "Times"),

        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.spacing = unit(2, "lines"),
        panel.border = element_blank(),

        axis.title.y = element_text(colour = "black",size =20, family = "Times", angle = 90),
        axis.title.x = element_text(colour = "black", size =20, family = "Times"),
        axis.text.y=element_text(colour = "black",size = 20, family = "Times"),
        axis.text.x = element_text(colour = "black", size =20, family = "Times"),

        axis.ticks.x = element_line(size = 1),
        axis.ticks.y = element_line(size = 1),
        axis.line.x = element_line(size = 1),
        axis.line.y = element_line(size = 1),

        strip.text = element_text(face="bold",colour = "black",size =14, family = "Times"))

pe.plot

# Save plot
# export plot for example vignette
# png("C://Code/beech-publication-wr/figs/fig-61.png")
# pe.plot
# dev.off()
davan690/beech-publication-wr documentation built on March 29, 2020, 11:09 a.m.