2012-07-19 55 views
1

我一直在尋找這一段時間,有沒有人看到這個ggplot語法有什麼問題?我收到此錯誤:ggplot不在圖中

Error: Discrete value supplied to continuous scale 

這是Z:

 Month Value 
1 2011-01-01 11 
2 2011-02-01  5 
3 2011-03-01  6 
4 2011-04-01  6 
5 2011-05-01  4 
6 2011-06-01  5 
7 2011-07-01  3 
8 2011-08-01  9 
9 2011-09-01 19 
10 2011-10-01  3 
11 2011-11-01  6 
12 2011-12-01  2 
13 2012-01-01  1 
14 2012-02-01  4 
15 2012-04-01  1 
16 2012-05-01  2 
17 2012-06-01 11 
18 2012-07-01  5 


ggplot(z, aes(Month, Value)) + 
    geom_bar(fill="orange",size=.3) + 
    theme_bw() + scale_x_discrete(name="Date") + 
    scale_y_continuous("Number") + 
    opts(title="Monthly issues", 
     axis.title.x = theme_text(face="bold", colour="#990000"), 
     axis.text.x = theme_text(angle=90), 
     axis.title.y = theme_text(face="bold", colour="#990000", angle=90) 
    ) + 
    geom_smooth(data=z,aes(Month,Value,group=1), method="lm", size=2, color="darkblue") 
+0

它對我來說運行良好,併產生一個情節就好了。檢查'str(z)'並確保沒有'Value'作爲一個因子存儲。 – joran 2012-07-19 14:32:03

+0

這是str(z)的輸出:str(z) 'data.frame':18 obs。 2個變量: $ Month:日期,格式:「2011-01-01」「2011-02-01」「2011-03-01」「2011-04-01」... $ Value:num 11 5 6 6 4 5 3 9 19 3 ... – 2012-07-19 14:33:52

回答

5

啊哈!問題是您的Month列,如您在評論中注意到的那樣存儲爲日期。 R認爲這是一個連續變量,因此與scale_x_discrete錯誤。如果您想使用geom_bar,您應該將其轉換爲as.character的字符。

+0

是的,當我這樣做z $ Month <-as.Character(z $ Month)時,我可以手動運行並查看圖形。問題是,當我嘗試在R腳本中運行這行代碼時,我的圖像沒有填充:png(paste(m,「 - 」,「Issues」,「。png」,sep =「」),height = 700,width = 650),並創建了ggplot命令和dev.off(),png文件,但它們都是空的。 – 2012-07-19 14:51:08

+0

@MikeDude這是一個完全不同的問題,很常見,它是[FAQ](http://cran.at.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics -not-work_003f)。 – joran 2012-07-19 14:54:08

+0

非常感謝。這是一個很棒的產品(as.character)。我終於能夠通過grid.arrange解決圖像生成問題,但不知道更好的方法,但是,這將爲我做。 – 2012-07-19 15:10:45