2013-02-15 88 views
2

以下数据显示了我的项目,时间范围及其阶段。我想使用下面显示的R ggplot()代码可视化这些数据。但是,正如我们下面所看到的,从数据中推断月份时会出现错误。我想使用Months的名称作为x轴标签。此外,我需要打印除矩形框外的项目名称。请帮助我。谢谢。R中月份名称的错误

> temp 
    projects  starts  ends order  Phase 
     A 2013-02-15 2013-03-15  1 Research 
     A 2013-03-16 2013-04-15  1 Prototype 
     B 2013-04-07 2013-04-30  2 Research 
    B 2013-05-01 2013-08-30  2 Prototype 
     C 2013-05-01 2013-07-30  3 Research 
     D 2013-05-01 2013-07-30  4 Research 


> a = ggplot(temp, aes(xmin = starts, xmax = ends, ymin = order, ymax = order+0.5)) + geom_rect(aes(fill=Phase), color="black") + theme_bw() 
> b = a + geom_text(aes(x= starts + (ends-starts)/2 ,y=order+0.25, label=projects)) 
> b 
Error in unit(x, default.units) : 'x' and 'units' must have length > 0 
In addition: Warning messages: 
1: In Ops.factor(ends, starts) : - not meaningful for factors 
2: In Ops.factor(starts, (ends - starts)/2) : + not meaningful for factors 
3: Removed 6 rows containing missing values (geom_text). 

请同时参阅R.版本

> version 
       _        
platform  i686-pc-linux-gnu    
arch   i686       
os    linux-gnu      
system   i686, linux-gnu    
status          
major   2        
minor   15.2       
year   2012       
month   10       
day   26       
svn rev  61015       
language  R        
version.string R version 2.15.2 (2012-10-26) 

回答

4

尝试转换startsendsDate

temp$starts <- as.Date(temp$starts) 
temp$ends <- as.Date(temp$ends) 

如果不工作,你可能需要使用dput(temp)和粘贴那到你的问题。


复制粘贴+ OP的数据,转换为日期,然后使用OP代码 Using the ggplot2 code above

+0

你能告诉我怎样才能在矩形,而不是内部的顶部打印项目的名称。此外,我想删除重复的名称。 – samarasa 2013-02-15 22:14:38

+0

您可能想要在适当的坐标处绘制文本。不过,我会将其作为一个新问题发布。 – 2013-02-15 22:26:52