2013-03-16 88 views
3

功能scale_y_continuous(expand=c(0,0))去除利潤率是給我的錯誤:試圖在GGPLOT2可視化

Error: Discrete value supplied to continuous scale 

我真不明白這是什麼意思。該功能被稱爲scale_y_continuous,而不是scale_y_discreteHere's my data

require(ggplot2) # ggplot 
gp <- read.csv("Retail_Gas_Prices.csv") 
gp$Date <- as.Date(substr(gp$Date, 1, 10), "%m/%d/%Y") 
gp_melted <- melt(gp, id = "Date") 

gas_ml_plot <- ggplot(subset(gp_melted, variable != "Weekly.US"), 
       aes(Date, value, colour = variable)) + 
       geom_line() + ggtitle("Retail Gas Prices In The US") + 
       theme(axis.title.x = element_blank()) + 
       ylab("Cost in Dollars") + 
       theme(axis.ticks = element_blank()) + 
       labs(colour = "US Region") + 
       scale_color_discrete(labels = c("East Coast", "Midwest", 
       "Gulf Coast", "Rocky Mountain", "West Coast")) + 
       theme(legend.background = element_blank()) + 
       theme(legend.position = c(0, 1)) + 
       theme(legend.justification = c(0, 1)) + 
       scale_y_continuous(expand = c(0, 0)) + 
       scale_x_discrete(expand = c(0, 0)) 

回答

4

該錯誤消息是錯誤的,所述問題是日期對象傳遞到一個離散的x縮放,該離散值到一個連續的尺度提供。解決方案是使用正確的x尺度;由於x變量是日期,因此使用scale_x_date(expand = c(0, 0))代替scale_x_discrete(expand = c(0, 0))

我已在https://github.com/hadley/ggplot2/issues/783

+0

提交了一個描述此問題的錯誤報告謝謝Ista!我們可以將你編譯成ggplot2的某種人類代碼擴展,以便我可以得到有用的錯誤嗎? :P – KFunk 2013-03-17 00:27:22

+0

哈哈,@KFunk我想不出讓它自動化的方法,只是繼續張貼在SO上,我會盡我所能。 – Ista 2013-03-17 00:30:13