Search code examples
rggplot2geom-bar

change starting value for geom_bar


I have a plot that includes data from two different scales. So far, I've plotted both variables and adjusted the scale of one variable (ss) so that it is closer to the other variables. This greatly reduced the white space in the middle of the plot.

set.seed = 42
df <- data.frame(
  cat  = runif(10, 1, 20),
  mean = runif(10, 350, 450),
  ss   = runif(10, 1, 50))

ggplot(data = df) +
  geom_bar(aes(x = cat, y = ss + 250),
           stat = "identity",
           fill = "red") +
  geom_point(aes(x = cat, y = mean)) +
  geom_smooth(aes(x = cat, y = mean),
              method = "loess", se = TRUE) +
  scale_y_continuous(sec.axis = sec_axis(trans = ~.-250,
                                         name  = "sample size")) +   
  labs(y = "mean") +
  theme_bw()

enter image description here

However, I don't love the really long bars for sample size, and I'd like to change the limits on the left y axis so that it starts 250 (where ss = 0). Unfortunately, if I replace my current scale_y_continuous parameter with limits (see below), then the bars disappear. How do I do this?

ggplot(data = df) +
  geom_bar(aes(x = cat, y = ss + 250),
           stat = "identity",
           fill = "red") +
  geom_point(aes(x = cat, y = mean)) +
  geom_smooth(aes(x = cat, y = mean),
              method = "loess", se = TRUE) +
  scale_y_continuous(limits = c(250, 510),                         ### NEW Y AXIS LIMITS
                     sec.axis = sec_axis(trans = ~.-250,
                                         name  = "sample size")) +   
  labs(y = "mean") +
  theme_bw()

enter image description here

EDIT: Updated plot with @AllanCameron's suggestion. This is really close, but it has the values of the bars extend below 0 on the secondary axis.

ggplot(data = df) +
  geom_bar(aes(x = cat, y = ss + 250),
           stat = "identity",
           fill = "red") +
  geom_point(aes(x = cat, y = mean)) +
  geom_smooth(aes(x = cat, y = mean),
              method = "loess", se = TRUE) +
  scale_y_continuous(sec.axis = sec_axis(trans = ~.-250,
                                         name  = "sample size")) +   
  labs(y = "mean") +
  theme_bw() +
  coord_cartesian(ylim = c(250, 510))  ### NEW 

enter image description here


Solution

  • Just expand parameter in scale_y_continuous() to c(0,0). This tells ggplot2 to not add padding to the plot box.

      ggplot(data = df) +
      geom_bar(aes(x = cat, y = ss + 250),
               stat = "identity",
               fill = "red") +
      geom_point(aes(x = cat, y = mean)) +
      geom_smooth(aes(x = cat, y = mean),
                  method = "loess", se = TRUE) +
      scale_y_continuous(sec.axis = sec_axis(trans = ~.-250, name  = "sample size"),
                         expand = c(0,0)) +   # New line here!
      labs(y = "mean") +
      theme_bw() +
      coord_cartesian(ylim = c(250, 510)) 
    

    enter image description here