我想要在某个时间点插入垂直线的时间序列图。 我的数据涵盖了从1990年1月起。 当我使用ggplot2时,垂直线被推到范围之外,而不是在期望的点。
下面的代码将显示发生了什么,但随机生成的数据。 任何帮助表示赞赏!
library(ggplot2) library(zoo) library(reshape) library(epitools) ##Generate Price Variables NormalPrices = as.data.frame(matrix(rnorm(300*3, mean=50, sd=10), ncol=3)) rownames(NormalPrices) = paste("sample", 1:300, sep="") colnames(NormalPrices) = paste("Price", 1:3, sep="") ##Assign as Time Series Data datt=ts(NormalPrices,start=c(1990,1), frequency=12) View(datt) time1=time(datt) time1=as.month(time1) time1=time1$dates datt=melt(data.frame(time1, datt),id.vars="time1") ## Plot P=ggplot((datt), aes(time1, value)) + geom_line() +facet_grid(variable ~ .)+ ggtitle("Level Prices") P + geom_vline(xintercept = 2001-04-01,colour="black", linetype = "longdash")+ theme_bw()+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))
I want a time-series plot with a vertical line inserted at some time point. My data spans from 1990 January onward. When I use ggplot2, the vertical line is pushed outside of the range, and not at the desired point.
The code below will show what's happening, but on randomly generated data. Any help is appreciated!
library(ggplot2) library(zoo) library(reshape) library(epitools) ##Generate Price Variables NormalPrices = as.data.frame(matrix(rnorm(300*3, mean=50, sd=10), ncol=3)) rownames(NormalPrices) = paste("sample", 1:300, sep="") colnames(NormalPrices) = paste("Price", 1:3, sep="") ##Assign as Time Series Data datt=ts(NormalPrices,start=c(1990,1), frequency=12) View(datt) time1=time(datt) time1=as.month(time1) time1=time1$dates datt=melt(data.frame(time1, datt),id.vars="time1") ## Plot P=ggplot((datt), aes(time1, value)) + geom_line() +facet_grid(variable ~ .)+ ggtitle("Level Prices") P + geom_vline(xintercept = 2001-04-01,colour="black", linetype = "longdash")+ theme_bw()+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))最满意答案
问题是,你的日期( "2001-04-01" )没有正确转换。 您可以调试此类问题,例如使用
> class("2001-04-01") [1] "character"它不符合你的类从data.frame (这是Date )
>str(datt) 'data.frame': 900 obs. of 3 variables: $ time1 : Date, format: "1990-01-01" "1990-02-01" "1990-03-01" "1990-04-01" ... $ variable: Factor w/ 3 levels "Price1","Price2",..: 1 1 1 1 1 1 1 1 1 1 ... $ value : num 52.1 46 46.3 50.7 64.6 ...尝试将character -Array转换为日期优先:
date <- as.Date("2001-04-01", format="%Y-%m-%d") gg <- ggplot((datt), aes(time1, value)) + geom_line() + facet_grid(variable ~ .)+ ggtitle("Level Prices") gg + geom_vline(xintercept = as.numeric(date), linetype=4) + theme_bw()+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))结果是:
Problem is, that your date ("2001-04-01") isn't converted correctly. You can debug issues like this e.g. using
> class("2001-04-01") [1] "character"which doesn't match your class from the data.frame (which is Date)
>str(datt) 'data.frame': 900 obs. of 3 variables: $ time1 : Date, format: "1990-01-01" "1990-02-01" "1990-03-01" "1990-04-01" ... $ variable: Factor w/ 3 levels "Price1","Price2",..: 1 1 1 1 1 1 1 1 1 1 ... $ value : num 52.1 46 46.3 50.7 64.6 ...try converting the character-Array into a date first:
date <- as.Date("2001-04-01", format="%Y-%m-%d") gg <- ggplot((datt), aes(time1, value)) + geom_line() + facet_grid(variable ~ .)+ ggtitle("Level Prices") gg + geom_vline(xintercept = as.numeric(date), linetype=4) + theme_bw()+ theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"))results in:
更多推荐
发布评论