gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:18:22 25 4
gpt4 key购买 nike

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

enter image description here

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)

最佳答案

通过对该答案进行一些修改match-legend-text-color-in-geom-text-to-symbol,您将获得所需的内容。但请注意,答案使用grid的编辑功能。

# 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))

浏览 list 列表。您要编辑的杂项位于列表的底部,即“指南框”中的杂项-名称以“标签”开头。有四个小技巧:

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

关于r - 如何在R中手动更改ggplot2图例的文本颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43661251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com