Code/04_plot_seed_black_white.R

# By grid plot
# Base plot code
# feb2020

#plot theme
source("./R/davidson_2019_theme.r")
# source("./R/figures/study-design-data.R")

# libraries needed
# source("./R/r-packages-needed.R", echo = FALSE)
# source("./R/theme_raw_fig3s.r", echo = FALSE)
# source("./R/davidson_2019_theme.r", echo = FALSE)
require(citr)
require(deSolve)
require(lubridate)
require(jagsUI)
require(cowplot)
require(Matrix)
require(ggthemes)
require(ggplot2)
require(gridExtra)
require(coda)
require(tidyverse)
require(reshape2)
require(knitr)
require(knitcitations)
require(jtools)
require(flextable)
require(rmarkdown)
require(tinytex)
require(citr)
require(stringr)
require(tidyverse)

# jitter everything the same is harrrrrd
location.move <- position_dodge(width = 30)

#import ploting data
plot.dat.all1 <- read_csv("./data/plot-all-data1.csv")
glimpse(plot.dat.all1)

## reduce dataset to only cols of interest
names(plot.dat.all1)

seed_mean_vall_con_year3 <- plot.dat.all1 %>%

  select(cum.seed, Control, Valley, year, Date, Conditions) %>%

  mutate(year = as.factor(year)) %>%

  group_by(Control, Valley, Date, Conditions) %>%
  summarise(N = mean(ifelse(cum.seed > 0, ifelse(cum.seed > 0, log(cum.seed), 0), 0)),
            Rats = factor("ALL"),
            sd.s = sd(ifelse(cum.seed > 0, log(cum.seed), 0), na.rm = TRUE),
            se.s = sd(ifelse(cum.seed > 0, log(cum.seed), 0)) / sqrt(length(cum.seed)) * 1.96,
            lcl.s = mean(ifelse(cum.seed > 0, log(cum.seed), 0)) - (sd(ifelse(cum.seed > 0, log(cum.seed), 0)) / sqrt(length(cum.seed)) *
                                                                      1.96),
            lcl_seed = exp(lcl.s),
            ucl.s = mean(ifelse(cum.seed > 0, log(cum.seed), 0)) + (sd(ifelse(cum.seed > 0, log(cum.seed), 0)) / sqrt(length(cum.seed)) *
                                                                      1.96),
            ucl_seed = exp(ucl.s), mean_seed = exp(N)) %>%
  ungroup()


#data to plot
# seed_mean_vall_con_year3$lcl_seed
# seed_mean_vall_con_year3$ucl_seed
# seed_mean_vall_con_year3$mean_seed


# summaries_Control, valley, year
seed_mean_vall_con_CON_year <- plot.dat.all1 %>%

    select(cum.seed, Control, Valley, Conditions, year, Date) %>%

      mutate(year = as.factor(year),
             Conditions = as.factor(Conditions)) %>%

        group_by(Control, Valley, Date,Conditions) %>%
  summarise(N = mean(ifelse(cum.seed > 0, ifelse(cum.seed > 0, log(cum.seed), 0), 0)),
            # Rats = factor("ALL"),
            sd.s = sd(ifelse(cum.seed > 0, log(cum.seed), 0), na.rm = TRUE),
            se.s = sd(ifelse(cum.seed > 0, log(cum.seed), 0)) / sqrt(length(cum.seed)) * 1.96,
            lcl.s = mean(ifelse(cum.seed > 0, log(cum.seed), 0)) - (sd(ifelse(cum.seed > 0, log(cum.seed), 0)) / sqrt(length(cum.seed)) *
                                                                      1.96),
            lcl_seed = exp(lcl.s),
            ucl.s = mean(ifelse(cum.seed > 0, log(cum.seed), 0)) + (sd(ifelse(cum.seed > 0, log(cum.seed), 0)) / sqrt(length(cum.seed)) *
                                                                      1.96),
            ucl_seed = exp(ucl.s), mean_seed = exp(N)) %>%
  ungroup()

#data to plot
# seed_mean_vall_con_CON_year$lcl_seed
# seed_mean_vall_con_CON_year$ucl_seed
# seed_mean_vall_con_CON_year$mean_seed


#spliting dataset based on these levels
# levels(seed_mean_vall_con_CON_year$Conditions)


## -----------------------dataset of replicates with rats present ------------------
seed_mean_vall_con_COND_year_RATS <- seed_mean_vall_con_CON_year %>%
  filter(Conditions == "rats.present") %>%
  mutate(
    gp.treat = factor(paste(Valley, Control)),
      Rats = factor("RATS", levels = c("ALL,", "RATS","NO_RATS")),
  )

## -----------------------dataset of replicates with rats removed ------------------
seed_mean_vall_con_COND_year_NO_RATS <- seed_mean_vall_con_CON_year %>%

  filter(Conditions == "rats.removed") %>%
  mutate(
    gp.treat = factor(paste(Valley, Control)),
    Rats = factor("NO_RATS", levels = c("ALL,", "RATS","NO_RATS"))
    )

seed_mean_vall_con_COND_year1 <- bind_rows(seed_mean_vall_con_COND_year_NO_RATS,seed_mean_vall_con_COND_year_RATS)





##-------------------plot code all conditions

# names(seed_mean_vall_con_year3)

p1 <- ggplot(data = seed_mean_vall_con_year3, aes(y = mean_seed, x = Date)) +
  geom_point(aes(shape = Valley, col = Control), alpha = 0.7,
             size = 6) +
  geom_errorbar(aes(ymin = lcl.s, ymax = ucl.s, width = 0), lwd = 0.4, col = "black", position = location.move, alpha = 0.7) +
  facet_wrap(~Conditions, ncol = 1)


p1


#
#
# p2 <-
#
#   ggplot(data = seed_mean_vall_con_COND_year1, aes(y = mean_seed, x = Date)) +
#   geom_point(aes(shape = Valley, col = Control), alpha = 0.7,
#              size = 6) +
#   geom_errorbar(aes(ymin = lcl_seed, ymax = ucl_seed, width = 0))
#
# +
#   facet_wrap(~Rats)
#
# p2
#
# #doesnt work!!
# p3 <- p2 +
#   # Remove fill legend and replace the fill legend using the newly created size
#   guides(
#     col = "none",
#     size = guide_legend(override.aes = list(
#       shape = c(15,0),alpha = 1
#     )),
#     shape = guide_legend(override.aes = list(
#       shape = c(24, 21), size = 4
#     )),
#     fill = guide_legend(override.aes = list(
#       col = c("white", "black"),shape = c("square"),
#       size = 4
#     ))
#   )


# p3

# +
# geom_rect(aes(xmin=ymd('2000-01-01'),xmax = ymd('2000-12-31'),ymin = -Inf,ymax = Inf), colour = "grey90", fill = "grey90") +
# geom_rect(aes(xmin=ymd('2002-01-01'),xmax = ymd('2002-12-31'),ymin = -Inf,ymax = Inf), colour = "grey90", fill = "grey90") +
# geom_rect(aes(xmin=ymd('2004-01-01'),xmax = ymd('2004-12-31'),ymin = -Inf,ymax = Inf), colour = "grey90", fill = "grey90") +

#   xlab(expression(paste("Time", "(", italic(t), ")"))) +
#   ylab(expression(paste("Available seed "," ", "(", italic(Seed[jt]), ")"))) +
#   scale_color_manual(name = "Stoat Control",
#                      values = c("black", "white", "white")) +
#
#   scale_shape_manual(name = "Ecosystem",
#                      values = c(24, 21)) +
#
#   scale_size_manual(name = "Rat Control", values = c(2.5, 3, 2.5)) +
#   # manually define the fill colours
#
#   scale_fill_manual(name = "Stoat Control",
#                     values = c("white", "black", "white")) +
# 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 = "none",
  #       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"))

# p.design <- plot.dat.all1 %>%
#   mutate(grid = as.numeric(factor(grid)))
#
# # glimpse(plot.dat.all1)
# # adding NA grid to plot
# bd.row <- p.design[1, ]
# bd.row$grid <- "blank"
# bd.row$grid <- "blank"
# bd.row$grid <- "blank"
# bd.row$grid <- "blank"
# bd.row$Date <- NA
#
# # bind row to plot data
# p.design145 <- rbind(p.design, bd.row)
# # glimpse(p.design145$grid)
#
# # labels
# rat.labs <- c("No", "Reduced")
#
# #re-factoring
# p.design1 <- p.design145  %>%
#   mutate(grid = factor(grid, levels = c(
#     "1", "2", "3", "4", "blank", "5", "6", "7", "8"
#   )),
#   Rats = factor(Rats, labels = rat.labs))
#
# ## ----conf-intervals-above-zero-------------------------------------------
# #making datasest
# p.design2 <- p.design1 %>%
#   group_by(Control, Valley, Date) %>%
#   summarise(N = mean(ifelse(cum.seed > 0, ifelse(cum.seed > 0, log(cum.seed), 0), 0)),
#             Rats = factor("Full", levels = c("Full", "Reduced")),
#             sd.s = sd(ifelse(cum.seed > 0, log(cum.seed), 0), na.rm = TRUE),
#             se.s = sd(ifelse(cum.seed > 0, log(cum.seed), 0)) / sqrt(length(cum.seed)) * 1.96,
#             lcl.s = mean(ifelse(cum.seed > 0, log(cum.seed), 0)) - (sd(ifelse(cum.seed > 0, log(cum.seed), 0)) / sqrt(length(cum.seed)) *
#                                                                       1.96),
#             lcl.slog = exp(lcl.s),
#             ucl.s = mean(ifelse(cum.seed > 0, log(cum.seed), 0)) + (sd(ifelse(cum.seed > 0, log(cum.seed), 0)) / sqrt(length(cum.seed)) *
#                                                                       1.96),
#             ucl.slog = exp(ucl.s)) %>%
#   ungroup() %>%
#   mutate(cum.seed = exp(N),
#          grid = factor(paste(Control, Valley)))
#
# ## ------------------------------------------------------------------------
# # fig.3.plot.seed.sum.final
#
# # plot
# plot.final.f3.1 <- ggplot(p.design2,
#                           aes(
#                             y = cum.seed,
#                             col = Rats,
#                             shape = Valley,
#                             fill = Control,
#                             x = Date
#                           )) +
#   geom_rect(aes(xmin=ymd('2000-01-01'),xmax = ymd('2000-12-31'),ymin = -Inf,ymax = Inf), colour = "grey90", fill = "grey90") +
#   geom_rect(aes(xmin=ymd('2002-01-01'),xmax = ymd('2002-12-31'),ymin = -Inf,ymax = Inf), colour = "grey90", fill = "grey90") +
#   geom_rect(aes(xmin=ymd('2004-01-01'),xmax = ymd('2004-12-31'),ymin = -Inf,ymax = Inf), colour = "grey90", fill = "grey90") +
#
#   geom_errorbar(aes(ymin = lcl.slog, ymax = ucl.slog, width = 0), lwd = 0.4,
#                 col = "black", position = location.move, alpha = 0.7) +
#
#   geom_line(col = "grey50") +
#   geom_point(aes(y = cum.seed,
#                  x = Date
#   ),alpha = 0.7, size = 6, position = location.move, size = 0.95) +
#   xlab(expression(paste("Time", "(", italic(t), ")"))) +
#   ylab(expression(paste("Available seed "," ", "(", italic(Seed[jt]), ")"))) +
#   scale_color_manual(name = "Stoat Control",
#                      values = c("black", "white", "white")) +
#
#   scale_shape_manual(name = "Ecosystem",
#                      values = c(24, 21)) +
#
#   scale_size_manual(name = "Rat Control", values = c(2.5, 3, 2.5)) +
#   # manually define the fill colours
#
#   scale_fill_manual(name = "Stoat Control",
#                     values = c("white", "black", "white")) +
#
#
#
#
#   # Remove fill legend and replace the fill legend using the newly created size
#   guides(
#     col = "none",
#     size = guide_legend(override.aes = list(
#       shape = c(15,0),alpha = 1
#     )),
#     shape = guide_legend(override.aes = list(
#       shape = c(24, 21), size = 4
#     )),
#     fill = guide_legend(override.aes = list(
#       col = c("white", "black"),shape = c("square"),
#       size = 4
#     ))
#   ) +
#
#   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 = "none",
#         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"))
#
# plot.final.f3.1



# # export plot for example vignette
# jpeg("./figs/fig-3.1-study.jpeg")
# fig.2.plot.design
# dev.off()
davan690/beech-publication-wr documentation built on March 29, 2020, 11:09 a.m.