简体   繁体   English

快速将“整理”的数据转换为超越图(又称超越图)的方法

[英]methods to quickly turn 'tidy' data into exceedance plots (aka exceedence plots)

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. 变成如下所示的“超标图”或“超标图”(在水资源应用中通常用两种方式拼写),它们只是沿x轴对变量进行排序/排序。

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 : 产生这个, 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? 我对R(和编程)还很陌生,我想我可以构建一个通用函数,或者如果我很幸运,可以使用一个现有的库/函数为我压缩这个过程? 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. 看看您所做的,似乎并不需要创建那些单独的data.frames。 You can just use mostly dplyr functions to do the same thing: 您可以只使用大多数dplyr函数来执行相同的操作:

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

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

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