Search code examples
rggplot2geom-area

Creating a smooth line when using geom_area in ggplot - R


I am trying to create an area chart with a smooth line using R and ggplot. I am able to create an area chart but I am unable to create a smooth line. I have tried a combination of geom_area and either geom_smooth or stat_summary. Stat_summary produced a smooth line but it only created a smooth line only one side of the x-axis at a time. I'm fairly new to R so I may be making a very elementary error and not realizing it. Any help would be greatly appreciated!

My goal is to create something like this:

Sample Image

I have attached the sample data I have been using:

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"))
                                                                               

Here is the code with geom_smooth that produces an area chart but does not create a smooth line:

  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) 

Here is the current plot produced:

Current Plot


Solution

  • Try this approach that is close to what you want. You can define stat_smooth() and consider the geom area, in that way you can shade the curves. Here the code to produce a plot similar to that showed:

    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))
    

    Output:

    enter image description here

    If more details are required, try with 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')
    

    Output:

    enter image description here

    And for almost close representation of the plot you added:

    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'))
    

    Output:

    enter image description here

    As we have used dark colors, be sure you have installed ggdark package.