ggplot2每个图例标签的不同文字颜色 [英] ggplot2 different text colors for each legend label

查看:2055
本文介绍了ggplot2每个图例标签的不同文字颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让图例标签的文字颜色与其相关的填充/线条颜色相匹配。理想情况下,我想将标签颜色映射到美学上,但在这一点上,我很乐意手动指定颜色。以下是使用内置PlantGrowth数据集的一些测试代码,其中包括我尝试手动指定标签颜色的操作:

  ggplot( data = PlantGrowth,aes(x = group,y = weight,fill = group))+ 
geom_boxplot()+
scale_fill_discrete(guide = guide_legend(label.theme = element_text(angle = 0,
size = 9,
color = c(red,
green,
blue))))

运行此代码时,图例标签全部使用我指定的第一种颜色(红色)。相反,我希望每个图例标签使用不同的颜色。这种类型的操作目前可能在ggplot2中吗?

首先,我试着尝试不同版本的当前尝试使用主题(legend.text = element_text(color = c(red,blue,green)))但以上都没有工作,所以我有去 gtable s。



你想要什么是可能的,但要求你非常熟悉 gtable s和 gtable 包。这看起来非常混乱,因为有很多嵌套列表,我们最终想要修改的对象位于底部。只需要忍耐一下:

pre $ library $ g $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
pGrob < - ggplotGrob(ggplot(data = PlantGrowth,aes(x = group,y = weight,fill = group))+
geom_boxplot()+
scale_fill_discrete(guide = guide_legend(label .theme = element_text(angle = 0,
size = 9))))



gb< - 其中(grepl(guide-box, (grepl(guides,pGrob $ grobs [[gb]] $ layout $ name))
label_text< - which(grepl(标签,pGrob $ grobs [[gb]] $ grobs [[gb2]] $ layout $ name))
pGrob $ grobs [[gb]] $ grobs [[gb2]] $ grobs [label_text]< - mapply(FUN = function(x,y){x $ gp $ col <-y; return(x)},
x = pGrob $ grobs [[gb]] $ grobs [[gb2]] $ grobs (标签文本),
y = c(红色,绿色,蓝色),SIMPLIFY = FALSE)

grid.draw(pGrob)



创建 gb gb2 label_text 都被设计为动态地达到我们想要修改的底层。一旦我们有了获取对象的路径,我们就可以使用 mapply 修改它们,并将 col 更改为你想要的矢量。


I'm attempting to get the text color of legend labels to match their associated fill/line colors. Ideally, I'd like to map the label color to an aesthetic, but at this point I'd be just as happy to manually specify the colors. Here is some test code using the built-in PlantGrowth dataset, which includes my attempt to manually specify the label colors:

ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) +
    geom_boxplot() +
    scale_fill_discrete(guide=guide_legend(label.theme=element_text(angle=0,
                                                                    size=9,
                                                                    color=c("red",
                                                                            "green",
                                                                            "blue"))))

When I run this code the legend labels all use the first color I specify (red). Instead, I want each legend label to use a different color. Is this type of operation currently possible in ggplot2?

解决方案

First off, I tried experimenting with different versions of your current attempt as well as using theme(legend.text = element_text(color = c("red","blue","green"))) but none of the above worked so I had to go to gtables.

What you want is possible, but requires you to be very familiar with gtables and the gtable package. This is going to look very messy because there are a lot of nested lists and the object we ultimately want to modify is at the bottom of one. Just bear with me:

library(ggplot2)
library(gtable)
library(grid)
pGrob <- ggplotGrob(ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) +
  geom_boxplot() +
  scale_fill_discrete(guide=guide_legend(label.theme=element_text(angle=0,
                                                                  size=9))))



gb <- which(grepl("guide-box", pGrob$layout$name))
gb2 <- which(grepl("guides", pGrob$grobs[[gb]]$layout$name))
label_text <- which(grepl("label",pGrob$grobs[[gb]]$grobs[[gb2]]$layout$name))
pGrob$grobs[[gb]]$grobs[[gb2]]$grobs[label_text] <- mapply(FUN = function(x, y) {x$gp$col <- y; return(x)},
                                                           x =   pGrob$grobs[[gb]]$grobs[[gb2]]$grobs[label_text],
                                                           y =   c("red", "green", "blue"), SIMPLIFY = FALSE)

grid.draw(pGrob)

The first 3 lines that create gb, gb2, and label_text are all designed to dynamically get to that bottom level we want to modify. Once we have our path to get the objects, we can then modify them using the mapply and change the col to be the vector you want.

这篇关于ggplot2每个图例标签的不同文字颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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