Author:

| Published:

| Updated:


You can see this figure in my previous post as well. In this post I have taken a different approach of loading data in R. You can manually enter data in R using the base R package which I demonstrated in this post. The code below should work without modification and generate figure at the end of the post.

# House Keeping:
rm(list = ls()) # Clear environment
library(ggplot2) # Plotting
library(dplyr) # Data Management

# Data Management:
a = "
  Year pp_grow
  1986	988.06
  1987	660.65
  1988	494.03
  1989	823.98
  1990	1166.62
  1993	794.77
  1994	651.51
  1995	806.96
  1996	894.59
  1997	303.28
  1998	525.53
  1999	454.51
  2000	502.67
  2001	519.23
  2003	452.73
  2005	459.03
  2006	472.95
  2007	931.04
  2008	873.81
  2010	387.10
  2011	440.69
  2012	349.76
  2013	768.60
  2014	868.17
  2015	1203.00
  2016	763.97
  2017	851.65
"
my.data = read.table(text = a, 
                     header = TRUE)

pp.data = my.data %>% 
  group_by(Year) %>% 
  summarise(pp_grow = mean(pp_grow, na.rm = T),
            gsr.p50 = pp_grow + pp_grow*50/100,
            gsr.s50 = pp_grow - pp_grow*50/100)

# Color Designation:
cols = c("GSR + 50%" = "red", 
         "GSR" = "Purple", 
         "GSR - 50%" = "green",
         "Mean Line" = "orange")

# Plotting:
ggplot(pp.data,aes(x = Year,
                   y = pp_grow,
                   color = "GSR")) +
  
  # Rainfall
  geom_line(size = 0.75) + 
  xlim(1985, 2017) +
  ylim(0, 2000) + 
  ylab("Growing Season (Apr.-Sept.) Rainfall (mm)") +
  xlab("Year") +
  
  # Theme:
  theme(plot.background = element_rect(fill = "white", 
                                       color = "black",
                                       size = 0),
        panel.background = element_rect(fill = "white", 
                                        color = "black",
                                        size = 0),
        panel.grid.major.x = element_line(color = "lightgrey",
                                          linetype = 2,
                                          size = 0),
        panel.grid.minor.x = element_line(color = "lightgrey",
                                          linetype = 2,
                                          size = 0),
        panel.grid.major.y = element_line(color = "grey",
                                          linetype = 2,
                                          size = 0.25),
        panel.grid.minor.y = element_line(color = "grey",
                                          linetype = 2,
                                          size = 0.25),
        axis.line.x.top = element_line(color = "white",
                                       linetype = 2,
                                       size = 0),
        axis.line.y.right = element_line(color = "white",
                                         linetype = 2,
                                         size = 0),
        axis.line.x.bottom = element_line(color = "black",
                                          linetype = 1,
                                          size = 0.75),
        axis.line.y.left = element_line(color = "black",
                                        linetype = 1,
                                        size = 0.75)) +
  
  # + 50% Rainfll
  geom_line(data = pp.data, 
            aes(y = gsr.p50,
                color = "GSR + 50%"),
            size = 0.75,
            linetype = 2) +
  
  # Mean Line:
  geom_line(data = NULL, 
            aes(y = (meanline = mean(pp_grow)),
                color = "Mean Line"),
            size = 0.5,
            linetype = 2) + 
  
  # - 50% Rainfll
  geom_line(data = pp.data, 
            aes(y = gsr.s50,
                color = "GSR - 50%"),
            size = 0.75,
            linetype = 2) +
  
  # Values of Rainfall:
  geom_text(aes(label = round(pp_grow, 2)),
            color = "black",
            size = 2.5,
            hjust = "outward",
            angle = 90) +
  
  # Adjust Legend:
  labs(color = "GS Rainfalls:") +
  scale_color_manual(values = cols)

#Save Plot: This image will be saved in default working directory, unless you set your own. Image should look like image below:
#ggsave("rainfall.jpeg")

Discover more from Dr. Bijesh Mishra

Subscribe to get the latest posts sent to your email.



Discover more from Bijesh Mishra, Ph.D.

Subscribe now to keep reading and get access to the full archive.

Continue Reading