"Error: Continuous value supplied to discrete scale" in default data set example mtcars and ggplot2

"Error: Continuous value supplied to discrete scale" in default data set example mtcars and ggplot2

我正在尝试使用以下代码复制 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")

as.factor 让它工作

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