如何使用 GGPLOT 从多个 df 列制作具有相同 x 轴的堆叠线图?

How to use GGPLOT to make stacked line graphs with same x-axis from multiple df columns?

我有一个 data.frame 有多个列记录一年中几个月的值。

 zdate mwe1.x mwe2.x mwe3.x mwe4.x mwe5.x mwe6.x mwe7.x mwe1.y mwe2.y
1  Jan 2017     10      0      1      0      0      4      0     41      5
2  Feb 2017      7      0      0      0      0      0      0     76     33
3  Mar 2017     16      0      0      0      0      6      0    261     59
4  Apr 2017     40      4      0      0      1      0      0    546     80
5  May 2017      8      0      0      0      1      4      0    154     18
6  Jun 2017      7      0      0      0      2      1      0     74      4
7  Jul 2017     20      0      0      0      0      1      0    116      8
8  Aug 2017     25      6      1      0      3      6      1    243     54
9  Sep 2017      8      2      2      0      3      5      0    257     46
10 Oct 2017      2      0      0      0      0      0      0     74      7
11 Nov 2017     13      0      0      0      1      0      0    144      9
12 Dec 2017      6      0      3      0      2      1      0    164     20
   mwe3.y mwe4.y mwe5.y mwe6.y mwe7.y
1      17      4     11      4     28
2      61      0     22      7     72
3      91      1     69     16    309
4      71      0     94     19    206
5      29      0     44      3     58
6      21      0     15      2     66
7      12      0     23      2     20
8      20      0     36      2     55
9      42      0     55      7     89
10     13      0     24      0      7
11     39      0     18      1     11
12     54      0     88      5     51 

我想为这些列绘制单独的折线图,但具有相同的 x 轴,并且相互堆叠。我正在尝试使用 'ggplot2' 和 'facet_wrap' 来执行此操作,但我似乎无法弄清楚该怎么做。我可以获得单线图:

plot <- ggplot(all, aes(x = all$date, y = all$mwe1.x)) +
+     geom_line()

但我想将其与 'mwe1.y' 的线图直接叠加在其下方。有人可以帮我吗?

也许您正在寻找这个。关键是将数据重塑为 long 以充分发挥 ggplot2 的潜力。这里的代码使用 tidyverse 函数:

library(tidyverse)
#Code
df %>% mutate(zdate=factor(zdate,levels = unique(zdate),ordered = T)) %>%
  pivot_longer(-zdate) %>%
  ggplot(aes(x=zdate,y=value,group=name,color=name))+
  geom_line()

输出:

使用了一些数据:

#Data
df <- structure(list(zdate = c("Jan-2017", "Feb-2017", "Mar-2017", 
"Apr-2017", "May-2017", "Jun-2017", "Jul-2017", "Aug-2017", "Sep-2017", 
"Oct-2017", "Nov-2017", "Dec-2017"), mwe1.x = c(10L, 7L, 16L, 
40L, 8L, 7L, 20L, 25L, 8L, 2L, 13L, 6L), mwe2.x = c(0L, 0L, 0L, 
4L, 0L, 0L, 0L, 6L, 2L, 0L, 0L, 0L), mwe3.x = c(1L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 2L, 0L, 0L, 3L), mwe4.x = c(0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L), mwe5.x = c(0L, 0L, 0L, 1L, 1L, 2L, 
0L, 3L, 3L, 0L, 1L, 2L), mwe6.x = c(4L, 0L, 6L, 0L, 4L, 1L, 1L, 
6L, 5L, 0L, 0L, 1L), mwe7.x = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
0L, 0L, 0L, 0L), mwe1.y = c(41L, 76L, 261L, 546L, 154L, 74L, 
116L, 243L, 257L, 74L, 144L, 164L), mwe2.y = c(5L, 33L, 59L, 
80L, 18L, 4L, 8L, 54L, 46L, 7L, 9L, 20L), mwe3.y = c(17L, 61L, 
91L, 71L, 29L, 21L, 12L, 20L, 42L, 13L, 39L, 54L), mwe4.y = c(4L, 
0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), mwe5.y = c(11L, 
22L, 69L, 94L, 44L, 15L, 23L, 36L, 55L, 24L, 18L, 88L), mwe6.y = c(4L, 
7L, 16L, 19L, 3L, 2L, 2L, 2L, 7L, 0L, 1L, 5L), mwe7.y = c(28L, 
72L, 309L, 206L, 58L, 66L, 20L, 55L, 89L, 7L, 11L, 51L)), class = "data.frame", row.names = c(NA, 
-12L))

或者这样:

#Code 2
df %>% mutate(zdate=factor(zdate,levels = unique(zdate),ordered = T)) %>%
  pivot_longer(-zdate) %>%
  mutate(name=factor(name,levels = unique(name),ordered = T)) %>%
  ggplot(aes(x=zdate,y=value,group=name,color=name))+
  geom_line()+
  facet_wrap(.~name,ncol = 7,scales = 'free_y')+
  theme(legend.position = 'none',
        axis.text.x = element_text(angle=90))

输出:

更新: OP只想要特定的变量,所以我们可以使用filter()

#Code 3
df %>% mutate(zdate=factor(zdate,levels = unique(zdate),ordered = T)) %>%
  pivot_longer(-zdate) %>%
  filter(name %in% c('mwe1.x','mwe1.y')) %>%
  mutate(name=factor(name,levels = unique(name),ordered = T)) %>%
  ggplot(aes(x=zdate,y=value,group=name,color=name))+
  geom_line()+
  facet_wrap(.~name,ncol = 7,scales = 'free_y')+
  theme(legend.position = 'none',
        axis.text.x = element_text(angle=90))

输出: