如何为两条 geom_smooth 行之间的区域着色?
How to color the area between two geom_smooth lines?
我在数据框中有 3 列,我想使用 geom_smooth() 从中创建可视化:
ggplot(my_data_frame) +
aes(x = fin_enquete,
y = intentions,
colour = candidat) +
geom_point(alpha = 1/6,
shape = "circle",
size = .5L) +
geom_smooth(mapping = aes(y = erreur_inf),
size = .5L,
span = .42,
se = F) +
geom_smooth(mapping = aes(y = erreur_sup),
size = .5L,
span = .42,
se = F) +
geom_smooth(method = "loess",
size = 1.5L,
span = .42,
se = F) +
labs(x = "Date de fin d'enquête",
y = "Pourcentage d'intentions de vote") +
theme_minimal() +
theme(text = element_text(family = "DIN Pro")) +
coord_cartesian(expand = F) +
easy_remove_legend()
3 lines with geom_smooth
我想给上下线之间的区域上色。我知道 geom_ribbon() 函数,但我不确定在这种情况下是否可以使用它。
有人有解决办法吗?
祝你有愉快的一天!
您可以使用 geom_ribbon
并在 geom_ribbon
调用中自己计算 loess
模型吗?
玩具随机数据
dat <- data.frame(x=1:100, y=runif(100), y2=runif(100)+1, y3=runif(100)+2)
现在假设我们想要在 y
和 y3
之间绘制一条平滑的色带,并在它们之间画一条线 y2
:
ggplot( dat , aes(x, y2)) +
geom_ribbon(aes(ymin=predict(loess(y~x)),
ymax=predict(loess(y3~x))), alpha=0.3) +
geom_smooth(se=F)
您可以使用 lapply(
) smooth 来计算 df 值的范围,例如 (5,11,13) 来计算平滑度并仅绘制 se.
[= 的两条边14=]
示例代码:
library(ggplot2)
ggplot(data = mtcars,
mapping = aes(x = wt,
y = mpg)) +
geom_point(size = 2)+
lapply(c(5,11, 13), function (i) {
geom_smooth(
data = ~ cbind(., facet_plots = i),
method = lm,
se=F,
formula = y ~ splines::bs(x, i)
)
})+
#facet_wrap(vars(facet_plots))
geom_ribbon(
stat = "smooth",
method = "loess",
se = TRUE,
alpha = 0, # or, use fill = NA
colour = "black",
linetype = "dotted")+
theme_minimal()
剧情:
我在数据框中有 3 列,我想使用 geom_smooth() 从中创建可视化:
ggplot(my_data_frame) +
aes(x = fin_enquete,
y = intentions,
colour = candidat) +
geom_point(alpha = 1/6,
shape = "circle",
size = .5L) +
geom_smooth(mapping = aes(y = erreur_inf),
size = .5L,
span = .42,
se = F) +
geom_smooth(mapping = aes(y = erreur_sup),
size = .5L,
span = .42,
se = F) +
geom_smooth(method = "loess",
size = 1.5L,
span = .42,
se = F) +
labs(x = "Date de fin d'enquête",
y = "Pourcentage d'intentions de vote") +
theme_minimal() +
theme(text = element_text(family = "DIN Pro")) +
coord_cartesian(expand = F) +
easy_remove_legend()
3 lines with geom_smooth
我想给上下线之间的区域上色。我知道 geom_ribbon() 函数,但我不确定在这种情况下是否可以使用它。
有人有解决办法吗?
祝你有愉快的一天!
您可以使用 geom_ribbon
并在 geom_ribbon
调用中自己计算 loess
模型吗?
玩具随机数据
dat <- data.frame(x=1:100, y=runif(100), y2=runif(100)+1, y3=runif(100)+2)
现在假设我们想要在 y
和 y3
之间绘制一条平滑的色带,并在它们之间画一条线 y2
:
ggplot( dat , aes(x, y2)) +
geom_ribbon(aes(ymin=predict(loess(y~x)),
ymax=predict(loess(y3~x))), alpha=0.3) +
geom_smooth(se=F)
您可以使用 lapply(
) smooth 来计算 df 值的范围,例如 (5,11,13) 来计算平滑度并仅绘制 se.
[= 的两条边14=]
示例代码:
library(ggplot2)
ggplot(data = mtcars,
mapping = aes(x = wt,
y = mpg)) +
geom_point(size = 2)+
lapply(c(5,11, 13), function (i) {
geom_smooth(
data = ~ cbind(., facet_plots = i),
method = lm,
se=F,
formula = y ~ splines::bs(x, i)
)
})+
#facet_wrap(vars(facet_plots))
geom_ribbon(
stat = "smooth",
method = "loess",
se = TRUE,
alpha = 0, # or, use fill = NA
colour = "black",
linetype = "dotted")+
theme_minimal()
剧情: