1

I have a faceted bar graph.

 dat <- data.frame(ID = c("A", "A", "B", "B", "C", "C"),
                   A = c("Type 1", "Type 2", "Type 1", "Type 2", "Type 1", "Type 2"),
                   B = c(1, 2, 53, 87, 200, 250))

 ggplot(data = dat, aes(x = A, y = B)) + 
   geom_bar(stat = "identity") +
   facet_wrap(~ID, scales= "free_y")

How do I code to only have 3 y-axis values displayed per graph?

I've tried

   +scale_y_continuous(breaks=3)
  • You can try `scale_y_continuous(n.breaks = 3)`, but 3 breaks is not guaranteed. It just outputs 3 breaks if the 3 breaks can be 'pretty'. – teunbrand Apr 10 '20 at 08:47

1 Answers1

0

To get more control over the breaks you can write your own breaks function. The following code gives you exactly 3 breaks. However, this very basic approach does not necessarily result in "pretty" breaks:

library(ggplot2)

my_breaks <- function(x) { 
  seq(0, round(max(x), 0), length.out = 3) 
} 

my_limits <- function(x) { 
  c(x[1], ceiling(x[2]))
}

# Dataset 2
dat <- data.frame(
  ID = c("A", "A", "A","B", "B", "B", "C", "C", "C"), 
  A = c("Type 1", "Type 2", "Type 3", "Type 1", "Type 2", "Type 3","Type 1", "Type 2", "Type 3"), 
  B = c(1.7388, 4.2059, .7751, .9489, 2.23405, .666589, 0.024459, 1.76190, 0.066678)) 

ggplot(data = dat, aes(x = A, y = B)) + 
  geom_bar(stat = "identity") + 
  facet_wrap(~ID, scales= "free_y") + 
  scale_y_continuous(breaks = my_breaks, limits = my_limits)

# Dataset 1
dat <- data.frame(ID = c("A", "A", "B", "B", "C", "C"),
                  A = c("Type 1", "Type 2", "Type 1", "Type 2", "Type 1", "Type 2"),
                  B = c(1, 2, 53, 87, 200, 250))

ggplot(data = dat, aes(x = A, y = B)) + 
  geom_bar(stat = "identity") + 
  facet_wrap(~ID, scales= "free_y") + 
  scale_y_continuous(breaks = my_breaks, limits = my_limits)

Created on 2020-04-10 by the reprex package (v0.3.0)

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thanks helpful and you are on the right track. When I try to apply that to my actual data set, it works for 8/10 facet plots, but for two plots it only labels 0. Any idea what's causing that? – Kimberly Peter Apr 10 '20 at 15:32
  • Example of problem: dat <- data.frame(ID = c("A", "A", "A","B", "B", "B", "C", "C", "C"), A = c("Type 1", "Type 2", "Type 3", "Type 1", "Type 2", "Type 3","Type 1", "Type 2", "Type 3"), B = c(1.7388, 4.2059, .7751, .9489, 2.23405, .666589, 0.024459, 1.76190, 0.066678)) my_breaks <- function(x) { seq(0, pretty(max(x))[1], length.out = 3) } ggplot(data = dat, aes(x = A, y = B)) + geom_bar(stat = "identity") + facet_wrap(~ID, scales= "free_y")+ scale_y_continuous(breaks = my_breaks) – Kimberly Peter Apr 10 '20 at 15:33
  • Hi @KimberlyPeter. This is indeed a tricky one. I came up with a better solution, which at least for both of the given datasets now results in exactly three breaks. Revised `my_breaks`. However, the major change is to add a second function to ensure that all three breaks are within the axis limits. – stefan Apr 10 '20 at 20:46
  • That seems to have fixed it! Thank you so much! – Kimberly Peter Apr 11 '20 at 17:18