简体   繁体   English

在 R 中使用 ggplot 的 facet_wrap 的流动持续时间曲线?

[英]Flow duration curve using facet_wrap of ggplot in R?

I am using fdc of hydroTSM package .我正在使用hydroTSM package fdc 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 .我有三个data.frame ,我想构建流持续时间曲线(FDC)的的data.frame使用facet_wrap的功能ggplot具有plotsthree rowsone column the following will produce FDC curves for DF1 .下面将生成DF1 FDC curves

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.我尝试将gridExtra packagegrid.arrange一起使用,以将三个图强制放在一个图形上。 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 .我会想使用的facet_wrap的选项ggplot In fact the figure is wrongly drawn by using DF1 data.事实上,该图是使用DF1数据错误绘制的。 i am looking for something like below:我正在寻找类似下面的东西:

在此处输入图片说明

Update: This is based on @Jon Spring suggestions.更新:这是基于@Jon Spring 的建议。

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())

在此处输入图片说明

You could do something like this with facets in ggplot:你可以用 ggplot 中的 facet 做这样的事情:

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:如果您希望将字母注释放在更靠左的位置,您可以交替使用patchwork包来堆叠和标记图:

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")

在此处输入图片说明

For sample data, we'll use the EgaEnEstellaQts daily flow data from the HydroGOF package.对于示例数据,我们将使用 HydroGOF 包中的 EgaEnEstellaQts 每日流量数据。 This as flows from 01/Jan/1961 to 31/Dec/1970.这是从 1961 年 1 月 1 日到 1970 年 12 月 31 日的流向。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM