如何手动更改ggplot2中的一个传奇中的关键标签?

时间:2023-01-09 04:19:28

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

该代码创建不适合发布的关键标签“(10.4,80)”和“(80150)”。相反,我想要“双重否定”和“a和/或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.

我想我可以回到dataframe和transform来得到一个新的变量和正确的标签。或者我可以重新标记因子?但是,我更喜欢在绘图的时候进行。

2 个解决方案

#1


35  

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

标准的方法是使用scale函数来更改组的显示标签。您可以将ggplot调用替换为

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

注意,scale的标题已经包含在scale_fill_discrete调用中。你也可以用坐标轴来做,如果你喜欢的话

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

#2


20  

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.

我找到了一种混合的方法。它确实重新标记了因子,但是我不需要在dataframe中这样做。而是在ggplot命令中执行。

ggplot(data, aes(grp, fill=factor(outcome,labels=c("low","high")))) + 
  geom_bar() +xlab("group") +ylab("number of subjects") +
   labs(fill="Serologic response")

Are there any other ways?

还有其他方法吗?

#1


35  

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

标准的方法是使用scale函数来更改组的显示标签。您可以将ggplot调用替换为

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

注意,scale的标题已经包含在scale_fill_discrete调用中。你也可以用坐标轴来做,如果你喜欢的话

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

#2


20  

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.

我找到了一种混合的方法。它确实重新标记了因子,但是我不需要在dataframe中这样做。而是在ggplot命令中执行。

ggplot(data, aes(grp, fill=factor(outcome,labels=c("low","high")))) + 
  geom_bar() +xlab("group") +ylab("number of subjects") +
   labs(fill="Serologic response")

Are there any other ways?

还有其他方法吗?