使用R中ggplot的facet_wrap的流动持续时间曲线 [英] Flow duration curve using facet_wrap of ggplot in R?
问题描述
我正在使用 hydroTSM软件包
的 fdc
.我有三个 data.frame
,我想使用 facet_wrap
功能构建 data.frame
的流动持续时间曲线(FDC)> ggplot 将 plots
放在三行
和一列
中.以下将为 DF1
生成 FDC曲线
.
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)
我尝试将 gridExtra包
与 grid.arrange
一起使用,以将三个图强制放在一个图形上.我不仅没有做到这一点,而且不是首选方法.我想使用 ggplot
的 facet_wrap
选项.实际上,使用 DF1
数据错误地绘制了该图.我正在寻找类似以下的内容:
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:
更新:这是基于@Jon Spring的建议.
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())
推荐答案
您可以使用ggplot中的构面执行类似的操作:
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())
如果要将字母注释放在更左端,则可以选择使用 patchwork
包来堆叠和标记图:
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")
这篇关于使用R中ggplot的facet_wrap的流动持续时间曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!