2013-02-15 90 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