如何手动更改ggplot2中的图例中的键标签 [英] How do I manually change the key labels in a legend in ggplot2

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

问题描述

我正在准备出版剧情。我创建了一个堆积箱图,显示每组患者的血清阴性与非阴性的复杂积累的频率。图例是使用数据框中的标签,这些标签适用于我们正在进行项目但不发布的我们。我想将名称改为读者更快理解的内容。



因此,例如运行以下脚本

  grp<  - gl(n = 4,k = 20,标签= c(组a,组b,组c,组d))
值< - runif(n = 80,min =数据,aes(grp,值,结果)
结果< - cut(值,2)
数据< - data.frame ,fill = results))+ geom_bar()+ xlab(group)
+ ylab(subject of number)+ labs(fill =Serologic response)

该代码创建不适合发布的密钥标签(10.4,80]和(80,150),相反,我希望double负面和积极的一个和/或B。

我想我可以回到数据框并转换为一个新的变量,并带有正确的标签。一个href =https://stackoverflow.com/questions/2339953/how-to-add-custom-series-labels-to-a-legend-in-rs-ggplot>或者我可以重新标记我的因素 a>?但是,我宁愿在绘图时这样做。

解决方案

标准方法是使用比例函数更改组的显示标签。你可以用 $ b

  ggplot(data,aes( grp,fill =结果))+ geom_bar()+ xlab(group)+ 
ylab(受试者人数)+
scale_fill_discrete(血清学反应,
breaks = c ((10.1,79.9),(79.9,150)),
labels = c(double negative,a和/或b为正数))

请注意,比例的标题已被纳入 scale_fill_discrete 调用中。如果你喜欢的话,也可以使用这个轴。

  ggplot(aes(grp,fill = outcome))+ geom_bar() + 
scale_x_discrete(group)+
scale_y_continuous(受试者人数)+
scale_fill_discrete(血清学反应,
breaks = c((10.1,79.9) ,(79.9,150)),
labels = c(双重否定,a和/或b为正数))


I am preparing a plot for publication. I created a stacked box plot to show frequency of patients in each group who were some complicated accumulation of seronegatives versus not. The legend is using the labels from the data frame which are appropriate for us who are working on the project but no for publication. I want to change the names to something more rapidly understood by the reader.

So for instance run the following script

grp <- gl(n=4,k=20,labels=c("group a","group b","group c", "group d"))
value <- runif(n=80, min=10, max=150)
outcome <- cut(value,2)
data <- data.frame(grp,value,outcome)
ggplot(data, aes(grp, fill=outcome)) + geom_bar() +xlab("group") 
             +ylab("number of subjects") + labs(fill="Serologic response")

That code creates key labels "(10.4,80]" and "(80,150]" which are not suitable for publication. Instead I would want "double negative" and "positive for a and/or b".

I guess I could go back to the dataframe and transform to get a new variable with the correct labeling. Or I could just relabel my factor? However, I would prefer to do it at the time of plotting.

解决方案

The standard way is to use the scale functions to change the displayed labels for groups. You can replace your ggplot call with

ggplot(data, aes(grp, fill=outcome)) + geom_bar() +xlab("group") +
  ylab("number of subjects") + 
  scale_fill_discrete("Serologic response", 
                      breaks=c("(10.1,79.9]","(79.9,150]"), 
                      labels=c("double negative", "positive for a and/or b"))

Note that the scale's title has been incorporated into the scale_fill_discrete call. You can do this with the axes too, if you like

ggplot(data, aes(grp, fill=outcome)) + geom_bar() +
  scale_x_discrete("group") +
  scale_y_continuous("number of subjects") + 
  scale_fill_discrete("Serologic response", 
                      breaks=c("(10.1,79.9]","(79.9,150]"), 
                      labels=c("double negative", "positive for a and/or b"))

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

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