2016-11-09 135 views
-3

以下是我的数据集:折线图多个变量

MONTH YEAR Load 
6 2011 5224.055 
7 2011 6073.028 
8 2011 5261.029 
9 2011 4769.155 
6 2012 4865.499 
7 2012 5797.578 
8 2012 5433.050 
9 2012 4482.148 
6 2013 4922.000 
7 2013 5808.981 
8 2013 4928.632 
9 2013 4395.204 
6 2014 4819.491 
7 2014 5258.155 
8 2014 4786.323 
9 2014 4468.914 
6 2015 4931.468 
7 2015 5403.063 
8 2015 5266.076 
9 2015 4803.703 

现在我想LOAD Y轴上在X轴和线年应该描绘负荷和年变化为每个月。单独一个月

回答

0

单独的行使用ggplot2(加dplyr到一个月数转换为月份名称上飞),这将是这样的(我们假设你的数据帧被称为dat):

library(dplyr) 
library(ggplot2) 

ggplot(dat %>% mutate(MONTH=factor(month.abb[MONTH], levels=month.abb)), 
     aes(x=YEAR, y=Load, colour=MONTH)) + 
    geom_line() + 
    geom_point() + 
    theme_bw() + 
    scale_y_continuous(limits=c(0,6500)) + 
    labs(colour="Month") 

enter image description here

+0

它的工作。谢谢。 –

0

试试这个(假设你的数据帧是在DF):

head(df) 

MONTH YEAR  Load 
1  6 2011 5224.055 
2  7 2011 6073.028 
3  8 2011 5261.029 
4  9 2011 4769.155 
5  6 2012 4865.499 
6  7 2012 5797.578 

如果你想显示每月的变化,然后使用以下命令:

df$MONTH <- as.factor(df$MONTH) 
ggplot(df, aes(YEAR, Load, colour = MONTH, group=MONTH, color=MONTH))+ 
    geom_line(lwd=2) +geom_point() 

enter image description here

如果你想显示每年的变化/月在一起,你可以试试这个:

library(ggplot2) 
library(scales) 
df$Date <- as.Date(paste(1, df$MONTH, df$YEAR, sep='/'), '%d/%m/%Y') 
ggplot(df, aes(Date, Load, colour = Load))+ 
    geom_line() +geom_point() + 
    scale_x_date(date_breaks= "1 month", date_labels = "%m/%Y") + 
    theme(axis.text.x = element_text(angle=90, vjust = 0.5)) 

enter image description here