我试图缩小条形之间的距离,但是,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)
任何帮助将不胜感激! 谢谢,
答案 0 :(得分:1)
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
的宽度参数(已经设置为0.6)。
这是一个很好的可重现的示例,但我鼓励您将来的Stack Overflow问题更多 minimal 。您拥有的所有theme
次呼叫都与此问题无关,尽管加载了5个套餐,但您实际使用的唯一一个是ggplot2
。下面有一些简单的代码。我已经删除了position_dodge
,因为它没有改变默认设置,我将scale_x_continuous
移到fill
并添加了{aes()
。 1}} ---这就是我喜欢的风格,我摆脱了主题定制,因为它的16行代码与问题的清晰度无关。
scale_fill_manual
作为调整宽度的一个例子(虽然这是过度绘图没有替换,所以如果你想让宽度更小,则编辑原始的f3定义。)
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
另外两个注释: