2

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

Abdel_El
  • 77
  • 6
  • I don't think it's possible with the current ggplot2 but you can try if you can find any extension https://exts.ggplot2.tidyverse.org/gallery/ – Tung May 17 '20 at 16:51
  • @Tung : ah thanks, I spent a lot of time to search that on the internet, maybe there is no solution with ggplot but what do you think, what I can do if I want just to put overlay between bars, I tried geom_col(position="identity") but it's difficult to distinguish between colors – Abdel_El May 17 '20 at 17:37
  • How about this https://github.com/clauswilke/ggtextures – Tung May 17 '20 at 18:42
  • With appropriate manipulation, `geom_ribbon` is what you want to use and this is included within the standard ggplot2 R package. – statstew May 17 '20 at 21:13

1 Answers1

1

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 = "")
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • thanks a lot, I have to readjust my database to be able to make this plot I see, with what I have now it's not possible, it's like every time I must have the point in the x-axis for the beginning and the end of the plot line, what you suggest me to use as function to do that simply ? – Abdel_El May 17 '20 at 23:48
  • @Abdel_El I really think you need to start by converting all your labels that represent dates to actual dates. That would make it far easier. – Allan Cameron May 18 '20 at 08:57
  • @ Allan Cameron : exactly this is what I deed, to have all the elements included in "m_list" , not just a half for labels, what do you suggest to use in your code ?? to have all the elements: jan_0, jan_1......Dec_46,Dec_47 in the x-axis – Abdel_El May 18 '20 at 16:28
  • @Abdel_El there are only half the codes on your example image. If you include all of them you won't be able to read them. If you really want to try that, then change to `scale_x_continuous(labels = m_list, breaks = 1:48 / 2, limits = c(1, 24))`. But you're still not listening about changing to a proper date format. – Allan Cameron May 18 '20 at 16:58