如何手动更改 ggplot2 中图例中的关键标签
我正在准备要出版的情节。我创建了一个堆积箱形图来显示每组中患有血清阴性病例复杂积累的患者与非血清阴性患者的频率。图例使用数据框中的标签,这些标签适合我们正在从事该项目的人,但不适合发布。我想将这些名称更改为读者更容易理解的名称。
因此,例如运行以下脚本
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")
该代码创建不适合发布的键标签“(10.4,80]”和“(80,150]”。相反,我想要“双负”和“a和/或b的正”我
想我可以返回数据框并进行转换以获取具有正确标签的新变量。 href="https://stackoverflow.com/questions/2339953/how-to-add-custom-series-labels-to-a-legend-in-rs-ggplot">或者我可以重新标记我的因素? 但是,我更愿意在绘图时这样做。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标准方法是使用比例函数来更改组的显示标签。您可以将
ggplot
调用替换为注意,比例尺的标题已合并到
scale_fill_discrete
调用中。如果您愿意,也可以使用轴来完成此操作The standard way is to use the scale functions to change the displayed labels for groups. You can replace your
ggplot
call withNote 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 命令中执行此操作。
还有其他方法吗?
I found a hybrid way of doing it. It does relabel the factor but I do not have to do it in the dataframe. Instead I just do it in the ggplot command.
Are there any other ways?
在 ggplot2 中,您可以使用
scale_fill_manual()
绘图的外观
In ggplot2, you can use
scale_fill_manual()
How the plot looks like