2

我有数据框(例如)

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)

在此处输入图像描述

如果我想获得像这样的最佳图形,我必须做什么,我可以看到条形之间的叠加,谢谢?????? 在此处输入图像描述

4

1 回答 1

1

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

在此处输入图像描述

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

我创建此图的方式是使 x 轴连续但应用标签list(我已重命名m_list,因为拥有与常用函数同名的数据对象是一个非常糟糕的主意)。我已经绘制了形状geom_area并使它们部分透明。

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

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

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

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 = "")
于 2020-05-17T21:43:38.753 回答