简体   繁体   English

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

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

I have a data.frame with multiple columns recording values for months of the year.我有一个 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 

I would like to plot separate line charts for these columns, but with the same x-axis, and stacked on top of each other.我想为这些列绘制单独的折线图,但具有相同的 x 轴,并堆叠在彼此的顶部。 I am trying to use 'ggplot2' and the 'facet_wrap' to do this, but I can't seem to work out how exactly to do it.我正在尝试使用 'ggplot2' 和 'facet_wrap' 来做到这一点,但我似乎无法弄清楚如何做到这一点。 I can get a single line plot:我可以得到一个单线图:

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

But I want to have this stacked with the line plot for 'mwe1.y' directly below it.但我想把它与'mwe1.y'的线图直接放在它下面。 Can anybody help me out?有人可以帮我吗?

Maybe you are looking for this.也许你正在寻找这个。 The key is to reshape your data to long in order to use the full potential of ggplot2 .关键是将您的数据重塑为 long 以充分利用ggplot2潜力。 Here the code using tidyverse functions:这里使用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()

Output:输出:

在此处输入图片说明

Some data used:使用的一些数据:

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

Or maybe this:或者也许这个:

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

Output:输出:

在此处输入图片说明

Update: The OP only wish specific variables, so we can use filter() :更新: 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))

Output:输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM