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

问题描述 投票:2回答:2

我正在使用fdchydroTSM package。我有3个data.frame,我想使用data.framefacet_wrap功能构造ggplot的流动持续时间曲线(FDC),以使plotsthree rows中的one column。以下将为FDC curves产生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)

enter image description here

我试图将gridExtra packagegrid.arrange结合使用,以将三个图强制显示在单个图形上。我不仅没有做到这一点,而且不是首选方法。我想使用facet_wrapggplot选项。实际上,该图是使用DF1数据错误绘制的。我正在寻找类似下面的内容:

enter image description here

更新:这是基于@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())

enter image description here

r ggplot2 probability facet-wrap hydrotsm
2个回答
2
投票

您可以使用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())

enter image description here

如果要在最左边放置字母批注,则可以选择使用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")

enter image description here


0
投票

对于示例数据,我们将使用HydroGOF软件包中的EgaEnEstellaQts日常流量数据。从1961年1月1日到1970年12月31日。创建三年的数据进行绘图

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)

0
投票

对于示例数据,我们将使用HydroGOF软件包中的EgaEnEstellaQts日常流量数据。从1961年1月1日到1970年12月31日。创建三年的数据进行绘图

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)
© www.soinside.com 2019 - 2024. All rights reserved.