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

zzzyeukh  于 2023-04-27  发布在  其他
关注(0)|答案(2)|浏览(170)

可能有一个简单的方法可以做到这一点,但我不确定它是什么。我试图使它,以便在图例中的文本匹配旁边的颜色框。我一直在尝试这样做了一段时间,并没有找到一种方法来使用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)
xzlaal3s

xzlaal3s1#

对这个答案做了一些修改,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))

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

vpfxa7rd

vpfxa7rd2#

使用ggtext包可以在不编辑grobs的情况下实现这一点。将图例文本标签指定为element_markdown,并将它们 Package 在使用所需颜色的<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")

**编辑:**删除图例的一种方法是使用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")

相关问题