问题描述
我正在准备要出版的情节.我创建了一个堆叠箱形图,以显示每组中存在一些复杂的血清阴性累积的患者的频率.图例使用了数据框中的标签,这些标签适用于我们正在从事该项目但不适合发布的人.我想将名称更改为读者更容易理解的名称.
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.
所以例如运行以下脚本
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 的肯定".
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.
推荐答案
标准的方法是使用缩放功能来更改组的显示标签.您可以将 ggplot
调用替换为
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"))
请注意,刻度的标题已合并到 scale_fill_discrete
调用中.如果你愿意,你也可以用坐标轴来做这个
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 中图例中的键标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!