gpt4 book ai didi

r - ggplot 中的颜色(geom_segment)

转载 作者:行者123 更新时间:2023-12-04 09:37:50 24 4
gpt4 key购买 nike

使用 facet_grid 时,如何根据数据中的一个因素为 geom_segments 着色?我的方法失败了,因为颜色的分配是错误的。

这是一些数据:

visual_data=data.frame(Values = 10:1, Words = c("yeah","what","is","up","and","how", "are", "things","for", "you"), group = c("a","b","a","b","a","b","a","b","a","b"), importance=c("#EF2A2A","#EF2A2A", "#E4FA11", "#E4FA11", "#E4FA11", "#E4FA11","#EF2A2A","#EF2A2A","#EF2A2A", "#E4FA11"))

此代码创建一个情节:
graphic=ggplot(visual_data, aes(xend=Values, x=0, y=reorder(Words, Values), yend=reorder(Words, Values))) +
geom_text(aes(x=Values, label=Values, hjust=-0.3), color="#389912",family="sans") +
geom_segment(size=4,colour=visual_data$importance) +
theme(axis.text=element_text(size=10,family="sans"),axis.title=element_text(size=13,face="bold",family="sans"),strip.text.y = element_text(size=12,family="sans"), plot.title=element_text(size=14,face="bold",family="sans")) +
facet_grid(group~., scales = "free")+
theme_bw()
graphic

可以看到的是,例如,“是的”和“什么”不共享相同的条形颜色,尽管它们应该根据我的数据规范。

有没有人解决这个问题?

最佳答案

您需要在 aes() 中输入颜色并添加 scale_colour_identity() :

ggplot(visual_data,
aes(x=0, xend=Values, y=reorder(Words, Values), yend=reorder(Words, Values))) +
geom_text(aes(x=Values, label=Values, hjust=-0.3), color="#389912",family="sans") +
geom_segment(size=4, aes(colour=importance)) +
scale_colour_identity() +
theme(axis.text=element_text(size=10,family="sans"),
axis.title=element_text(size=13,face="bold",family="sans"),
strip.text.y = element_text(size=12,family="sans"),
plot.title=element_text(size=14,face="bold",family="sans")) +
facet_grid(group~., scales = "free")+
theme_bw()

enter image description here

关于r - ggplot 中的颜色(geom_segment),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35985749/

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