簡體   English   中英

Plot 在 geom_bar 上划一條線

[英]Plot a line over geom_bar

我正在嘗試 plot 在我的條形圖上划一條線,顯示導入 + 導出。 有人可以幫忙嗎? 在 geom_line 之前一切正常。 這個網站https://aledemogr.com/2017/05/29/plots-based-on-un-data-in-r/啟發了我這樣做,但似乎無法做到這一點......

library(tidyverse)
library(readxl)

eximp <- read_excel("C:/Users/xxx/Downloads/eximp.xlsx")


mydata <- eximp %>% mutate(Balance=Export+Import) %>% 
  gather(Type,value,-1,-4)
base <- mydata %>%
  filter(Type != "Balance")

balance <- mydata %>%
  filter(Type == "Balance")

ggplot(balance, aes(x = Date, y = value)) +
  geom_bar(data = base, aes(fill = Type), stat = "identity") +
  geom_point(aes(colour = Type)) +
  geom_line(aes(colour = Type, group=1)) +
  labs(x = "", y = "Trade")+
  theme_classic()

.

Error: Aesthetics must be either length 1 or the same as the data (1): colour, x and y

數據:

eximp
# A tibble: 10 x 3
   Date                Export Import
   <dttm>               <dbl>  <dbl>
 1 2020-03-01 00:00:00 1000.   -804.
 2 2020-04-01 00:00:00 1212.  -2011.
 3 2020-05-01 00:00:00  806.  -1504.
 4 2020-06-01 00:00:00  700.   -320.
 5 2020-07-01 00:00:00  689.   -650.
 6 2020-08-01 00:00:00  260.  -1423.
 7 2020-09-01 00:00:00  371.   -512.
 8 2020-10-01 00:00:00  390.   -800.
 9 2020-11-01 00:00:00  235.  -1402.
10 2020-12-01 00:00:00   98.2  -662.



> dput(eximp)
structure(list(Date = structure(c(1583020800, 1585699200, 1588291200, 
1590969600, 1593561600, 1596240000, 1598918400, 1601510400, 1604188800, 
1606780800), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    Export = c(1000.49219126285, 1211.52108001295, 805.787086089857, 
    699.544263919341, 689.023877568315, 260.324542543, 370.641681212495, 
    390.499682375661, 234.51939579408, 98.1534745), Import = 
c(-803.910230262846, 
    -2011.04895501295, -1504.48058508986, -320.103174919341, 
    -650.005315568315, -1423.04591701548, -511.837273212495, 
    -800.04198337566, -1401.98263179408, -661.847163189375)), row.names = 
c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

當您創建“余額”dataframe 時,它看起來像是空的。 我認為您不需要拆分數據框,而是使用不同的美學。

library(tidyverse)

eximp <- structure(list(Date = structure(c(1583020800, 1585699200, 1588291200, 
                                           1590969600, 1593561600, 1596240000, 1598918400, 1601510400, 1604188800, 
                                           1606780800), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
                        Export = c(1000.49219126285, 1211.52108001295, 805.787086089857, 
                                   699.544263919341, 689.023877568315, 260.324542543, 370.641681212495, 
                                   390.499682375661, 234.51939579408, 98.1534745), Import = 
                              c(-803.910230262846, 
                                -2011.04895501295, -1504.48058508986, -320.103174919341, 
                                -650.005315568315, -1423.04591701548, -511.837273212495, 
                                -800.04198337566, -1401.98263179408, -661.847163189375)), row.names = 
                         c(NA, 
                           -10L), class = c("tbl_df", "tbl", "data.frame"))



mydata <- eximp %>% mutate(Balance=Export+Import) %>% 
      gather(Type,value,-1,-4)
# You can plot from here directly

head(arrange(mydata, Date))
# A tibble: 6 x 4
Date                Balance Type    value
<dttm>                <dbl> <chr>   <dbl>
1 2020-03-01 00:00:00    197. Export  1000.
2 2020-03-01 00:00:00    197. Import  -804.
3 2020-04-01 00:00:00   -800. Export  1212.
4 2020-04-01 00:00:00   -800. Import -2011.
5 2020-05-01 00:00:00   -699. Export   806.
6 2020-05-01 00:00:00   -699. Import -1504.

使用 mydata 作為一切的源數據,我們將 plot 值作為條形的 y 美學,將 Balance 作為線的美學。

ggplot(mydata, aes(x = Date, y = value)) +
      geom_bar(aes(fill = Type), stat = "identity") +  # could use geom_col and drop the stat
      geom_point(aes(colour = Type)) +
      geom_line(aes(x= Date, y = Balance)) +
      labs(x = "", y = "Trade")+
      theme_classic()

在此處輸入圖像描述

查看您嘗試模擬的示例,您可能還想使用平衡作為點幾何的 y 美學。 這就是我接近這個數字的方式

mydata %>%
      ggplot(aes( x = Date))+  # Only specifying the common aesthetics
      geom_col(aes(y = value, fill = Type))+   # Using geom_col which defaults to plotting the value rather than count
      geom_line(aes(y = Balance))+  
      geom_point(aes(y= Balance)) +  # so points are on top of line and showing balance
      labs(x = "", y = "Trade")+
      theme_classic()

在此處輸入圖像描述

這與@Brian Fisher 的回答差不多,但使用了更新的pivot_longer() function 並使用一個修改后的數據集。

library(dplyr)
library(tidyr)
library(ggplot2)


mydata <- 
  eximp %>% 
  mutate(Balance = Export+Import) %>% 
  pivot_longer(-Date)


ggplot() +
  geom_col(data = filter(mydata, name != "Balance"), aes(Date, value, fill = name))+
  geom_line(data = filter(mydata, name == "Balance"), aes(Date, value), size = 1) +
  labs(x = NULL,
       y = "Trade",
       fill = "Type")+
  theme_classic()

代表 package (v1.0.0) 於 2021 年 3 月 29 日創建

暫無
暫無

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

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