使用 ggplot,如何根据日期组合线和多边形?
Using ggplot, How does combine lines and polygons based on dates?
假设我有以下数据框:
Date_spill<-rev(seq(as.Date("2019-02-28"), length = 3135, by = "-1 day"))
Total<-data.frame(Total_H7=runif(3135, min=0.6, max = 0.8))
Total_var<-data.frame(Total_H7=runif(3135, min=0.4, max = 0.55))
CPM_period<-data.frame(x=c(as.numeric(Date_spill[702]), as.numeric(Date_spill[1402]), as.numeric(Date_spill[702]), as.numeric(Date_spill[1402]), as.numeric(Date_spill[1970]), as.numeric(Date_spill[2145]), as.numeric(Date_spill[1970]), as.numeric(Date_spill[2145])
),
y=c(min(Total_var$Total_H7), min(Total_var$Total_H7), max(Total$Total_H7), max(Total$Total_H7), min(Total_var$Total_H7), min(Total_var$Total_H7), max(Total$Total_H7), max(Total$Total_H7)),
period=c("a", "a", "a", "a", "b", "b", "b", "b"))
我想绘制一个结合了两条线和两个多边形的图形。我正在使用以下代码:
Total_ReVarCov<-ggplot() +
geom_line(aes(y = Total$Total_H7, x=Date_spill), color="skyblue3", linetype="twodash", alpha=0.6)+ #First time series
geom_line(aes(y = Total_var$Total_H7, x=Date_spill), color="darkred", linetype="twodash", alpha=0.6)+ #Second time series
geom_polygon(aes(x=CPM_period$x, y=CPM_period$y, group=CPM_period$period))+ #Two polygons
geom_vline(xintercept=as.numeric(Date_spill[2528]), linetype=4)+ #An specific fact
labs(x = "", y="Aggregated Index", colour="Variables")
这给了我以下错误 Error: Invalid input: date_trans works with objects of class Date only
我确定代码与 geom_polygon
有关,因为当我编写必须绘制多边形的行时出现错误。使用我的原始数据,没有多边形的图如下:
如何使用日期绘制多边形?
问题是您在定义数据集 CPM_period
和 geom_vline
时将日期变量转换为数字。因此,将其保留为日期:
library(ggplot2)
Date_spill<-rev(seq(as.Date("2019-02-28"), length = 3135, by = "-1 day"))
Total<-data.frame(Total_H7=runif(3135, min=0.6, max = 0.8))
Total_var<-data.frame(Total_H7=runif(3135, min=0.4, max = 0.55))
CPM_period<-data.frame(x= Date_spill[c(702, 1402, 702, 1402, 1970, 2145, 1970,2145)],
y=c(min(Total_var$Total_H7), min(Total_var$Total_H7), max(Total$Total_H7), max(Total$Total_H7), min(Total_var$Total_H7), min(Total_var$Total_H7), max(Total$Total_H7), max(Total$Total_H7)),
period=c("a", "a", "a", "a", "b", "b", "b", "b"))
ggplot() +
geom_line(aes(y = Total$Total_H7, x=Date_spill), color="skyblue3", linetype="twodash", alpha=0.6)+ #First time series
geom_line(aes(y = Total_var$Total_H7, x=Date_spill), color="darkred", linetype="twodash", alpha=0.6)+ #Second time series
geom_polygon(aes(x=CPM_period$x, y=CPM_period$y, group=CPM_period$period))+ #Two polygons
geom_vline(xintercept=Date_spill[2528], linetype=4)+ #An specific fact
labs(x = "", y="Aggregated Index", colour="Variables")
假设我有以下数据框:
Date_spill<-rev(seq(as.Date("2019-02-28"), length = 3135, by = "-1 day"))
Total<-data.frame(Total_H7=runif(3135, min=0.6, max = 0.8))
Total_var<-data.frame(Total_H7=runif(3135, min=0.4, max = 0.55))
CPM_period<-data.frame(x=c(as.numeric(Date_spill[702]), as.numeric(Date_spill[1402]), as.numeric(Date_spill[702]), as.numeric(Date_spill[1402]), as.numeric(Date_spill[1970]), as.numeric(Date_spill[2145]), as.numeric(Date_spill[1970]), as.numeric(Date_spill[2145])
),
y=c(min(Total_var$Total_H7), min(Total_var$Total_H7), max(Total$Total_H7), max(Total$Total_H7), min(Total_var$Total_H7), min(Total_var$Total_H7), max(Total$Total_H7), max(Total$Total_H7)),
period=c("a", "a", "a", "a", "b", "b", "b", "b"))
我想绘制一个结合了两条线和两个多边形的图形。我正在使用以下代码:
Total_ReVarCov<-ggplot() +
geom_line(aes(y = Total$Total_H7, x=Date_spill), color="skyblue3", linetype="twodash", alpha=0.6)+ #First time series
geom_line(aes(y = Total_var$Total_H7, x=Date_spill), color="darkred", linetype="twodash", alpha=0.6)+ #Second time series
geom_polygon(aes(x=CPM_period$x, y=CPM_period$y, group=CPM_period$period))+ #Two polygons
geom_vline(xintercept=as.numeric(Date_spill[2528]), linetype=4)+ #An specific fact
labs(x = "", y="Aggregated Index", colour="Variables")
这给了我以下错误 Error: Invalid input: date_trans works with objects of class Date only
我确定代码与 geom_polygon
有关,因为当我编写必须绘制多边形的行时出现错误。使用我的原始数据,没有多边形的图如下:
如何使用日期绘制多边形?
问题是您在定义数据集 CPM_period
和 geom_vline
时将日期变量转换为数字。因此,将其保留为日期:
library(ggplot2)
Date_spill<-rev(seq(as.Date("2019-02-28"), length = 3135, by = "-1 day"))
Total<-data.frame(Total_H7=runif(3135, min=0.6, max = 0.8))
Total_var<-data.frame(Total_H7=runif(3135, min=0.4, max = 0.55))
CPM_period<-data.frame(x= Date_spill[c(702, 1402, 702, 1402, 1970, 2145, 1970,2145)],
y=c(min(Total_var$Total_H7), min(Total_var$Total_H7), max(Total$Total_H7), max(Total$Total_H7), min(Total_var$Total_H7), min(Total_var$Total_H7), max(Total$Total_H7), max(Total$Total_H7)),
period=c("a", "a", "a", "a", "b", "b", "b", "b"))
ggplot() +
geom_line(aes(y = Total$Total_H7, x=Date_spill), color="skyblue3", linetype="twodash", alpha=0.6)+ #First time series
geom_line(aes(y = Total_var$Total_H7, x=Date_spill), color="darkred", linetype="twodash", alpha=0.6)+ #Second time series
geom_polygon(aes(x=CPM_period$x, y=CPM_period$y, group=CPM_period$period))+ #Two polygons
geom_vline(xintercept=Date_spill[2528], linetype=4)+ #An specific fact
labs(x = "", y="Aggregated Index", colour="Variables")