如何填充 geom_xspline 创建的曲线下的区域
How to fill area under a curve created by geom_xspline
对于后面的数据df
和plot,希望能把线条平滑一些,尤其是下图中蓝色矩形标出的尖角部分:
df <- structure(list(date = c("2022-2-1", "2022-2-2", "2022-2-3", "2022-2-4",
"2022-2-5", "2022-2-6", "2022-2-7", "2022-2-8", "2022-2-9", "2022-2-10",
"2022-2-11", "2022-2-12", "2022-2-13", "2022-2-14", "2022-2-15",
"2022-2-16", "2022-2-17"), pct_change = c(4, 4, 4.04, 4.04, 4.04,
4.44, 4.88, 4.62, 4.8, 5.2, 4.7, 5.06, 4.56, 4.8, 4.32, 4.02,
4.01)), class = "data.frame", row.names = c(NA, -17L))
df1 <- df %>%
mutate_at(vars(-date), funs(./100))
df1 %>%
ggplot(aes(x=as.POSIXct(date), y=pct_change)) +
geom_line(size=1, alpha=0.7, color='red') +
geom_hline(yintercept=c(min(df1$pct_change), max(df1$pct_change)), linetype='solid', col='black')
使用ggalt::geom_xspline(spline_shape=0.3, size=1, alpha=0.7, color='red')
,我得到了平滑的线,但填充区域并不完美(如您所见,它填充了geom_line(size=1, alpha=0.7, color='red')
绘制的原始曲线下方的区域。
library(ggalt)
df1 %>%
ggplot(aes(x=as.POSIXct(date), y=pct_change)) +
# geom_line(size=1, alpha=0.7, color='red') +
geom_xspline(spline_shape=0.3, size=1, alpha=0.7, color='red') +
geom_ribbon(aes(ymin = min(pct_change), ymax = pct_change), fill = 'red', alpha=0.3, position = "identity") +
# ggforce::stat_bspline(geom = "area", alpha = 0.3, color='red') +
geom_hline(yintercept=c(min(df1$pct_change), max(df1$pct_change)), linetype='solid', col='black')
如何填充曲线下方的区域,例如 geom_xspline()
创建的曲线,或者我们有其他解决方案?感谢您的提前帮助。
这里有一个替代方案,使用 stat_smooth()
和 geom = "area"
而不是 geom_xspline()
,并使用 coord_cartesian
设置绘图限制:
library(tidyverse)
df1 %>%
ggplot(aes(x=as.POSIXct(date), y=pct_change)) +
stat_smooth(
geom = "area",
size = 1,
fill = "red",
color = "red",
alpha = 0.3,
span = .3
) +
coord_cartesian(ylim = c(.04, .0525), expand = FALSE)
由 reprex package (v2.0.1) 创建于 2022-03-01
对于后面的数据df
和plot,希望能把线条平滑一些,尤其是下图中蓝色矩形标出的尖角部分:
df <- structure(list(date = c("2022-2-1", "2022-2-2", "2022-2-3", "2022-2-4",
"2022-2-5", "2022-2-6", "2022-2-7", "2022-2-8", "2022-2-9", "2022-2-10",
"2022-2-11", "2022-2-12", "2022-2-13", "2022-2-14", "2022-2-15",
"2022-2-16", "2022-2-17"), pct_change = c(4, 4, 4.04, 4.04, 4.04,
4.44, 4.88, 4.62, 4.8, 5.2, 4.7, 5.06, 4.56, 4.8, 4.32, 4.02,
4.01)), class = "data.frame", row.names = c(NA, -17L))
df1 <- df %>%
mutate_at(vars(-date), funs(./100))
df1 %>%
ggplot(aes(x=as.POSIXct(date), y=pct_change)) +
geom_line(size=1, alpha=0.7, color='red') +
geom_hline(yintercept=c(min(df1$pct_change), max(df1$pct_change)), linetype='solid', col='black')
使用ggalt::geom_xspline(spline_shape=0.3, size=1, alpha=0.7, color='red')
,我得到了平滑的线,但填充区域并不完美(如您所见,它填充了geom_line(size=1, alpha=0.7, color='red')
绘制的原始曲线下方的区域。
library(ggalt)
df1 %>%
ggplot(aes(x=as.POSIXct(date), y=pct_change)) +
# geom_line(size=1, alpha=0.7, color='red') +
geom_xspline(spline_shape=0.3, size=1, alpha=0.7, color='red') +
geom_ribbon(aes(ymin = min(pct_change), ymax = pct_change), fill = 'red', alpha=0.3, position = "identity") +
# ggforce::stat_bspline(geom = "area", alpha = 0.3, color='red') +
geom_hline(yintercept=c(min(df1$pct_change), max(df1$pct_change)), linetype='solid', col='black')
如何填充曲线下方的区域,例如 geom_xspline()
创建的曲线,或者我们有其他解决方案?感谢您的提前帮助。
这里有一个替代方案,使用 stat_smooth()
和 geom = "area"
而不是 geom_xspline()
,并使用 coord_cartesian
设置绘图限制:
library(tidyverse)
df1 %>%
ggplot(aes(x=as.POSIXct(date), y=pct_change)) +
stat_smooth(
geom = "area",
size = 1,
fill = "red",
color = "red",
alpha = 0.3,
span = .3
) +
coord_cartesian(ylim = c(.04, .0525), expand = FALSE)
由 reprex package (v2.0.1) 创建于 2022-03-01