1

How to get rid of all this space where the blue lines are?

Data:

data = data.frame(is_repeat = c(0,0,0,1,1,1,1,1,1,1),
                  value = c(12000,8000,20000,14000,15000,11000,20000,60000,20000, 20000))

data$is_repeat = factor(data$is_repeat, levels = c(0,1),
                                        labels = c("One-time", "Repeat"))

Plot:

ggplot(data, aes(is_repeat, value)) +
      geom_bar(stat = "identity", width = 0.3) + 
      ggtitle("Title") + 
      xlab("Type of event") +
      ylab("Total Value") +
      ylim(0, 150000) +
      theme_minimal()

enter image description here

edit: I looked at that question and it did NOT solve my problem. My guess is that in the other question's plot, there are 4 bars, so it looks filled. I want to reduce the total width of the X axis.

another edit: Added data.

rnewbie
  • 145
  • 1
  • 1
  • 7
  • To get help you will definitely have to give more context and explanation about your problem. What's the expected result vs current result? Can you show more code? – Takoyaro Jun 10 '16 at 18:14
  • 4
    Similar: http://stackoverflow.com/questions/25887066/remove-space-between-bars-ggplot2 – aosmith Jun 10 '16 at 18:15
  • 1
    `geom_bar(stat = "identity", width = 1)` – alistaire Jun 10 '16 at 18:23
  • While a cute solution, I'm not really looking to adjust the widths of the bars. I'm looking to remove the space between the bars and the space from the end of the plot to the bars. I looked at that question, but it gives some code with no explanation. I can't understand what is going on at all. – rnewbie Jun 10 '16 at 18:32
  • Could you add your input data here? – Miha Jun 10 '16 at 19:00
  • To reduce the width of the x-axis you need to control the plotting device; That is not something you can specify in the ggplot command. For example: assign your plot to `p`, then `ggsave("plot.pdf", plot=p, width=4, height=8)`. Now the whole plot is narrower, but you still probably need `geom_bar(width=...)` (as noted by @alistaire and @miha) to control the relative width of the bars vs. the space between the bars. Finally, `scale_x_discrete(expand=...)` (from @miha answer) can adjust the space between the bars and the edge of the plot. – bdemarest Jun 10 '16 at 20:17
  • In `geom_bar`, `width` defaults to "90% of the resolution of the data", which when `x` is a factor means 0.9 (as factors are stored as integers). Setting it to smaller values will leave more relative space between bars; setting it to 1 will leave no space between bars. To reduce the width of the plot (and therefore the x scale and the width of the bars), save it as narrower with `ggsave`. If you click the "Zoom" button in RStudio, you can resize the window to see the effect before you save. – alistaire Jun 10 '16 at 20:44
  • ggsave solved my problem. Thank you very much for it, the resizing in the zoom pane is invaluable too! It's kind of unfortunate that we can't specify this in the plotting function. I guess there are some logistical problems. – rnewbie Jun 11 '16 at 06:42

1 Answers1

3

If you are looking to remove the space between the bars completely and you don't mind the width of bars you could do it with:

 geom_bar(stat="identity", position="stack", width=1)

or theme(aspect.ratio=1)

And to remove the space from the end of the plot to the bars you need

scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat")) 

So your code looks like this:

  ggplot(data, aes(is_repeat, value)) +
  geom_bar(stat="identity", position="stack", width=1) + 
  ggtitle("Title") + 
  xlab("Type of event") +
  ylab("Total Value") +
  ylim(0, 150000) + 
  scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat")) +
  theme_minimal()

And the output:

enter image description here

You can add space between bars with changing the width=1

Miha
  • 2,559
  • 2
  • 19
  • 34