r - 默认数据集示例 mtcars 和 ggplot2 中的 "Error: Continuous value supplied to discrete scale"

标签 r ggplot2 colors scatter-plot scatter

我正在尝试复制 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")

该网页上的示例表示代码应产生以下结果:

plot

但是当我在 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/

相关文章:

r - 如何在 ggplot2 的文本注释中包含绘制符号字形?

pdf - 将绘图对象存储在列表中

R 使用值列表作为色标

r - 绘制由 Ranger 函数计算的特征重要性

vb.net - Visual Basic错误BC30456 'RED is not a member of '颜色'

visual-c++ - 从整数值中提取 rgb 颜色分量

r - R堆叠百分比条形图,带有二进制因子和标签的百分比(带ggplot)

python - 从 Python 或 R 中的文件名列表中提取子字符串

r - 询问如何在 R 中使用 ggplot 制作世界热图?

r - 具有相等轴的散点图