2017-09-22 50 views
0

我試圖創建一個比較年收入的陰謀,但我無法得到它的工作,不明白爲什麼。比較每年的收入

考慮我的DF:

df <- data.frame(date = seq(as.Date("2016-01-01"), as.Date("2017-10-01"), by = "month"), 
       rev = rnorm(22, 150, sd = 20)) 

    df %>% 
     separate(date, c("Year", "Month", "Date")) %>% 
     filter(Month <= max(Month[Year == "2017"])) %>% 
     group_by(Year, Month) %>% 
     ggplot(aes(x = Month, y = rev, fill = Year)) + 
     geom_line() 
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic? 

我真的不明白爲什麼這是行不通的。我想要的是從1月到10月的兩條線。

回答

1

這應該爲你工作:

library(tidyverse) 
df <- data.frame(date = seq(as.Date("2016-01-01"), as.Date("2017-10-01"), by = "month"), 
       rev = rnorm(22, 150, sd = 20)) 

df %>% 
    separate(date, c("Year", "Month", "Date")) %>% 
    filter(Month <= max(Month[Year == "2017"])) %>% 
    ggplot(aes(x = Month, y = rev, color = Year, group = Year)) + 
    geom_line() 

這只是其中出了錯因變量的類型分組,如果使用lubridate的日期(也是tidyverse包)

它可能是有用的
library(lubridate) 
df %>% 
    mutate(Year = as.factor(year(date)), Month = month(date)) %>% 
    filter(Month <= max(Month[Year == "2017"])) %>% 
    ggplot(aes(x = Month, y = rev, color = Year)) + 
    geom_line() 
0

我認爲ggplot2很混亂,因爲它不能識別Month列的格式,在這種情況下它是一個字符。嘗試將其轉換爲數字:

... + 
ggplot(aes(x = as.numeric(Month), y = rev, colour = Year)) + 
.... 

請注意,我用colour替換單詞fill,我認爲更有意義的這張圖:

sample output

順便說一句,我不知道的group_by聲明是添加任何東西。無論有沒有它,我都會得到相同的圖表。