I'm hoping to find better ways to turn a "tidy" data frame like this:
Into an "exceedance plot", or "exceedence plot" (commonly spelled both ways in water resources applications) like below, which just ranks/orders a variable along the x-axis.
Here's the lengthy way I do it now:
(sample data):
library(tidyverse)
timestep <- c("a", "b", "c", "a", "b", "c", "a", "b", "c")
var <- c("x", "x", "x", "y", "y", "y", "z", "z", "z")
taf <- c(18,1,5,23,12,67,7,30,2)
df <- data.frame(timestep, var, taf)
Build a new data frame (which I think I need?):
df_a <- df %>% filter(var == "x") %>% arrange(desc(taf))
df_b <- df %>% filter(var == "y") %>% arrange(desc(taf))
df_c <- df %>% filter(var == "z") %>% arrange(desc(taf))
df_rank <- rbind(df_a, df_b, df_c)
ts_nums <- length(unique(timestep))
taf_var_rank <- rep(seq(ts_nums),ts_nums)
taf_var_rank_xaxis <- taf_var_rank/(ts_nums+1) #standard calc for xaxis
df_rank <- data.frame(df_rank, taf_var_rank, taf_var_rank_xaxis)
Producing this, df_rank
:
For my end goal of plots like these:
ggplot(df_rank, aes(x = taf_var_rank_xaxis, y = taf, color = var)) + geom_line() +
labs(x = "probability of exceedance")
I'm pretty new to R (and programming) and I think I could build a general function, or maybe if I'm lucky there's an existing library/functions to condense this process for me? Any help is very much appreciated, as I have some long time series with many variables.
cheers, dave
Look at what you did, it doesn't appear you need to make those separate data.frames. You can just use mostly dplyr functions to do the same thing:
df %>% arrange(var, desc(taf)) %>%
group_by(var) %>%
mutate(taf_var_rank = row_number(),
taf_var_rank_xaxis = taf_var_rank/(n()+1)) %>%
ggplot(aes(x = taf_var_rank_xaxis, y = taf, color = var)) +
geom_line() +
labs(x = "probability of exceedance")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.