首页 > 解决方案 > 在 ggplot - R 中使用 geom_area 时创建平滑线

问题描述

我正在尝试使用 R 和 ggplot 创建一个具有平滑线的面积图。我可以创建面积图,但无法创建平滑线。我尝试了 geom_area 和 geom_smooth 或 stat_summary 的组合。Stat_summary 生成了一条平滑线,但它一次只在 x 轴的一侧创建了一条平滑线。我对 R 相当陌生,所以我可能犯了一个非常基本的错误而没有意识到。任何帮助将不胜感激!

我的目标是创建这样的东西:

示例图像

我附上了我一直在使用的示例数据:

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) 

这是当前生成的图:

当前情节

标签: rggplot2geom-area

解决方案


尝试这种接近您想要的方法。您可以定义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软件包。


推荐阅读