如何在R中手动更改ggplot2图例的文本颜色?

12

可能有一种简单的方法来实现这个,但我不确定是什么。我试图使图例中的文本与旁边的颜色框匹配。我已经尝试了很长时间,没有找到一种使用element_text函数将多个颜色添加到图例中的方法。我没有问题让每个标签都是相同的颜色,但是否有一种方法可以使每个图例标签具有不同的颜色?

输入图像描述

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)

相关帖子 https://dev59.com/qGAg5IYBdhLWcg3wb6kd - zx8754
我正在尝试查看是否有一种通过legend.text或至少不需要过多代码的方式来实现此目的的方法。 - Sam Miranda
这实际上比人们想象的要复杂得多。上面提供的相关链接很有用。另一个例子是: https://dev59.com/UKHia4cB1Zd3GeqPMgHq#43480246 - Mike H.
这正是我所想的。我希望有一种我不知道的简单方法来完成它。 - Sam Miranda
我尝试按照那些链接中的方法去做,但它并不适用于我正在尝试生成的图形。 - Sam Miranda
2个回答

8

可以通过使用ggtext包而无需编辑grobs来实现这一点。将图例文本标签指定为element_markdown,并将它们包装在使用所需颜色的<span>标记中。

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)
library(ggtext)
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(labels = paste("<span style='color:",
                                   fill,
                                   "'>",
                                   unique(data$category),
                                   "</span>"),
                    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_markdown(size=12))+
  theme(legend.key.size=unit(2,'lines'))+
  theme(legend.key=element_rect(size=5))+
  labs(title="donut plot")

enter image description here

编辑:移除图例的一种方法是使用theme(legend.position = "none")

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(labels = paste("<span style='color:",
                                   fill,
                                   "'>",
                                   unique(data$category),
                                   "</span>"),
                    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.position = "none")+
  labs(title="donut plot")

enter image description here


2
你知道如何删除彩色方块吗?由于文本已经着色,现在我们不需要图例中的方块了。 - Make42
在一个最小的工作示例中,使用legend.text=element_markdown()会更容易理解。 - undefined

7
通过对这个答案的一些修改,您可以得到您想要的效果。请注意,该答案使用了grid的编辑功能。具体内容请参考match-legend-text-color-in-geom-text-to-symbol
# 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))

请查看 grobs 列表。您想要编辑的 grobs 在列表底部,属于“guide-box” grobs 集合中,名称以“label”开头。共有四个 grobs:
- 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)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接