2017-09-22 55 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声明是添加任何东西。无论有没有它,我都会得到相同的图表。