簡體   English   中英

如何使用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))

輸出:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM