在 ggplot - R 中使用 geom_area 时创建平滑线
Creating a smooth line when using geom_area in ggplot - R
我正在尝试使用 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
试试这个接近你想要的方法。您可以定义 stat_smooth()
并考虑 geom 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
包。
我正在尝试使用 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
试试这个接近你想要的方法。您可以定义 stat_smooth()
并考虑 geom 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
包。