Flow duration curve using facet_wrap of ggplot in R?

混江龙づ霸主 提交于 2021-02-19 05:27:05

问题


I am using fdc of hydroTSM package. I have three data.frame and i would like to construct Flow duration curves (FDC) of the data.frame using facet_wrap functionality of ggplot to have the plots in three rows and one column. the following will produce FDC curves for DF1.

library(tidyverse)
library(hydroTSM)
library(gridExtra)

DF1 = data.frame(Ob = runif(1000,0,500), A = runif(1000,0,700), B = runif(1000,2,800))
DF2 = data.frame(Ob = runif(1000,0,500), A = runif(1000,0,700), B = runif(1000,2,800))
DF3 = data.frame(Ob = runif(1000,0,500), A = runif(1000,0,700), B = runif(1000,2,800))

fdc(DF1, plot = TRUE)

I tried to use gridExtra package with grid.arrange to force the three plots on a single figure. I not only failed to do it but it is not the preferred method. I would want to used the facet_wrap options of the ggplot. In fact the figure is wrongly drawn by using DF1 data. i am looking for something like below:

Update: This is based on @Jon Spring suggestions.

graphics.off()
rm(list = ls())

library(tidyverse)
library(hydroTSM)
library(gridExtra)

DF1 = data.frame(Ob = runif(800,0,500), M1= runif(800,0,700), M2 = runif(800,2,800), df = rep("Upstream", 800))
DF2 = data.frame(Ob = runif(1000,0,500), M1 = runif(1000,0,700), M2 = runif(1000,2,800), df = rep("Midstream", 1000))
DF3 = data.frame(Ob = runif(1000,0,500), M1 = runif(1000,0,700), M2 = runif(1000,2,800), df = rep("Downstream", 1000))

# combine data into one table with id column for the source
 bind_rows(DF1, DF2, DF3) %>% 
   # reshape into longer format
  pivot_longer(-df, names_to = "src", values_to = "flow") %>%
  arrange(-flow) %>%
  group_by(df, src) %>%
  mutate(flow_pct = 1 - percent_rank(flow)) %>%
  ungroup() %>%

  ggplot(aes(flow_pct, flow, color = src)) +
  geom_line() +
  theme_light() +
  facet_wrap(~df, ncol = 1) +
  labs(x = "% Time flow equalled or exceeded",
       y = "Q, [m3/s]") +
  theme(strip.text = element_text(hjust = 0, color = "black"),
        strip.background = element_blank())


回答1:


You could do something like this with facets in ggplot:

library(tidyverse)
# combine data into one table with id column for the source
bind_rows(DF1, DF2, DF3, .id = "df") %>% 
  mutate(df = LETTERS[as.numeric(df)]) %>%
  # reshape into longer format
  pivot_longer(-df, names_to = "src", values_to = "flow") %>%
  arrange(-flow) %>%
  group_by(df, src) %>%
  mutate(flow_pct = 1 - percent_rank(flow)) %>%
  ungroup() %>%

  ggplot(aes(flow_pct, flow, color = src)) +
  geom_line() +
  theme_light() +
  facet_wrap(~df, ncol = 1) +
  labs(x = "% Time flow equalled or exceeded",
       y = "Q, [m3/s]") +
  theme(strip.text = element_text(hjust = 0, color = "black"),
        strip.background = element_blank())

If you want the letter annotations placed farther left, you could alternately use the patchwork package to stack and label the plots:

library(tidyverse)
library(patchwork)

flow_plot <- function(df) {
  df %>% 
  pivot_longer(everything(), names_to = "src", values_to = "flow") %>%
  arrange(-flow) %>%
  group_by(src) %>%
  mutate(flow_pct = 1 - percent_rank(flow)) %>%
  ungroup() %>%

  ggplot(aes(flow_pct, flow, color = src)) +
  geom_line() +
  theme_light() +
  guides(color = guide_legend()) +
  labs(x = "% Time flow equalled or exceeded",
       y = "Q, [m3/s]") +
    theme(legend.position = c(0.85,0.6))
}


flow_plot(DF1) /
  flow_plot(DF2) /
  flow_plot(DF3) +
  plot_annotation(tag_levels = "A")




回答2:


For sample data, we'll use the EgaEnEstellaQts daily flow data from the HydroGOF package. This as flows from 01/Jan/1961 to 31/Dec/1970. Create three years of data to plot

library(hydroGOF)
library(gridExtra)
library(tidyverse)

Q1 <- window(EgaEnEstellaQts, start=as.Date('1961-01-01'), end=as.Date('1961-12-31'))
Q2 <- window(EgaEnEstellaQts, start=as.Date('1963-01-01'), end=as.Date('1963-12-31'))
Q3 <- window(EgaEnEstellaQts, start=as.Date('1965-01-01'),  end=as.Date('1965-12-31'))


# Because these objects are all the same length, we can put them in one data frame

flow_df <- tibble(Q1 = coredata(Q1), Q2 = coredata(Q2), Q3 = coredata(Q3))

# Add percent ranks which we'll use to plot the fdc

p1 <- flow_df %>% 
  gather(key = period, value = flow)  %>% 
  group_by(period) %>% 
  mutate(rank = 1 - percent_rank(flow)) %>% 
  ggplot(aes(x = rank, y = flow, colour = period)) +
  geom_line() +
  scale_y_continuous(name = 'Discharge', trans = 'log10') +
  scale_x_continuous(name = 'Percentage of time flow is exceeded', breaks = seq(0,1,0.25), labels = c('0', '25%', '50%', '75%', '100%')) +
  labs(subtitle = 'A')


#Make the other graphs as required (just place holders here)    

p2 <- p1 + labs(subtitle = 'B')
p3 <- p1 + labs(subtitle = 'C')

# Arrange with grid arrange      
grid.arrange(p1, p2, p3)


来源:https://stackoverflow.com/questions/59914776/flow-duration-curve-using-facet-wrap-of-ggplot-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!