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

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

问题描述

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

pre $ 错误单位(tic_pos.c,mm):'x'和'单位'必须有长度> 0

我该怎么办?

<$ p $ b $ ggplot(mat,aes(x = sales,col = type))+
geom_density()+ labels(red_sold,blue_sold,yellow_sold)


解决方案

mat $ type 一个因素?如果不是,那会导致错误。此外,您不能使用标签(...)这种方式。



由于您没有提供任何数据,下面是一个使用内置 mtcars 数据集的示例。

  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 不是一个因素。


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

What should I do?

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

解决方案

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

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

In this example,

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

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

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

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