Reduce space between groups of bars in ggplot2

2019-02-23 08:11发布

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

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) +

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

Data:

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")

Plot:

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

Returns: enter image description here

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) 

to

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

1条回答
冷血范
2楼-- · 2019-02-23 08:59

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... orig graph

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....

enter image description here

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...

similar graph

Including the graph from your link above.... enter image description here

And removing all of your constraints.... enter image description here

    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.

查看更多
登录 后发表回答