Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse secondary continuous_y_axis in ggplot2

Tags:

r

ggplot2

yaxis

I have a dataset I would like to graph with two Y-axes. The would like to reverse the secondary Y-axis so that it runs -60 to -40. The primary Y-axis should run 300-550. I cannot figure out how to reverse the secondary Y-axis without reversing the primary Y-axis. Right now the secondary Y-axis is how I want it, but the primary Y-axis is incorrect.

Any help would be greatly appreciated!

graph

enter image description here

my dataset

X    y1     y2
1   394 -45.65
1   377 -45.62
1   377 -45.72
6   342 -46.38
6   375 -46.35
6   408 -46.47
11  336 -47.22
11  349 -47.18
11  376 -47.31
16  397 -48.15
16  397 -48.1
16  397 -48.25
21  395 -49.15
21  406 -49.1
21  417 -49.25
26  390 -50.2
26  418 -50.15
26  446 -50.31
31  427 -51.28
31  438 -51.23
31  337 -51.4
36  427 -52.38
36  438 -52.33
36  337 -52.5
41  342 -53.47
41  377 -53.42
41  376 -53.58
46  372 -54.54
46  387 -54.49
46  415 -54.64
51  360 -55.55
51  376 -55.51
51  442 -55.65
56  308 -56.5
56  424 -56.45
56  459 -56.59
61  395 -57.35
61  356 -57.31
61  436 -57.43
66  436 -58.1
66  370 -58.07
66  412 -58.17
71  488 -58.73
71  527 -58.71
71  489 -58.79
76  396 -59.24
76  379 -59.22
76  410 -59.28
81  372 -59.62
81  477 -59.6
81  478 -59.64
86  507 -59.87
86  445 -59.86
86  427 -59.88
91  414 -59.99
91  427 -59.99
91  436 -60
96  415 -60.01
96  396 -60.01
96  440 -60
101 415 -59.93
101 371 -59.94
101 467 -59.92
106 478 -59.78
106 433 -59.79
106 415 -59.75
111 453 -59.56
111 381 -59.57
111 414 -59.53
116 406 -59.31
116 376 -59.32
116 366 -59.28
121 398 -59.04
121 380 -59.05
121 463 -59.01
126 536 -58.77
126 327 -58.78
126 396 -58.74
131 397 -58.52
131 381 -58.53
131 505 -58.49
136 367 -58.3
136 442 -58.31
136 400 -58.27
141 515 -58.12
141 385 -58.13
141 346 -58.1
146 368 -58
146 343 -58
146 338 -57.99
151 357 -57.94
151 338 -57.94
151 338 -57.94
156 407 -57.94
156 382 -57.94
156 352 -57.95
161 350 -58.01
161 336 -58.01
161 374 -58.02
166 372 -58.15
166 355 -58.14
166 342 -58.16
171 392 -58.35
171 332 -58.33
171 311 -58.37
176 358 -58.6
176 365 -58.58
176 391 -58.63
181 338 -58.9
181 369 -58.88
181 414 -58.93
186 340 -59.22
186 405 -59.2
186 350 -59.26
191 369 -59.57
191 410 -59.55
191 393 -59.61
196 416 -59.92
196 394 -59.9
196 377 -59.95
201 342 -60.25
201 375 -60.23
201 336 -60.28
206 349 -60.55
206 376 -60.52
206 397 -60.57
211 367 -60.78
211 395 -60.76
211 406 -60.8
216 390 -60.93
216 418 -60.92
216 427 -60.94
221 438 -60.99
221 337 -60.99
221 342 -60.99
226 377 -60.94
226 376 -60.94
226 372 -60.92
231 387 -60.76
231 415 -60.76
231 360 -60.73
236 376 -60.44
236 442 -60.45
236 308 -60.4
241 424 -59.99
241 459 -60.01
241 395 -59.93
246 356 -59.4
246 436 -59.43
246 436 -59.33
251 370 -58.68
251 412 -58.72
251 488 -58.6
256 527 -57.85
256 489 -57.89
256 394 -57.75
261 377 -56.92
261 342 -56.96
261 375 -56.81
266 336 -55.9
266 349 -55.94
266 376 -55.79
271 397 -54.82
271 367 -54.87
271 395 -54.7
276 406 -53.7
276 390 -53.75
276 418 -53.58
281 427 -52.56
281 438 -52.61
281 337 -52.44
286 342 -51.42
286 377 -51.47
286 376 -51.3
291 372 -50.3
291 387 -50.35
291 415 -50.18
296 360 -49.22
296 376 -49.27
296 442 -49.11
301 308 -48.2
301 424 -48.25
301 459 -48.1
306 395 -47.26
306 356 -47.3
306 436 -47.17
311 436 -46.41
311 370 -46.45
311 412 -46.33
316 488 -45.67
316 527 -45.7
316 489 -45.6
321 394 -45.04
321 377 -45.07
321 342 -44.98
326 375 -44.54
326 336 -44.56
326 349 -44.5
331 376 -44.18
331 397 -44.19
331 367 -44.15
336 395 -43.96
336 406 -43.96
336 390 -43.94
341 418 -43.88
341 427 -43.88
341 438 -43.88
346 337 -43.95
346 342 -43.94
346 377 -43.96
351 376 -44.16
351 372 -44.15
351 387 -44.19
356 415 -44.52
356 360 -44.49
356 376 -44.56
361 442 -45
361 308 -44.97
361 424 -45.06
366 459 -45.58

My Code

ggplot() +
  geom_point(data = df, aes(x = X, y = y1),
             size = 3, shape = 2, colour = "black") +
  stat_smooth(data = df, aes(x = X, y = y1),
              method = lm, se = TRUE, fill = NA,
              formula = y ~ poly(x, 3, raw = TRUE),
              colour = "black", linetype = "longdash") +
  geom_line(data = df, aes(x = X, y = y2 * (-8)),
            colour = "magenta3", size = 1) +
  scale_y_continuous(sec.axis = sec_axis(~./8, name = "y2 \n"),
                     trans = 'reverse')
like image 592
Christopher Avatar asked Jul 10 '18 03:07

Christopher


1 Answers

Assuming the chart itself is okay & you just want the y-axis labels to go from 300 to 550 on one side & -60 to -40 on the other, you are looking for a one-to-one function that maps from the first set of values to the other. And since both are (I assume) increasing, rev() is not required.

My grasp of linear regression coefficient formulae is pretty rusty, but the following should give the right values for the linear transformation:

> lm(y ~ x, data = data.frame(x = c(300, 550), y = c(-60, -40)))

Call:
lm(formula = y ~ x, data = data.frame(x = c(300, 550), y = c(-60, 
    -40)))

Coefficients:
(Intercept)            x  
     -84.00         0.08 

Plug these values into the transformation for the secondary y-axis:

ggplot(data = df, aes(x = X, y = y1)) + 
  geom_point(size = 3, shape = 2, colour = "black") + 
  stat_smooth(method = lm, se = TRUE, fill = NA, 
              formula = y ~ poly(x, 3, raw = TRUE), 
              colour = "black", linetype = "longdash") +
  geom_line(aes(y = y2*(-8)), colour = "magenta3", size=1) +
  scale_y_continuous(limits = c(300, 550),
                     sec.axis = sec_axis(trans = ~ -84 + 0.08 * ., 
                                         name = "y2"))

plot

like image 125
Z.Lin Avatar answered Oct 23 '22 01:10

Z.Lin