小编Pra*_*a S的帖子

ggplot2垂直线未插入所需位置

我想要一个时间序列图,在某个时间点插入一条垂直线.我的数据从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"))
Run Code Online (Sandbox Code Playgroud)

情节

plot r ggplot2

4
推荐指数
1
解决办法
334
查看次数

标签 统计

ggplot2 ×1

plot ×1

r ×1