Search code examples
rggplot2graphicsgeom-col

how to change the type of ggplot graphic


I have the data frame (for example)

df <-data.frame(apport=c("Org 2016","Min 1","Min 2","Min 3"), 
                Aou_14=c(7.69,0,0,0), Sep_16=c(7.69,0,0,0),Sep_17=c(15.38,0,0,0),
                Oct_18=c(46.15,0,0,0),Oct_19=c(7.69,0,0,0),Nov_20=c(15.38,0,0,0),
                Fev_27=c(0,50,0,0),Mar_28=c(0,50,0,0), Mar_29=c(0,0,62.5,0),
                Avr_30=c(0,0,25,0),Avr_31=c(0,0,12.5,0),Mai_32=c(0,0,0,50),
                Mai_33=c(0,0,0,50))


 list<-c("Jan_0", "Jan_1", "Fev_2", "Fev_3","Mar_4", "Mar_5", "Avr_6", "Avr_7", "Mai_8", "Mai_9", "Jui_10", "Jui_11", "Jul_12", "Jul_13", "Aou_14", "Aou_15", "Sep_16", "Sep_17", "Oct_18", "Oct_19", "Nov_20","Nov_21", "Dec_22", "Dec_23", "Jan_24", "Jan_25", "Fev_26", "Fev_27", "Mar_28", "Mar_29", "Avr_30","Avr_31", "Mai_32", "Mai_33", "Jui_34", "Jui_35", "Jul_36", "Jul_37", "Aou_38", 
     "Aou_39", "Sep_40", "Sep_41", "Oct_42", "Oct_43", "Nov_44", "Nov_45", "Dec_46", "Dec_47")

I used ggplot to have a bar chart

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()+scale_x_discrete(labels = list)

enter image description here

what I must do if I want to get a best graphic like this one where I can see the superposition between bars, thanks ????? enter image description here


Solution

  • It's certainly possible to recreate your plot in ggplot:

    enter image description here

    The problem is that I had to make up my own data because your example is not reproducible. Your ggplot call references an object called data2, which you haven't provided, and also references columns that aren't in your example data frame, plus an object called mycolors that isn't defined.

    The way I created this plot was to make the x axis continuous but apply the labels from list (which I have renamed m_list because it's a really bad idea to have a data object with the same name as a commonly used function). I have drawn the shapes with geom_area and made them partly transparent.

    If you want an answer that tells you how to get your data into a format where you can plot it like this, you'll need to show us the actual data you used.

    One piece of advice I would give is that if you want to analyse and plot data with dates, it would be far better to store them as actual dates instead of strings or factors. Otherwise you will constantly have to come up with hacks and workarounds.

    Here is a full reproducible example. If you copy and paste this into your R console, you will get the above plot (though you will have to press "zoom" and change the window's dimensions to get it to the correct shape)

    library(ggplot2)
    
    m_list <- c("Jan_0", "Jan_1", "Fev_2", "Fev_3", "Mar_4", "Mar_5", "Avr_6", 
                "Avr_7", "Mai_8", "Mai_9", "Jui_10", "Jui_11", "Jul_12", "Jul_13", 
                "Aou_14", "Aou_15", "Sep_16", "Sep_17", "Oct_18", "Oct_19", "Nov_20", 
                "Nov_21", "Dec_22", "Dec_23", "Jan_24", "Jan_25", "Fev_26", "Fev_27", 
                "Mar_28", "Mar_29", "Avr_30", "Avr_31", "Mai_32", "Mai_33", "Jui_34", 
                "Jui_35", "Jul_36", "Jul_37", "Aou_38", "Aou_39", "Sep_40", "Sep_41", 
                "Oct_42", "Oct_43", "Nov_44", "Nov_45", "Dec_46", "Dec_47")
    
    df <- structure(list(x = c(9.5, 10, 10.5, 13.5, 14, 14.5, 15, 15.5, 
        18, 18.5, 15, 15.5, 16, 16.5, 17.5, 18, 16.5, 17, 17.5, 18), 
        y = c(0L, 100L, 0L, 0L, 20L, 20L, 35L, 5L, 5L, 0L, 0L, 50L, 
        30L, 10L, 10L, 0L, 0L, 50L, 50L, 0L), group = structure(c(1L, 
        1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
        4L, 4L, 4L, 4L), .Label = c("ORG-1", "MIN-1", "MIN-2", "MIN-3"
        ), class = "factor")), row.names = c(NA, -20L), class = "data.frame")
    
    my_colours <- c("red", "green", "#A09000", "blue")
    
    ggplot(df, aes(x, y, fill = group, colour = group)) +
      geom_area(alpha = 0.5, position = "identity", size = 0 )+
      geom_line() +
      scale_y_continuous(breaks = 10*0:10, expand = c(0, 0)) +
      scale_fill_manual(values = my_colours, name = "") +
      scale_colour_manual(values = my_colours, name = "") +
      theme_bw() + 
      scale_x_continuous(labels = m_list[seq_along(m_list) %% 2 == 1], 
                         breaks = 1:24, limits = c(1, 24)) +
      theme(panel.grid = element_blank(),
            axis.text.x = element_text(angle = 45, hjust = 1),
            legend.position = c(0.9, 0.65)) + 
      labs(x = "", y = "")