控制position_dodge的层次 [英] control hierachy of position_dodge

查看:671
本文介绍了控制position_dodge的层次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ggplot有没有办法控制哪个元素在另一个元素的前面被绘制出来(mtcars,aes(x = factor(cyl),fill = factor(vs)))+ 
geom_bar(position = position_dodge(width = - 0.5))
pre>



在红色的前面。颠倒顺序而不破解alpha值是否合适?

解决方案

您的控制权限有限。使用因子水平我们可以控制i) fill 颜色排序和ii) position_dodge 的顺序使用<$ c $

 

p1 < - ggplot(mtcars,aes(x =因子(cyl),fill =因子(vs,0:1),group =因子(vs,0:1)))+ $ b $ (mtcars,aes(x =因子(cyl),fill =因子(vs,0:1),组) =因子(vs,1:0)))+
geom_bar(position = position_dodge(width = - 0.5))

p3 < - ggplot(mtcars,aes(x = factor (= 1,0),组=因子(vs,0:1)))+
geom_bar(position = position_dodge(width = - 0.5))
$ b $ (x =因子(cyl),fill =因子(vs,1:0),组=因子(vs,1:0)))+
geom_bar(position = position_dodge(width = - 0.5))

library(cowplot)
plot_grid(p1,p2,p3,p4,align ='hv')



因此,似乎只有躲避顺序很重要。至少在dev版本中,右侧栏总是绘制在左侧栏的前面。


Is there a way to control which element is plotted in front of the other if one uses dodged bar charts.

 ggplot(mtcars, aes(x=factor(cyl), fill=factor(vs))) +
    geom_bar(position= position_dodge (width = - 0.5))

In this example the blue bars are plotted in front of the red ones. Is it possiple to reverse the order without hacking alpha values?

解决方案

Your control here is limited. Using factor levels we can control i) the fill color ordering and ii) the ordering of position_dodge using group.

Here are the four options:

p1 <- ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs, 0:1), group = factor(vs, 0:1))) +
  geom_bar(position = position_dodge(width = - 0.5))

p2 <- ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs, 0:1), group = factor(vs, 1:0))) +
  geom_bar(position = position_dodge(width = - 0.5))

p3 <- ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs, 1:0), group = factor(vs, 0:1))) +
  geom_bar(position = position_dodge(width = - 0.5))

p4 <- ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs, 1:0), group = factor(vs, 1:0))) +
  geom_bar(position = position_dodge(width = - 0.5))

library(cowplot)
plot_grid(p1, p2, p3, p4, align = 'hv')

So it seems only the dodging order is important. In the dev version at least, the right bar is always plotted in front of the left bar.

这篇关于控制position_dodge的层次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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