ggplot2垂直线未插入所需位置(ggplot2 vertical line not inserted at the desired position)

编程入门 行业动态 更新时间:2024-10-11 13:22:08
ggplot2垂直线未插入所需位置(ggplot2 vertical line not inserted at the desired position)

我想要在某个时间点插入垂直线的时间序列图。 我的数据涵盖了从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:

enter image description here

更多推荐

本文发布于:2023-07-14 19:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106716.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:垂直线   所需   位置   vertical   desired

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!