Search code examples
rggplot2graphicsgeom-col

how to change the format of graphique in ggplot to combine geom_col and geom_surface


I'm trying to plot with ggplot my data frame by using the code bellow

p1 <- ggplot(dates2, aes(x=periode_ap, y=pourcentage_parc, fill=apport))+
        geom_col(position = position_stack(reverse = TRUE))+
        ylim(0,100)+
        scale_fill_manual(values=mycolors, name="")+
        theme_light()

i get this graphic picture but what i want is this one ::

picture 2

how can i change my code to get the right graphic please !!! or i must use another package not ggplot to do that !!


Solution

  • If you post your data, we can work with that. Otherwise, here's an example of something that should work. Use a combination of geom_area and geom_line with your data, and adjust fill= and color= aesthetics, respectively for each.

    df <- data.frame(
      x=c(4,5,6,7,8:15,10,11,12,12,13,14,15),
      y=c(0,100,0,0,20,20,40,10,10,10,10,0,0,50,0,0,60,60,0),
      id=c(rep('Big Peak',3), rep('Other Thing',9),rep('Another Item',3),rep('At the end',4))
    )
    
    ggplot(df, aes(x,y)) + theme_bw() + xlim(0,20) +
      geom_area(aes(fill=id), alpha=0.2, position='identity') +
      geom_line(aes(color=id), size=0.8) +
      scale_y_continuous(expand=expansion(c(0,0.22))) +
      theme(legend.position=c(0.8,0.8), legend.background = element_rect(color='black'))
    

    enter image description here

    A potentially important aesthetic is to adjust the scale_y_continuous expansion to 0 for the bottom limit in order to avoid whitespace there. Otherwise, your area geoms will be "floating" over the x axis line.