1

I want to scale and label the two y-axis for the following dataset and plot.

climate <- tibble(
  Month = 1:4,
  Temp = c(NA, 0.16,0.05,-0.05),
  Precip = c(340,343,337,438)
)

ylim.sec <- c(300, 480)  
ylim.prim  <- c(-0.1, 0.4)    


b <- diff(ylim.prim)/diff(ylim.sec)
a <- b*(ylim.prim[1] - ylim.sec[1])


ggplot(climate) + 
  geom_line(mapping = aes(x = Month, y = Precip)) +
  geom_bar(mapping = aes(x = Month, y = Temp/b-a),size = 2, color = "blue", fill="grey", stat = "identity", width=0.5) +
  scale_y_continuous(name = "Interruptions/day", 
                     sec.axis = sec_axis(~(. + a)*b, name = "Productivity % of best", 
                                         labels = function(b) { paste0(round(b * 100, 0), "%")})) 

The first y-axis (Precip) should range (and start) from 300 to 480 and the second y axis should range form -10% to 40%. I can change a and b but this doesnt change the labels of the axis. It will always starts by one.

edwin
  • 61
  • 4
  • It's a pain to do so. See https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales – tester Nov 03 '20 at 20:59

0 Answers0