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

问题描述

我有一个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'的线图叠加在其正下方。有人可以帮我吗?

解决方法

也许您正在寻找这个。关键是要重塑数据以充分利用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()

输出:

enter image description here

使用了一些数据:

#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,20L,25L,2L,13L,6L),mwe2.x = c(0L,0L,4L,6L,0L),mwe3.x = c(1L,1L,3L),mwe4.x = c(0L,mwe5.x = c(0L,3L,2L),mwe6.x = c(4L,5L,1L),mwe7.x = c(0L,mwe1.y = c(41L,76L,261L,546L,154L,74L,116L,243L,257L,144L,164L),mwe2.y = c(5L,33L,59L,80L,18L,54L,46L,9L,20L),mwe3.y = c(17L,61L,91L,71L,29L,21L,12L,42L,39L,54L),mwe4.y = c(4L,mwe5.y = c(11L,22L,69L,94L,44L,15L,23L,36L,55L,24L,88L),mwe6.y = c(4L,19L,5L),mwe7.y = c(28L,72L,309L,206L,58L,66L,89L,11L,51L)),class = "data.frame",row.names = c(NA,-12L))

或者也许是这样

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

输出:

enter image description here

更新:OP仅希望使用特定变量,因此我们可以使用filter()

#Code 3
df %>% mutate(zdate=factor(zdate,ordered = T)) %>%
  pivot_longer(-zdate) %>%
  filter(name %in% c('mwe1.x','mwe1.y')) %>%
  mutate(name=factor(name,axis.text.x = element_text(angle=90))

输出:

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...