如何获得班级日期的 x 轴的垂直 geom_vline ?
尽管我在 POSIXct
和 geom_vline
的 google 群组中找到了 Hadley 的帖子,但我无法完成它。我有一个时间序列,想画一条垂直线,例如 1998 年、2005 年和 2010 年。我尝试使用 ggplot 和 qplot 语法,但我仍然要么根本看不到垂直线,要么在第一个垂直网格处绘制垂直线,并且整个系列被移动右边有点奇怪。
gg <- ggplot(data=mydata,aes(y=somevalues,x=datefield,color=category)) +
layer(geom="line")
gg + geom_vline(xintercept=mydata$datefield[120],linetype=4)
# returns just the time series plot I had before,
# interestingly the legend contains dotted vertical lines
我的日期字段的格式为“1993-07-01”,属于 Date
类。
Even though I found Hadley's post in the google group on POSIXct
and geom_vline
, I could not get it done. I have a time series from and would like to draw a vertical line for years 1998, 2005 and 2010 for example. I tried with ggplot
and qplot
syntax, but still I either see no vertical line at all or the vertical line is drawn at the very first vertical grid and the whole series is shifted somewhat strangely to the right.
gg <- ggplot(data=mydata,aes(y=somevalues,x=datefield,color=category)) +
layer(geom="line")
gg + geom_vline(xintercept=mydata$datefield[120],linetype=4)
# returns just the time series plot I had before,
# interestingly the legend contains dotted vertical lines
My date field has format "1993-07-01" and is of class Date
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试
as.numeric(mydata$datefield[120])
:一个简单的测试示例:
Try
as.numeric(mydata$datefield[120])
:A simple test example:
如果您希望线条保持在原位,无论您是否日期位于第 120 行。
You could also do
geom_vline(xintercept = as.numeric(as.Date("2015-01-01")), linetype=4)
if you want the line to stay in place whether or not your date is in the 120th row.根据您将“日期”列传递给
aes
的方式,as.numeric
或as.POSIXct
可以工作:library(ggplot2 )
使用
aes(as.Date(Dates),...)
使用
aes(Dates, ...)
Depending on how you pass your "Dates" column to
aes
, eitheras.numeric
oras.POSIXct
works:library(ggplot2)
using
aes(as.Date(Dates),...)
using
aes(Dates, ...)
as.numeric
对我有用as.numeric
works to me