如何更改ggplot图形的类型

how to change the type of ggplot graphic

我有数据框(举例)

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

我用 ggplot 做了条形图

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)

如果我想得到像这样的最好的图形,我必须怎么做才能看到条形之间的叠加,谢谢??????

当然可以在 ggplot 中重新创建您的情节:

问题是我不得不自己编造数据,因为你的例子不可重现。您的 ggplot 调用引用了一个您未提供的名为 data2 的对象,还引用了不在您的示例数据框中的列,以及一个名为 mycolors 的对象未定义。

我创建此图的方法是使 x 轴连续,但应用来自 list 的标签(我已将其重命名为 m_list,因为将数据对象与与常用函数同名)。我用 geom_area 绘制了形状并使它们部分透明。

如果您想要一个答案,告诉您如何将数据转换为可以像这样绘制的格式,您需要向我们展示您使用的实际数据。

我要给出的一条建议是,如果您想分析和绘制带有日期的数据,最好将它们存储为实际日期而不是字符串或因子。否则,您将不得不不断想出一些技巧和解决方法。

这是一个完整的可重现示例。如果您将其复制并粘贴到您的 R 控制台中,您将得到上面的图(尽管您必须按 "zoom" 并更改 window 的尺寸以使其达到正确的形状)

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