试图用geom_segment绘制一个箭头

时间:2014-04-07 21:47:56

标签: r ggplot2

我有这个数据框,我想使用geom_segment,但是我收到了这个错误:

Error: Invalid input: date_trans works with objects of class Date only

我将矢量转换为as.Date,任何想法在这里可能出错?

structure(list(Date = structure(c(16130, 16191, 16252, 16314, 
16375, 16436, 16495, 16556, 16617, 16679, 16740, 16801), class = "Date"), 
    Total_Used = c(381.62, 389.25, 400.93, 412.96, 425.35, 438.11, 
    451.25, 464.79, 478.73, 493.09, 507.89, 523.12), Capacity = c(448L, 
    448L, 448L, 448L, 448L, 448L, 448L, 448L, 448L, 448L, 448L, 
    448L), Percent_Used = structure(c(7L, 8L, 9L, 10L, 11L, 12L, 
    1L, 2L, 3L, 4L, 5L, 6L), .Label = c("100.73%", "103.75%", 
    "106.86%", "110.07%", "113.37%", "116.77%", "85.18%", "86.89%", 
    "89.49%", "92.18%", "94.94%", "97.79%"), class = "factor"), 
    Login = c("489,123,708", "498,906,182", "508,884,306", "519,061,992", 
    "529,443,232", "540,032,096", "550,832,738", "561,849,393", 
    "573,086,381", "584,548,109", "596,239,071", "608,163,852"
    )), .Names = c("Date", "Total_Used", "Capacity", "Percent_Used", 
"Login"), row.names = c(NA, -12L), class = "data.frame")

library(scales)
library(ggplot2)

ggplot(dc) + theme_gray()+
geom_bar(aes(Date,Total_Used),stat="identity", position="stack", fill="#38B0DE") + 
geom_line(data=dc, aes(Date,Total_Used), colour="#2F4F4F", size=1.5) + 
geom_line(aes(Date, Capacity), colour="red", size=1.5)+
geom_text(data=dc, aes(Date,Total_Used,label=Login),col="brown", hjust=2,vjust=0,size=4, angle=90)+
scale_x_date(breaks = "2 month", labels=date_format("%b-%y")) +
ylab("Total PC Used")+ggtitle("Single CDC Total Pysical PC and Projections")+
theme(axis.title.x = element_text(face="bold", colour="#990000"),axis.title.y = element_text(face="bold", colour="#990000"),axis.text.x  = element_text(angle=60, hjust=0, vjust=0),legend.position = "bottom", legend.direction='vertical')+
annotate("text", x=as.Date(c("2014-05-01")),y=410,label="PC Projections",size=4,colour="#2F4F4F",angle=10)+
annotate("text", x=as.Date(c("2014-05-01")),y=470,label="PC Capacity",size=4,colour="red")+
geom_segment(aes(x = as.Date(c("2015-03-01")), y = 448, xend = 3.5, yend = 25), arrow = arrow(length = unit(0.5, "cm")))

1 个答案:

答案 0 :(得分:4)

错误来自xned参数。您将起点(x)指定为日期,而将终点(xend)指定为数字。请尝试以下

geom_segment(aes(x = as.Date(c("2015-03-01")), y = 448, xend = as.Date(c("2015-03-01")), yend = 25), arrow = arrow(length = unit(0.5, "cm")))

enter image description here

相关问题