ggplot中的颜色(geom_segment) [英] Colours in ggplot (geom_segment)

查看:1532
本文介绍了ggplot中的颜色(geom_segment)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



以下是一些数据:

  visual_data = data.frame(Values = 10:1,Words = c(是,what,is,up,and,how,are,things,for,you group = c(a,b,a,b,a,b,a,b,a,b)重要性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,color = visual_data $ importance)+
主题(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 =免费)+
theme_bw()
graphic

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



有没有人可以解决这个问题?

aes()中并添加 scale_colour_identity() code $:

pre $ g $ p $ $ g $ p $重新排序(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(color = 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_gr id(group〜。,scales =free)+
theme_bw()

< a href =https://i.stack.imgur.com/x8gE8.png =nofollow noreferrer>


how can color geom_segments according to a factor in the data when facet_grid is used? My approach fails, as the assignment of colors is wrong.

Here's some data:

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

This code creates a plot:

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

What can be seen is that "yeah" and "what", for instance, do not share the same bar color, although they should according to my data specification.

Has anyone a solution to this?

解决方案

You need to put colour in aes() and add 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()

这篇关于ggplot中的颜色(geom_segment)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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