年度对比时间序列 ggplot2 R

3

我的 df:

> head(merged)
        Date patch     prod workmix_pct jobcounts travel FWIHweeklyAvg              month year
1 2013-03-29  BVG1 2.932208         100      9480   30.7      1.627024              March 2013
2 2013-03-29 BVG11 2.769156          10       968   34.3      4.475714              March 2013
3 2013-03-29 BVG12 2.857344          16      1551   33.8      3.098571              March 2013
4 2013-03-29 BVG13 2.870111          13      1267   29.1      1.361429              March 2013
5 2013-03-29 BVG14 3.011260          17      1625   28.1      1.550000              March 2013
6 2013-03-29 BVG15 3.236246          21      1946   24.9      1.392857              March 2013

我正在尝试绘制prod列的年度对比图。 我有从2013年3月2015年3月的数据。

这是我尝试过的:

ggplot(data=merged,aes(Date, prod)) + #dataframe 
  geom_line(data=merged[merged$patch %in% c("BVG1"),],aes(y=prod, colour="red"),lwd = 1.3,)+ #select BVG1
  geom_smooth() +
        scale_x_date(labels = date_format("%b-%Y"),breaks = "1 month") + #how many breaks and Date format
        ylab("Actual Prod") +
        ggtitle("Scotland's Overall Performance Financial Year\n2013/14 Vs 2014/15") +
        theme(axis.title.y = element_text(size = 25, vjust=0.3,face = "bold",color = "red"), 
        axis.text.y=element_text(size=25, color="blue"),
        plot.title = element_text(lineheight = .8,face = "bold",color = "red",size = 45, vjust = 1),
        legend.text = element_text(size=35))+ theme(legend.position="none")

这给了我这个图:

enter image description here

现在我想绘制2013年与2014年的比较,然后是2014年与2015年的比较。最后是2013年与2015年的比较。

这是我尝试过的:

ggplot(data=merged,aes(Date)) + #dataframe 
  geom_line(data=merged[merged$year==2013,],aes(y=prod, colour="red"),lwd = 1.3,)+ #select 2013
  geom_line(data=merged[merged$year==2014,],aes(y=prod, colour="blue"),lwd = 1.3,)+ #select 2014
        scale_x_date(labels = date_format("%b-%Y"),breaks = "1 month") + #how many breaks and Date format
        ylab("Actual Prod") +
        ggtitle("Scotland's Overall Performance Financial Year\n2013/14 Vs 2014/15") +
        theme(axis.title.y = element_text(size = 25, vjust=0.3,face = "bold",color = "red"), 
        axis.text.y=element_text(size=25, color="blue"),
        plot.title = element_text(lineheight = .8,face = "bold",color = "red",size = 45, vjust = 1),
        legend.text = element_text(size=35))+ theme(legend.position="none")

这是我收到的: enter image description here
希望能有以下内容: enter image description here
还有: enter image description here, 但不是以“周”为视图,而是以“月”为视图。
任何帮助或想法都将不胜感激。
非常感谢。
更新:
根据Ruthger Righart的回答,我进行了以下操作:
library(dplyr)

mergedYearonYearProdMeans = merged %>%
                                group_by(year,month) %>%
                                mutate(MonthlyAve = mean(prod))
ordered.months <- factor(mergedYearonYearProdMeans$month, as.character(mergedYearonYearProdMeans$month))

ggplot(data=mergedYearonYearProdMeans,aes(ordered.months,MonthlyAve,group=year,shape=year,color=year)) + #dataframe 
  geom_line()+ 
  scale_color_manual(values = c("red","blue","green"))

我的图表没有从2015年1月开始。产品只在1月、2月和3月显示,其他月份不应该显示平坦的绿线,如下所示。

enter image description here


你的数据中有月份和年份,那么为什么不作为下一步重新计算每个月和年份的平均Prod值,并使用aes(month)创建一个新的ggplot呢? - Ruthger Righart
不确定如何做。任何示例代码将不胜感激。 - Shery
嗨Shery,我创建了一个示例来展示如何制作这样的图表。希望这有所帮助! - Ruthger Righart
这太棒了...我没想到要按月份和年份汇总数据...解决了我的问题...谢谢@RuthgerRighart - Shery
@RuthgerRighart 抱歉,我的图表不是从一月份开始的,2015年其他月份也不应该有任何值。(请参见上面的更新)。你有什么想法吗? - Shery
1个回答

2
通常,对于这类图形来说,数据的准备工作是最重要的。 根据您的数据,我猜测您需要计算每年和每月平均“prod”值。可以使用plyr包中的ddply函数执行此步骤。以下是一个简单的数据示例以了解如何操作:
library(plyr)

dat<-data.frame(year=c("2012","2012","2012", "2012","2012","2012"), month=c("Jan", "Jan", "Jan", "Feb", "Feb", "Feb"), prod=as.numeric(c("2.00", "1.00", "3.00", "0.50", "1.50", "2.00")))

newdat<-ddply(dat, .(year, month), summarize, prod = mean(prod)) 

在这一步之后,你的数据应该在newdat中为每个年份和月份有一个平均值 "prod" ,并且已经使用正确的格式,可以使用ggplot绘图。我创建了一个新的简化数据示例,其格式相同:

df<-data.frame(year=c("2012","2012","2012","2012","2013","2013","2013","2013"), month=c("Jan","Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan","Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), prod=c("0.33","0.24","0.36","0.22","0.31","0.28","0.39","0.25", "0.23","0.22","0.46","0.52","0.61","0.18","0.59","0.55", "0.13","0.14","0.56","0.42","0.41","0.48","0.59","0.65"))

需要创建一个向量来正确排列x轴上的月份(否则ggplot会按字母顺序排序月份)

ordmonth<- factor(df$month, as.character(df$month))

library(ggplot2)

p<-ggplot(data=df, aes(x=ordmonth, y=prod, group=year, shape=year, color=year))+geom_line()
p<-p+scale_color_manual(values = c("red", "blue"))

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接