如何手动更改 ggplot2 中图例中的关键标签

发布于 2024-12-03 08:33:37 字数 737 浏览 1 评论 0原文

我正在准备要出版的情节。我创建了一个堆积箱形图来显示每组中患有血清阴性病例复杂积累的患者与非血清阴性患者的频率。图例使用数据框中的标签,这些标签适合我们正在从事该项目的人,但不适合发布。我想将这些名称更改为读者更容易理解的名称。

因此,例如运行以下脚本

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反差帅 2024-12-10 08:33:37

标准方法是使用比例函数来更改组的显示标签。您可以将 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"))

注意,比例尺的标题已合并到 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"))

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"))
极度宠爱 2024-12-10 08:33:37

我找到了一种混合的方法。它确实重新标记了该因素,但我不必在数据框中执行此操作。相反,我只是在 ggplot 命令中执行此操作。

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

还有其他方法吗?

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.

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?

你怎么敢 2024-12-10 08:33:37

在 ggplot2 中,您可以使用 scale_fill_manual()

colors <- c("red","blue")
outcome_labels <- c("low","high")

ggplot(data, aes(grp, fill=outcome)) + 
  geom_bar() +
  scale_fill_manual(values = colors,labels = outcome_labels) +
  xlab("group") +ylab("number of subjects") + labs(fill="Serologic response")

绘图的外观

在此处输入图像描述

In ggplot2, you can use scale_fill_manual()

colors <- c("red","blue")
outcome_labels <- c("low","high")

ggplot(data, aes(grp, fill=outcome)) + 
  geom_bar() +
  scale_fill_manual(values = colors,labels = outcome_labels) +
  xlab("group") +ylab("number of subjects") + labs(fill="Serologic response")

How the plot looks like

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文