我正在尝试复制 the example here (sthda.com)使用以下代码:
# Change point shapes and colors manually
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
scale_shape_manual(values=c(3, 16, 17))+
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
theme(legend.position="top")
该网页上的示例表示代码应产生以下结果:
但是当我在 R 中运行它时,出现以下错误:
"Error: Continuous value supplied to discrete scale"
有谁知道这段代码可能有什么问题?或者为什么我得到的结果与示例不同?
如果有人可以运行示例代码并告诉我他们是否遇到相同的错误,我将不胜感激。
最佳答案
是的,我能够通过将颜色和形状美学转换为因素来修复它:
ggplot(mtcars, aes(x=wt, y=mpg, color=as.factor(cyl), shape=as.factor(cyl))) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
scale_shape_manual(values=c(3, 16, 17))+
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
theme(legend.position="top")
关于r - 默认数据集示例 mtcars 和 ggplot2 中的 "Error: Continuous value supplied to discrete scale",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43359050/