[英]In ggplot2,specify values to use for geom_smooth() confidence interval (similar to geom_errorbar)
I am plotting temperature values over the course of a year using geom_smooth()
.我正在使用geom_smooth()
绘制一年中的温度值。 I have daily min, max, and mean temperatures, and I would like to display the mean as a line, and then the min and max like you would display a confidence interval.我有每天的最低、最高和平均温度,我想将平均值显示为一条线,然后像你一样显示一个置信区间的最小值和最大值。 There is a se = T
argument to geom_smooth()
, and I am wondering how I can supply variables to use as the "confidence interval." geom_smooth()
有一个se = T
参数,我想知道如何提供变量以用作“置信区间”。 I know you can do this with box plots with geom_errorbar()
, and I'm hoping there is a similar version for line plots.我知道你可以用geom_errorbar()
的箱形图来做到这一点,我希望有一个类似的线图版本。
I would like my plot to look like the results of this, but using the max and min values to display the confidence interval.我希望我的情节看起来像这样的结果,但使用最大值和最小值来显示置信区间。
mean <- runif(365, min = 10, max = 25)
max <- runif(365, min = 26, max = 35)
min <- runif(365, min = 0, max = 9)
temp <- data.frame(day = 1:365, mean = mean)
ggplot(data = temp, aes(x = day, y = mean)) +
geom_smooth()
EDIT: I am adding two years of data, and would like each year to have its own geom_smooth line and its own geom_ribbon.编辑:我正在添加两年的数据,并且希望每年都有自己的 geom_smooth 线和自己的 geom_ribbon。 As the code is now, each year has its own geom_smooth line but there is a single geom_ribbon for all years.就像现在的代码一样,每年都有自己的 geom_smooth 线,但所有年份都有一个 geom_ribbon。
mean1 <- runif(365, min = 10, max = 25)
max1 <- runif(365, min = 26, max = 35)
min1 <- runif(365, min = 0, max = 9)
mean3 <- runif(365, min = 10, max = 25)
max3 <- runif(365, min = 30, max = 40)
min3 <- runif(365, min = -5, max = 5)
mean2 <- runif(365, min = 10, max = 25)
max2 <- runif(365, min = 200, max = 220)
min2 <- runif(365, min = -10, max = 9)
temp <- rbind(data.frame(day = 1:365, mean = mean1, max = max1, min = min1, label = 'A'),
data.frame(day = 1:365, mean = mean2, max = max2, min = min2, label = 'B'),
data.frame(day = 1:365, mean = mean3, max = max3, min = min3, label = 'C'))
ggplot(data = temp, aes(x = day, y = mean, color = label)) +
geom_ribbon(data = temp %>% group_by(label),
aes(ymin = stats::predict(loess(min ~ day)), ymax = stats::predict(loess(max ~ day)),
color = label),
alpha = 0.1) +
geom_smooth(se = F)
Thanks!谢谢!
EDIT #2: See bottom for smoothed ribbon.编辑#2:平滑色带见底部。 Adopted from https://stackoverflow.com/a/71423425/6851825采用自https://stackoverflow.com/a/71423425/6851825
EDIT: 2nd option below uses min
/ max
directly to define shaded range.编辑:下面的第二个选项直接使用min
/ max
来定义阴影范围。
We could specify a fixed confidence interval by putting the confidence interval into a ribbon layer that uses the stat_smooth
calculation, but where we override the y range to use the smooth value plus a constant.我们可以通过将置信区间放入使用stat_smooth
计算的功能区层中来指定一个固定的置信区间,但是我们覆盖 y 范围以使用平滑值加上一个常数。
ggplot(data = temp, aes(x = day, y = mean)) +
stat_smooth(
geom="ribbon",
aes(ymax = after_stat(y) + 1,
ymin = after_stat(y) - 1),
alpha = 0.1
) +
geom_smooth(se = FALSE)
Of if you mean you want to use the min and max values directly as your shaded range:如果你的意思是你想直接使用最小值和最大值作为你的阴影范围:
ggplot(data = temp, aes(x = day, y = mean)) +
geom_ribbon(aes(ymin = min, ymax = max),
alpha = 0.1) +
geom_smooth(se = FALSE)
Adopted from https://stackoverflow.com/a/71423425/6851825 , we can put the min and max into the data frame and make a ribbon that calls predict(loess(y~x))
to calculate the min and max ranges.采用https://stackoverflow.com/a/71423425/6851825 ,我们可以将最小值和最大值放入数据框中,并制作一个调用predict(loess(y~x))
的功能区来计算最小值和最大值范围。
temp <- data.frame(day = 1:365, mean = mean,
max = max, min = min)
ggplot(data = temp, aes(x = day)) +
geom_ribbon(alpha = 0.1,
aes(ymin = predict(loess(min~day)),
ymax = predict(loess(max~day)))) +
geom_smooth(aes(y = mean), se = FALSE) +
# these are to confirm the ranges match what geom_smooth would produce
geom_smooth(aes(y = max), se = FALSE, linetype = "dashed", size = 0.2) +
geom_smooth(aes(y = min), se = FALSE, linetype = "dashed", size = 0.2)
EDIT based on new grouped data:根据新的分组数据编辑:
The prior technique doesn't work with grouped data because the base predict
function doesn't "see" groups unless we've put it inside dplyr::group_by
and dplyr::mutate
.先前的技术不适用于分组数据,因为基本predict
函数不会“看到”组,除非我们将它放在dplyr::group_by
和dplyr::mutate
中。 So we can use those to precalculate the values we need in the data =
part.所以我们可以使用这些来预先计算我们在data =
部分中需要的值。
ggplot(data = temp, aes(x = day, y = mean, color = label)) +
geom_ribbon(data = temp %>% group_by(label) %>%
mutate(ymin_smooth = stats::predict(loess(min~day)),
ymax_smooth = stats::predict(loess(max~day))),
aes(ymin = ymin_smooth, ymax = ymax_smooth, fill = label),
alpha = 0.1) +
geom_smooth(se = F)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.