多个变量的折线图
Line chart for multiple Variables
以下是我的数据集:
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
现在我想要在 Y 轴上 LOAD,在 X 轴上Year 并且线条应该描述[的负载和年份变化=15=]每个月。 不同月份的单独行
使用 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")
试试这个(假设你的数据帧是 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()
如果你想一起显示每年/每月的变化,你可以试试这个:
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))
以下是我的数据集:
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
现在我想要在 Y 轴上 LOAD,在 X 轴上Year 并且线条应该描述[的负载和年份变化=15=]每个月。 不同月份的单独行
使用 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")
试试这个(假设你的数据帧是 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()
如果你想一起显示每年/每月的变化,你可以试试这个:
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))