0

I have a multi-panel plots that I have created using facet_wrap function and I would like to add a top tick to each of the y-axis (so that each y-axis ends with a number).

I can manually do this when I am creating one plot at a time with scale_y_continuous (setting a limit based on the maximum value), but I am not sure how I would do this using facet_wrap. Setting the limit using max() doesn't seem to work.

Just to give you an idea of what I am talking about, below is a code for creating single plot with ggplot2, and enabling the top tick on the y-axis to appear.

plot <- ggplot(diamonds, aes(clarity)) +
  geom_bar() +
  scale_y_continuous(expand = c(0, 0),limits=c(0,15000)) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5),
    axis.line = element_line(),
    axis.title = element_text(size=15,face="bold")) +
  xlab("x_1") +
  ylab("y_1") +
  theme(panel.background = element_blank()
    ,panel.grid.major = element_blank()
    ,panel.grid.minor = element_blank()
    ,panel.border = element_blank()) +
  labs(title = "xy", size = 20)
plot

And this is the plot.

enter image description here

Thank you in advance! Edit: I have edited the code to show the plot. Thanks again!

Joshlhj
  • 67
  • 1
  • 6

1 Answers1

1

You can define your own function for the breaks. A simple solution would be

break_fct <-  function(x){
  round(seq(min(x), max(x), length = 5))
}

Of course you can probably improve on this using some combination of pretty and the maximum. You should find a solution that works well for your data.

plot <- ggplot(diamonds, aes(clarity)) +
  geom_bar() +
  scale_y_continuous(expand = c(0, 0), breaks = break_fct) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5),
        axis.line = element_line(),
        axis.title = element_text(size=15, face="bold")) +
  xlab("x_1") +
  ylab("y_1") +
  theme(panel.background = element_blank()
        ,panel.grid.major = element_blank()
        ,panel.grid.minor = element_blank()
        ,panel.border = element_blank()) +
  labs(title = "xy", size = 20) + 
  facet_grid(cut~., scales = "free_y")

plot

plot

shadow
  • 21,823
  • 4
  • 63
  • 77
  • Hi shadow, Thank you so much for your answer. Couple of questions though. I tried playing around with the `break_fct` function above, trying to get the right maximum values but I could get a nice set of ticks. I always ended up with numbers rounded up to nearest whole number (like the plot you have created above). What I want is to get the numbers rounded up nicely to the nearest 1000 (such that when the maximum value is 14537, the top tick can become 15000). I tried `round(x, -3)` to round the max value (within `break_fct`) to nearest thousand but it doesn't seem to work well... – Joshlhj May 18 '15 at 13:47
  • Try something like `ceiling(x / 1000) * 1000`. – shadow May 18 '15 at 19:43
  • Thanks! But it still doesn't seem to do what I want. I can get the maximum value to be rounded up nicely to the 1000, but having breaks does not seem to get the top ticks... I tried incorporating this with `limits` but it is giving me errors.. I might actually just go with creating the plots separately and putting them together.. Thanks again! – Joshlhj May 19 '15 at 00:05