Home > front end >  How to change the ranges of the left and the right axis in ggplot2?
How to change the ranges of the left and the right axis in ggplot2?

Time:12-29

data

structure(list(VAR1 = 1:25, VAR2 = c(33151.4302619749, 30243.6061009354, 
29075.4630823572, 27646.1136405244, 27227.44196157, 25910.8454253342, 
26405.0119958585, 26167.0056585366, 25230.4079822407, 24976.0912545877, 
25343.7017313494, 26159.0753483146, 26957.9730021768, 27759.9504648796, 
29046.5695915796, 26946.8014342613, 28679.717757397, 28511.198514726, 
31713.1021393953, 29817.0124623543, 29296.1166847962, 30338.1835634015, 
31091.0104433836, 31006.5299473411, 31774.4212233181), VAR3 = c(0.159880385135182, 
0.271631841511188, 0.32632879125533, 0.399844578596391, 0.421938705559609, 
0.478323940647091, 0.48149816119631, 0.509588233124643, 0.551439379228908, 
0.573370058778077, 0.580567765134673, 0.580904582887182, 0.576292211835652, 
0.566802717164287, 0.588146883004088, 0.609420847255646, 0.5934221293271, 
0.622638198647242, 0.600671162470947, 0.563000351965266, 0.641578758944993, 
0.629095088526046, 0.608579189017581, 0.65220556730865, 0.63606478018115
)), class = "data.frame", row.names = c(NA, -25L))

I want to draw a plot with dual y axis with the following code. It seems that the limit of the left axis doesn't match the red line appropriately and I want to stretch the red line with the lowest point being at the bottom of the plot.

ggplot(df, aes(VAR1))  
  geom_point(aes(y = VAR2), size = 3, color = '#D62728')  
  geom_line(aes(y = VAR2), size = 1, color = '#D62728', lty = 3)  
  geom_point(aes(y = VAR3*5.1e4), size = 3, color = '#1F77B4')   
  geom_line(aes(y = VAR3*5.1e4), size = 1, color = '#1F77B4')  
  scale_y_continuous(name = 'VAR2', sec.axis = sec_axis(~./5.1e4, name = "VAR3"))

enter image description here

CodePudding user response:

Scale the data differently (and add an intercept, not just a scalar).

Up front, the use of two axes is (intentionally) obfuscating the true relationship between the lines. From this image, one might infer that the two numbers overlap somehow, where in reality the ranges are far different (VAR2 is 25k-33k, VAR3 is 0.16-0.65). I recognize you are overlapping these intentionally, I'm adding this note as a caution that this methodology can introduce visual bias.

zz <- c(22000, 18000)
ggplot(df, aes(VAR1))  
  geom_point(aes(y = VAR2), size = 3, color = '#D62728')  
  geom_line(aes(y = VAR2), size = 1, color = '#D62728', lty = 3)  
  geom_point(aes(y = zz[1]   VAR3*zz[2]), size = 3, color = '#1F77B4')   
  geom_line(aes(y = zz[1]   VAR3*zz[2]), size = 1, color = '#1F77B4')  
  scale_y_continuous(name = 'VAR2', sec.axis = sec_axis(~(. - zz[1]) / zz[2], name = "VAR3"))

enter image description here

  • Related