ggplot geom_bar with position_dodge 不调整条形之间的距离
ggplot geom_bar with position_dodge is NOT adjusting the distance between bars
我正在尝试缩小条之间的距离,但是,position_dodge 似乎无法调整条之间的距离。
下面是我的代码示例,有两个不同宽度的输出,一个是 0.7,一个是 5,但是条之间的宽度不受影响...:[=15=]
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
当您在一个位置有多个条时使用,就像您在 "Variables" 轴上的 "a"
处绘制了多个条一样。例如,参见 ?position_dodge
:
中的示例
ggplot(mtcars, aes(x=factor(cyl), fill=factor(vs))) +
geom_bar(position="dodge")
这里,x 轴由 cyl
定义,但填充颜色由 vs
定义,因此我们将在每个 x 位置有多个由填充颜色区分的条形。 position dodge 表示将条形图彼此相邻放置(而不是彼此重叠 position = "identity"
或堆叠 position = "stack"
)。
每个位置只有一个柱状图,因此 position_dodge
什么都不做。完全摆脱你的 position_dodge
并使用 geom_bar
的宽度参数(你已经设置为 0.6)。
这是一个很好的可重现示例,但我鼓励您以后将 Stack Overflow 问题做得更最少。您所有的 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
作为调整宽度的例子(虽然这是overplotting而不是替换,所以如果你想让宽度变小编辑原来的f3定义)。
f3 + geom_bar(stat = "identity", width = 0.9)
另外两个注意事项:
您的大部分主题修改似乎都在重复 theme_classic()
--- 这也内置于 ggplot2
。使用 theme_classic()
而不是 theme_bw()
可能会让您离目标更近一步。
f3 + theme_classic()
正如 Roman 在评论中指出的那样,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()
我正在尝试缩小条之间的距离,但是,position_dodge 似乎无法调整条之间的距离。
下面是我的代码示例,有两个不同宽度的输出,一个是 0.7,一个是 5,但是条之间的宽度不受影响...:[=15=]
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
当您在一个位置有多个条时使用,就像您在 "Variables" 轴上的 "a"
处绘制了多个条一样。例如,参见 ?position_dodge
:
ggplot(mtcars, aes(x=factor(cyl), fill=factor(vs))) +
geom_bar(position="dodge")
这里,x 轴由 cyl
定义,但填充颜色由 vs
定义,因此我们将在每个 x 位置有多个由填充颜色区分的条形。 position dodge 表示将条形图彼此相邻放置(而不是彼此重叠 position = "identity"
或堆叠 position = "stack"
)。
每个位置只有一个柱状图,因此 position_dodge
什么都不做。完全摆脱你的 position_dodge
并使用 geom_bar
的宽度参数(你已经设置为 0.6)。
这是一个很好的可重现示例,但我鼓励您以后将 Stack Overflow 问题做得更最少。您所有的 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
作为调整宽度的例子(虽然这是overplotting而不是替换,所以如果你想让宽度变小编辑原来的f3定义)。
f3 + geom_bar(stat = "identity", width = 0.9)
另外两个注意事项:
您的大部分主题修改似乎都在重复
theme_classic()
--- 这也内置于ggplot2
。使用theme_classic()
而不是theme_bw()
可能会让您离目标更近一步。f3 + theme_classic()
正如 Roman 在评论中指出的那样,
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()