如何更改 ggplot 中的标签(图例)? [英] How to change labels (legends) in ggplot?

查看:28
本文介绍了如何更改 ggplot 中的标签(图例)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下,我想改变ggplot的标签,但是R总是提醒我:

My code is like below, I want to change the label of the ggplot, but R always remind me:

Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0

我该怎么办?

ggplot(mat,aes(x=sales,col=type))+
  geom_density()+labels("red_sold","blue_sold","yellow_sold")

推荐答案

mat$type 是一个因素吗?如果不是,那将导致错误.此外,您不能以这种方式使用 labels(...).

Is mat$type a factor? If not, that will cause the error. Also, you can't use labels(...) this way.

由于您没有提供任何数据,以下是使用内置 mtcars 数据集的示例.

Since you did not provide any data, here's an example using the built-in mtcars dataset.

ggplot(mtcars, aes(x=hp,color=factor(cyl)))+
  geom_density()+
  scale_color_manual(name="Cylinders",
                       labels=c("4 Cylinder","6 Cylinder","8- Cylinder"),
                       values=c("red","green","blue"))

在这个例子中,

ggplot(mtcars, aes(x=hp,color=cyl))+...

会导致与您相同的错误,因为 mtcars$cyl 不是一个因素.

would cause the same error that you are getting, because mtcars$cyl is not a factor.

这篇关于如何更改 ggplot 中的标签(图例)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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