0
library(dplyr)
library(tidyr)
library(ggplot2)
dummy.data <- data.frame(
  X1 = c(1,2,-1,0),
  X2 = c(2,3,-2,-1),
  Month = c("January", "June", "January", "June"),
  Colour = c("A", "B", "B", "A")
  ) %>% 
  gather(key, value, X1:X2)

ggplot(dummy.data) + 
  geom_line(aes(x = Month, y = value, colour = Colour, group = Colour)) +
  facet_grid(~variable)

enter image description here

I would like to have January at the left edge of each panel; June at the right edge. There should be no grey band between the endpoints of the coloured lines and the edges of the panel.

Hugh
  • 15,521
  • 12
  • 57
  • 100
  • Add `+ scale_x_discrete(expand = c(0, 0))`. Other than that, I would suggest making your question reproducible, such as adding all the packages you are using (I see at least 3!) and changing `variable` to `key` in `ggplot` – David Arenburg Dec 17 '14 at 11:55
  • Yeah, it's a duplicate. Weirdly, `tidyr` makes the variable `key` into `variable` a la `melt` – Hugh Dec 17 '14 at 12:06

1 Answers1

1

You can use the expand variable in scale_x_discrete. Note that you probably also need to adjust the labels, because otherwise they overlap. You can use hjust in theme to do this:

ggplot(dummy.data) + 
  geom_line(aes(x = Month, y = value, colour = Colour, group = Colour)) +
  facet_grid(~variable) + 
  scale_x_discrete(expand=c(0,0)) +
  theme(axis.text.x = element_text(hjust=c(0, 1)))
shadow
  • 21,823
  • 4
  • 63
  • 77