[英]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
. 您处在正确的轨道上,需要按data
和x
进行汇总,而不仅仅是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
调用中分别按组计算min
和max
,然后合并或同时进行。 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.