如何在R中手动更改ggplot2图例的文本颜色? [英] How to manually change text color of ggplot2 legend in R?

查看:211
本文介绍了如何在R中手动更改ggplot2图例的文本颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能有一种简单的方法来执行此操作,但是我不确定它是什么.我正在尝试使图例中的文本与其旁边的颜色框匹配.我已经尝试了一段时间,但没有找到一种使用element_text函数向图例添加多种颜色的方法.使每个标签都具有相同的颜色我没有问题,但是有没有一种方法可以使每个图例标签具有不同的颜色?

There might be a simple way to do this, but I am not sure what it is. I am trying to make it so that the text in the legend matches up with the color box next to it. I have been trying to do this for a while and have not found a way to use the element_text function to add multiple colors to the legend. I've had no problem making every label the same color, but is there a way to make each legend label a different color?

data<-data.frame(count=c(39,36,19,6), category=c("a","b","c","d"))
data$fraction = data$count / sum(data$count)
data = data[order(data$fraction), ]
data$ymax = cumsum(data$fraction)
data$ymin = c(0, head(data$ymax, n=-1))

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Create Plot
fill <- c("blue3","cyan3","darkgrey","forestgreen")

library(ggplot2)

p1 = ggplot(data, aes(fill=category, ymax=ymax, ymin=ymin, xmax=4, xmin=3.5)) +  
 geom_rect(colour="White") +
 coord_polar(theta="y") +
 scale_fill_manual(values=fill)+
 theme_bw()+
 geom_label(aes(label=paste(data$fraction*100,"%"),x=4,y=
 (ymin+ymax)/2),inherit.aes = F)+
 theme(panel.grid=element_blank())+
 theme(axis.ticks=element_blank()) +     
 xlim(c(0, 4)) +
 theme(axis.text=element_blank()) +
 theme(legend.text=element_text(color=fill,size=12))+
 theme(legend.key.size=unit(2,'lines'))+
 theme(legend.key=element_rect(size=5))+
 labs(title="donut plot")


 print(p1)

推荐答案

对此答案进行了一些修改,

With a couple of modifications to this answer, match-legend-text-color-in-geom-text-to-symbol, you get what you want. But note, the answer uses grid's editing functions.

# Your data and plot
data<-data.frame(count=c(39,36,19,6), category=c("a","b","c","d"))
data$fraction = data$count / sum(data$count)
data = data[order(data$fraction), ]
data$ymax = cumsum(data$fraction)
data$ymin = c(0, head(data$ymax, n=-1))


fill <- c("blue3","cyan3","darkgrey","forestgreen")

library(ggplot2)

p1 = ggplot(data, aes(fill=category, ymax=ymax, ymin=ymin, xmax=4, xmin=3.5)) +
 geom_rect(colour="White") +
 coord_polar(theta="y") +
 scale_fill_manual(values=fill)+
 theme_bw()+
 geom_label(aes(label=paste(data$fraction*100,"%"),x=4,y=
 (ymin+ymax)/2),inherit.aes = F)+
 theme(panel.grid=element_blank())+
 theme(axis.ticks=element_blank()) +     
 xlim(c(0, 4)) +
 theme(axis.text=element_blank()) +
 theme(legend.text=element_text(color=fill,size=12))+
 theme(legend.key.size=unit(2,'lines'))+
 theme(legend.key=element_rect(size=5))+
 labs(title="donut plot")


# Get the ggplot grob
g <- ggplotGrob(p1)

# Check out the grobs
library(grid)
grid.ls(grid.force(g))

浏览清单列表.您要编辑的杂项位于列表的底部,位于指南框"中的杂项中-名称以标签"开头.有四个小样:

Look through the list of grobs. The grobs you want to edit are towards the bottom of the list, in the 'guide-box' set of grobs - with names that begin with "label". There are four grobs:

label-3-3.4-4-4-4
标签-4-3.5-4-5-4
标签-5-3.6-4-6-4
label-6-3.7-4-7-4

label-3-3.4-4-4-4
label-4-3.5-4-5-4
label-5-3.6-4-6-4
label-6-3.7-4-7-4

# Get names of 'label' grobs.
names.grobs <- grid.ls(grid.force(g))$name 
labels <- names.grobs[which(grepl("^label", names.grobs))]

# Edit the 'label' grobs - change their colours
# Use the `editGrob` function
for(i in seq_along(labels)) {
    g <- editGrob(grid.force(g), gPath(labels[i]), grep = TRUE,  
         gp = gpar(col = fill[i]))
}

# Draw it
grid.newpage()
grid.draw(g)

这篇关于如何在R中手动更改ggplot2图例的文本颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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