简体   繁体   English

如何提取数据框中的最小值/最大值以将数据显示为功能区?

[英]How to extract the min/max values in a dataframe to display data as a ribbon?

I have several sets of data stored in a data frame. 我在数据框中存储了几组数据。 For the sake of this question, I provide below a way to generate this data frame, but IRL, I only have the merged data frame, not the intermediate ones. 为了解决这个问题,我在下面提供了一种生成此数据帧的方法,但是IRL,我只有merged数据帧,而没有中间的数据帧。

x <- seq.POSIXt(from = strptime("1970-01-01 00:00:00", format = "%Y-%m-%d %H:%M:%S"),
                to = strptime("1970-01-01 00:05:00", format = "%Y-%m-%d %H:%M:%S"),
                by = "10 sec")

x <- rep(x, each = 3)
y <- c()

set.seed(1)

for (i in 1:length(x)) {
  y <- c(y, runif(1, min = 0, max = i))
}

my.data.frame1 <- data.frame(x, y, data = as.factor("data1"))

y <- c()
for (i in 1:length(x)) {
  y <- c(y, runif(1, min = length(x) - i, max = length(x)))
}

my.data.frame2  <- data.frame(x, y, data = as.factor("data2"))

merged <- rbind(my.data.frame1, my.data.frame2)

ggplot(merged, aes(x, y, color = data)) + geom_point() + geom_line()

So for each type of data (data1 and data2), and for each date value on the x axis, I have 3 y values. 因此,对于每种类型的数据(data1和data2)以及x轴上的每个日期值,我都有3个y值。

The plot looks (bad) like this: 情节看起来(不好)是这样的:

在此处输入图片说明

What I want to do is to plot a geom_ribbon of the data but I don't know how to do it. 我想做的是绘制数据的geom_ribbon ,但我不知道该怎么做。

I first tried to extract the min and max values with an aggregate function as explained here for each time and build a new data frame without duplicate x values but couldn't make it work. 我首先尝试每次使用此处说明的aggregate函数提取最小值和最大值,并构建一个没有重复x值但无法正常工作的新数据框。

Can anyone help? 有人可以帮忙吗?

Edit: 编辑:

The code I tried with aggregate is this one: 我尝试使用aggregate的代码是以下代码:

aggregate(y ~ x, data = merged, max)

(Same for the min). (相同的分钟)。 But this does not make the difference between the data1 set and the data2 set. 但这并没有使data1集和data2集区别。 I know I could subset, but I guess it can be done using the "by" argument. 我知道可以子集化,但是我猜可以使用“ by”参数来完成。 Just couldn't make it work. 只是无法使其工作。

You were on the right track, and need to aggregate by both data and x instead of just x . 您处在正确的轨道上,需要按datax进行汇总,而不仅仅是x

You can either calculate the min and max by group separately in two aggregate calls and then merge or do both at the same time. 您可以在两个aggregate调用中分别按组计算minmax ,然后合并或同时进行。 For the second approach you'll need an additional step to get the output of the two functions into separate columns. 对于第二种方法,您将需要一个额外的步骤来将两个函数的输出放入单独的列中。

my.new.df = aggregate(y ~ data + x, data = merged, FUN = function(x) c(min = min(x), max = max(x)))

# Get the min and max as separate columns
my.new.df = as.data.frame(as.list(my.new.df))

ggplot(my.new.df, aes(x, fill = data)) + 
    geom_ribbon(aes(ymin = y.min, ymax = y.max), alpha = 0.6)

You can also make the plot directly using stat = "summary" in geom_ribbon instead of making an aggregate dataset for plotting. 您也可以直接在geom_ribbon使用stat = "summary"进行geom_ribbon而不必为绘图进行汇总。

ggplot(merged, aes(x, y, fill = data)) + 
    geom_ribbon(alpha = 0.6, stat = "summary", fun.ymax = max, fun.ymin = min)

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

相关问题 如何提取具有最小值或最大值的行? - How to extract the row with min or max values? R:当 max &lt;= min 时,如何替换(切换)数据帧中一行中的最大值和最小值? - R: How to to replace(switch) the max and min values in a row in a dataframe when max <= min? 如何在R中找到dataframe的字符串行的最大值和最小值? - How to find the max and min values of string rows of a dataframe in R? 如何在数据框中的变量中找到一组的最大值和最小值的差 - How to find the difference of max & min values in one group in a variable in a dataframe 针对 r 中的最大值和最小值提取时间 - Extract time against Max and Min values in r 表示数据帧中的行值,不包括R中的最小值和最大值 - mean from row values in a dataframe excluding min and max values in R 如何计算 R 中 dataframe 中一组其他唯一值的最小/最大项? - How can I count the min/max item for for a set of otherwise unique values within a dataframe in R? 使用R中的时间戳提取每小时最大/最小/中值 - Extract hourly max/min/median values with timestamp in R 如何使用 R 提取数据帧的最小和最大行并在布局中绘制多个图形 - how to extract min and max rows of data frame and draw multiple graph in a lay out using R 从具有最大最小值的数据框到每个键的值 - From dataframe with values per min max to value per key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM