问题描述
如果人使用躲避的条形图,是否有一种方法可以控制将哪个元素绘制在另一个元素的前面.
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))
在此示例中,蓝色条形图绘制在红色条形图之前.是否有可能在不修改Alpha值的情况下撤销订单?
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?
推荐答案
您在这里的控制受到限制.使用因子级别,我们可以控制i)fill
的颜色排序和ii)使用group
的position_dodge
排序.
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
.
以下是四个选项:
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的层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!