在 ggplot - R 中使用 geom_area 时创建平滑线
Posted
技术标签:
【中文标题】在 ggplot - R 中使用 geom_area 时创建平滑线【英文标题】:Creating a smooth line when using geom_area in ggplot - R 【发布时间】:2021-02-06 01:05:06 【问题描述】:我正在尝试使用 R 和 ggplot 创建一个带有平滑线的面积图。我可以创建面积图,但无法创建平滑线。我尝试了 geom_area 和 geom_smooth 或 stat_summary 的组合。 Stat_summary 生成了一条平滑线,但它一次只在 x 轴的一侧创建了一条平滑线。我对 R 相当陌生,所以我可能犯了一个非常基本的错误而没有意识到。任何帮助将不胜感激!
我的目标是创造这样的东西:
Sample Image
我附上了我一直在使用的示例数据:
data <- structure(list(time_min = c(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
9, 10, 11, 11, 15, 16, 19, 20, 21, 22, 23, 24, 25, 26, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 40, 41, 42, 43, 44,
45, 45, 46, 48, 49, 50, 52, 53, 53, 54, 55, 55, 56, 57, 58, 59
), team = c("a", "h", "a", "h", "a", "a", "a", "h", "h", "h",
"a", "h", "a", "a", "h", "h", "h", "h", "a", "a", "a", "h", "h",
"a", "a", "a", "h", "h", "a", "a", "h", "h", "a", "a", "h", "h",
"h", "a", "h", "a", "a", "a", "h", "a", "h", "a", "a", "a", "h",
"h", "a", "h", "a", "a", "h", "h", "h", "a", "a"), gf60 = c(-60,
60, -60, 120, -60, -120, -120, 120, 60, 60, -60, 60, -180, -60,
120, 60, 60, 60, -120, -60, -120, 60, 240, -120, -60, -120, 60,
240, -120, -180, 180, 120, -120, -180, 60, 120, 60, -60, 60,
-60, -120, -60, 240, -60, 60, -60, -120, -60, 180, 60, -120,
60, -120, -60, 60, 180, 60, -120, -120)), row.names = c(NA, -59L
), groups = structure(list(time_min = c(0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 15, 16, 19, 20, 21, 22, 23, 24, 25, 26, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 48, 49, 50, 52, 53, 54, 55, 56, 57, 58, 59), .rows = structure(list(
1:2, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11:12, 13L, 14:15,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L,
28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38:39,
40L, 41L, 42L, 43L, 44:45, 46L, 47L, 48L, 49L, 50L, 51:52,
53L, 54:55, 56L, 57L, 58L, 59L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 52L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
下面是带有 geom_smooth 的代码,它生成面积图但不创建平滑线:
ggplot(aes(x = time_min, y = gf60)) +
geom_smooth(method = "loess", se = FALSE, formula = 'y ~ x', span = 0.8) +
geom_area(aes(fill = team), show.legend = FALSE)
这是当前生成的图:
Current Plot
【问题讨论】:
请正确粘贴您的数据,您包含的输出出现错误! 我很抱歉。我已经重新加载了它。我相信它现在有效。我试图把它变成一种更容易阅读的格式,然后把它搞砸了。 尝试改变geom_area和geom_smooth的顺序! 【参考方案1】:尝试这种接近您想要的方法。您可以定义stat_smooth()
并考虑几何area
,这样您就可以对曲线进行着色。这里的代码生成类似于显示的图:
library(ggplot2)
#Code
ggplot(data,aes(x = time_min, y = gf60,group=team,color=team)) +
geom_smooth(method = "loess",
se = FALSE,
formula = 'y ~ x',
span = 0.8) +
stat_smooth(se=FALSE, geom="area",
method = 'loess', alpha=.5,
span = 0.8,aes(fill=team))
输出:
如果需要更多详细信息,请尝试使用facet_grid()
:
#Format labels 2
data$team <- factor(data$team,levels = c('h','a'),ordered = T)
#Code 2
ggplot(data,aes(x = time_min, y = gf60,group=team,color=team)) +
geom_smooth(method = "loess",
se = FALSE,
formula = 'y ~ x',
span = 0.8) +
stat_smooth(se=FALSE, geom="area",
method = 'loess', alpha=.5,
span = 0.8,aes(fill=team))+
ggdark::dark_theme_light()+
facet_grid(team~.,scales = 'free')+
theme(legend.position = 'none')
输出:
对于您添加的情节的几乎接近的表示:
library(ggplot2)
library(grid)
#Format labels 3
data$team <- factor(data$team,levels = c('h','a'),ordered = T)
#Code 3
ggplot(data,aes(x = time_min, y = gf60,group=team,color=team)) +
geom_smooth(method = "loess",
se = FALSE,
formula = 'y ~ x',
span = 0.8) +
stat_smooth(se=FALSE, geom="area",
method = 'loess', alpha=.5,
span = 0.8,aes(fill=team))+
ggdark::dark_theme_light()+
facet_grid(team~.,scales = 'free')+
theme(legend.position = 'none',
panel.spacing = unit(0, "lines"))+
scale_fill_manual(values=c('tomato','gold'))+
scale_color_manual(values=c('tomato','gold'))
输出:
由于我们使用了深色,请确保您已安装 ggdark
包。
【讨论】:
非常感谢!太棒了。感谢您的帮助。 @turtleR 太棒了!如果您有兴趣阅读此内容并考虑接受答案***.com/help/someone-answers以上是关于在 ggplot - R 中使用 geom_area 时创建平滑线的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 ggplot2 在 R 中制作具有透明背景的图形?
在 R 中的网格中模拟二维随机游走并使用 ggplot 绘图