mtcars ggplot不知道如何处理数字类 [英] mtcars ggplot doesn't know how to deal with class numeric

查看:74
本文介绍了mtcars ggplot不知道如何处理数字类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想使用mtcars和ggplot绘制一个简单的图:

I am just trying to make a simple plot using mtcars and ggplot:

ggplot(data=mtcars, aes(x=mpg,y=hp))+geom_line(mpg,hp,col=cyl)

但是我得到了错误:

ggplot不知道如何处理数字类

ggplot doesnt know how to deal with class numeric

这是怎么回事?

推荐答案

我的建议是在一个地方指定所有美学,并将cyl强制为factor以将颜色编码作为类别变量.

My suggestion is to specify all aesthetics in one place and to coerce cyl to factor for colour coding as categorial variable.

ggplot(data=mtcars, aes(x=mpg, y=hp, colour=factor(cyl))) + geom_line()

将创建

这篇关于mtcars ggplot不知道如何处理数字类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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