尝试在ggplot2可视化中去除边距

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))
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

谢谢你,Ista!我们能不能把你编译成 ggplot2 的某种人类代码扩展,这样我就可以得到有用的错误提示了? :P - KFunk
哈哈,@KFunk我想不出自动化的方法,只要继续在SO上发布问题,我会尽力帮忙。 - Ista

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接