![](/img/trans.png)
[英]Adjusting geom_bar (position=“dodge”) in ggplot
[英]ggplot geom_bar with position_dodge is NOT adjusting the distance between bars
我正在尝试将条形之间的距离缩小一些,但是,position_dodge似乎在调整条形之间的距离上不起作用。
下面是我的代码示例,其中两个输出的宽度不同,一个为0.7,另一个为5,但小节之间的宽度不受影响...:
library(ggplot2)
library(grid)
library(ggthemes)
library(scales)
library(gridExtra)
variables = c('a','b','c','d','e')
values = c(0.2,0.4,0.6,0.8,1.0)
std = c(0.05,0.06,0.03,0.08,0.09)
Data = data.frame(variables, values, std)
f3 = ggplot(data = Data, aes(x = variables, y = values, group = variables) ) +
geom_bar(stat='identity',width=0.6,position=position_dodge(width = 0.7),fill=c('#FF7F0E','#2CA02C','#D62728', '#00008B', '#B23AEE')) +
coord_flip() +
geom_errorbar(aes(ymin=values-std, ymax=values+std),
width=.2,size=0.3) +
scale_y_continuous("Variable Importance", expand = c(0,0),limits = c(0, 1.1), breaks=seq(0, 1.1, by = 0.1)) + # rescale Y axis slightly
scale_x_discrete("Variables", limits = c('a','b',"c","d","e" )) +
theme_bw() + # make the theme black-and-white rather than grey (do this before font changes, or it overrides them)
theme(
line = element_line(size=0.3),
plot.title = element_blank(), # use theme_get() to see available options
axis.title.x = element_text(family='sans',size=13),
axis.title.y = element_text(family='sans',size=13, angle=90),
axis.text.x = element_text(family='sans',vjust=0.4,size=11),
axis.text.y = element_text(family='sans',size=11),
panel.grid.major = element_blank(), # switch off major gridlines
panel.grid.minor = element_blank(), # switch off minor gridlines
legend.position = 'none', # manually position the legend (numbers being from 0,0 at bottom left of whole plot to 1,1 at top right)
legend.title = element_blank(), # switch off the legend title
legend.text = element_blank(),
legend.key.size = unit(1.5, "lines"),
legend.key = element_blank(), # switch off the rectangle around symbols in the legend
panel.border=element_blank(),
axis.line=element_line(size=0.3)
)
plot(f3)
任何帮助将不胜感激! 谢谢,
position_dodge
当你在一个位置上有多个酒吧使用,就像如果你有几间酒吧绘制在"a"
上的“变量”轴。 参见例如?position_dodge
的示例:
ggplot(mtcars, aes(x=factor(cyl), fill=factor(vs))) +
geom_bar(position="dodge")
在这里,x轴由cyl
定义,但填充颜色由vs
定义,因此我们将在每个x位置使用填充颜色来区分多个条形。 位置闪避表示将条形图彼此相邻放置(相对于位于彼此顶部position = "identity"
或堆叠position = "stack"
)。
每个位置上只有一个条,因此position_dodge
不执行任何操作。 完全摆脱您的position_dodge
并使用geom_bar
的width参数(您已经将其设置为0.6)。
这是一个很好的可重现的示例,但我鼓励您将来使“堆栈溢出”问题更小 。 您拥有的所有theme
调用均与该问题无关,尽管加载了5个软件包,但实际使用的唯一一个是ggplot2
。 下面是一些简化的代码。 我按照讨论的方法删除了position_dodge
,摆脱了scale_x_continuous
因为它并没有改变默认值,我将fill
移入了aes()
并添加了scale_fill_manual
---这只是我喜欢的样式,我摆脱了主题自定义的内容,因为它的16行代码与问题的清楚程度无关。
f3 = ggplot(data = Data,
aes(x = variables, y = values,
group = variables, fill = variables)) +
geom_bar(stat = 'identity',
width = 0.6) + ## adjust this width
coord_flip() +
geom_errorbar(aes(ymin = values - std, ymax = values + std),
width = .2, size = 0.3) +
scale_y_continuous("Variable Importance",
expand = c(0,0),
limits = c(0, 1.1),
breaks = seq(0, 1.1, by = 0.1)) +
scale_fill_manual(values = c('#FF7F0E','#2CA02C','#D62728', '#00008B', '#B23AEE'),
guide = FALSE) +
theme_bw()
f3
作为调整宽度的示例(尽管这是过度绘图而不是替换,所以如果要减小宽度,请编辑原始的f3定义)。
f3 + geom_bar(stat = "identity", width = 0.9)
另外两个注意事项:
您对主题的大部分修改似乎都是在复制theme_classic()
---也是ggplot2
内置的。 使用theme_classic()
而不是theme_bw()
可以使您开始更接近目标。
f3 + theme_classic()
正如罗曼在评论中指出的那样, geom_pointrange
可能更适合此数据。 条形顶部的置信区间通常很难看到和比较。 这是一个例子:
ggplot(data = Data, aes(x = variables, y = values, color = variables)) + geom_pointrange(aes(ymin = values - std, ymax = values + std), size = 1) + scale_y_continuous("Variable Importance", expand = c(0,0), limits = c(0, 1.1), breaks = seq(0, 1.1, by = 0.1)) + scale_color_manual(values = c('#FF7F0E','#2CA02C','#D62728', '#00008B', '#B23AEE'), guide = FALSE) + coord_flip() + theme_classic()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.