减少ggplot2中的条形组之间的空间 [英] Reduce space between groups of bars in ggplot2

查看:95
本文介绍了减少ggplot2中的条形组之间的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法删除geom_plot中多余的空白侧翼条组.

I haven't been able to remove extra white space flanking groups of bars in geom_plot.

我想做罗兰在这里取得的成就:删除酒吧ggplot2之间的空间,但是当我尝试实施他的解决方案时,出现错误警告消息: geom_bar()不再具有binwidth参数.请改为使用geom_histogram()."

I'd like to do what Roland achieves here: Remove space between bars ggplot2 but when I try to implement his solution I get the error "Warning message: geom_bar() no longer has a binwidth parameter. Please use geom_histogram() instead."

我将这行代码添加到绘图中(尝试不同的宽度):

I added this line of code to my plot (trying different widths):

  geom_histogram(binwidth = 0.5) +

,它返回错误:stat_bin()不能与y美学一起使用."而且没有情节.

which returns "Error: stat_bin() must not be used with a y aesthetic." and no plot.

数据:

mydf<- data.frame(Treatment = c("Con", "Con", "Ex", "Ex"),
             Response = rep(c("Alive", "Dead"), times=2),
             Count = c(259,10,290,21))

 aPalette<-c("#009E73", "#D55E00")

情节:

example<-ggplot(mydf, aes(factor(Response), Count, fill = Treatment)) + 
  geom_bar(stat="identity",position = position_dodge(width = 0.55), width = 
  0.5) + 
  scale_fill_manual(values = aPalette, name = "Treatment") + #legend title
  theme_classic() +
  labs(x = "Response", 
  y = "Count") + 
  scale_y_continuous(breaks = c(0,50,100,150,200,250,275), expand = c(0,0), 
  limits = c(0, 260)) +
  theme(legend.position = c(0.7, 0.3)) +
  theme(text = element_text(size = 15)) #change all text size

  example

返回:

注意::我不知道为什么会收到警告消息:已删除1行包含缺失值的行(geom_bar)".但我并不担心,因为使用我的实际数据不会发生这种情况 **重新注意-之所以发生这种情况,是因为我将y轴的限制设置为低于移除的条形的最大值.我不会更改代码,所以不必重新绘制图形,而是更改

Note: I don't know why I'm getting "Warning message: Removed 1 rows containing missing values (geom_bar)." but I'm not concerned about it because that doesn't happen using my actual data **Edit re: note - this is happening because I set the limit for the y-axis lower then the max value for the bar that was removed. I'm not going to change to code so I don't have to redraw my figure, but changing

limits = c(0, 260) 

limits = c(0, 300)

将显示所有条形图.万一别人有类似的问题.我将查找与此问题相关的帖子,并在我可以链接答案时使此编辑更加简洁

will show all the bars. In case someone else had a similar problem. I'm going to find a post related to this issue and will make this edit more concise when I can link an answer

推荐答案

如果我完全错过了您要在此处完成的工作,请原谅我,但是ggplot包含这么多空白的唯一原因是因为您将条形图限制为特定的宽度并增加了图形的大小. 图形内的空白是条形宽度和图形宽度的输出.

Forgive me if I completely missed what your trying to accomplish here but the only reason that ggplot has included so much white space is because you constrained the bars to a particular width and increased the size of the graph. The white space within the graph is an output of width of the bars and width of the graph.

使用原始图形...

Using your original graph...

我们注意到很多空白,但是你们都使垃圾箱变小并且图形变宽.将空间视为垃圾箱和空白之间的折衷.期望具有小容器且没有空白的宽图是不合逻辑的.为了解决这个问题,我们可以减小图形大小或增大bin大小.

We notice a lot of whitespace but you both made the bins small and your graph wide. Think of the space as a compromise between bins and whitespace. Its illogical to expect a wide graph with small bins and no whitespace. To fix this we can either decrease the graph size or increase the bin size.

首先,我们通过消除限制来将垃圾箱大小恢复为正常. 看起来很荒谬....

First we increase the bin size back to normal by removing your constraints. Which looks rediculous....

但是,通过查看您首先包含的ggplot2条之间的删除空间链接,他所做的就是删除约束和限制宽度.这样做会产生类似的图形...

But by looking at the Remove space between bars ggplot2 link that you included above all he did was remove constraints and limit width. Doing so would result in a similar graph...

包括上面链接中的图形....

Including the graph from your link above....

并消除所有约束....

And removing all of your constraints....

    example<-ggplot(mydf, aes(factor(Response), Count, fill = Treatment)) + 
  geom_bar(stat="identity",position = position_dodge()) +
  scale_fill_manual(values = aPalette, name = "Treatment") +
  theme_bw() +
  labs(x = "Response", y = "Count")

example

如果您的目标不是通过删除空格使您的图与链接中的图相似,那么我希望这对您有所帮助.

If your goal was not to make your graph similar to the one in the link by removing whitespace let me know, other then that I hope this helped.

这篇关于减少ggplot2中的条形组之间的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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