“错误:连续值提供给离散刻度”在默认数据集示例mtcars和ggplot2中 [英] "Error: Continuous value supplied to discrete scale" in default data set example mtcars and ggplot2

查看:215
本文介绍了“错误:连续值提供给离散刻度”在默认数据集示例mtcars和ggplot2中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试复制此处的示例(sthda.com)使用以下代码:

I am trying to replicate the example here (sthda.com) using the following code:

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

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

The example on that web page says that code should produce the following result:

但是当我在R中运行它时,出现以下错误:

But when I run it in R, I get the following error:


错误:连续值提供给离散刻度

"Error: Continuous value supplied to discrete scale"

有人知道此代码有什么问题吗?还是为什么我得到的结果与示例不同?

Does anyone know what could be wrong with this code? Or why I am getting a different result than the example?

如果有人可以运行示例代码并告诉我他们是否遇到相同的错误,我将非常感谢。 / p>

If someone could run the sample code and tell me if they get the same error I would be very grateful.

推荐答案

是的,我可以使用以下代码段对其进行修复:

Yeah, I was able to fix it with the following snippet:

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

这篇关于“错误:连续值提供给离散刻度”在默认数据集示例mtcars和ggplot2中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆