3

该功能scale_y_continuous(expand=c(0,0))给了我错误:

Error: Discrete value supplied to continuous scale

我真的不明白这是什么意思。该函数被调用scale_y_continuous,而不是scale_y_discrete这是我的数据

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

1 回答 1

4

错误消息是错误的,因为问题是 Date 对象被传递给离散的 x 比例,而不是离散值被提供给连续比例。解决方案是使用正确的 x 比例;由于 x 变量是一个日期,请使用scale_x_date(expand = c(0, 0)).scale_x_discrete(expand = c(0, 0))

我已在https://github.com/hadley/ggplot2/issues/783提交了描述此问题的错误报告

于 2013-03-16T23:54:55.377 回答