Home > Back-end >  Plotting second x axis in ggplot2
Plotting second x axis in ggplot2

Time:06-15

I am trying to change the following plot to show how depth and age correlate

enter image description here

The only thing I would like changes is to have the 'median' being shown as a scale on the top x axis, I do not want to actually plot it. I would like to show the relationship with depth and age so when looking at a certain depth on bottom x axis, one can review the top x axis to see when it was.

This is the data that I am using:

depth   car median
1   35.74243948 2020
2   42.170871   2012
3   42.086289   2004
4   37.89447248 1993
5   31.20782059 1983
6   28.08528395 1973
7   22.54616601 1963
8   32.50899326 1952
9   29.01270011 1942
10  33.08126927 1931
11  36.15984121 1921
12  28.59021896 1911
13  33.00353711 1901
14  28.57096032 1891
15  36.28325127 1880
16  20.88911761 1869
17  24.79009543 1859
18  34.93205204 1848
19  30.5315156  1837
20  40.64330918 1827
21  40.61580559 1818
22  32.05901443 1807
23  41.01255292 1797
24  52.41091739 1786
25  46.33041878 1775
26  33.08084756 1765
27  32.32787902 1755
28  54.02387432 1744
29  50.2425805  1734
30  46.66406552 1724
31  49.66426074 1713
32  46.20546128 1703
33  44.30758835 1692
34  43.44632528 1682
35  37.35647406 1672
36  52.43820402 1663
37  47.89665099 1652
38  49.12036227 1642
39  28.30939009 1631
40  37.96676039 1621
41  30.75896538 1611
42  32.46178394 1601
43  42.04289719 1592
44  39.04435104 1581
45  52.50130094 1571
46  52.20666323 1560
47  37.96828344 1550
48  39.07549843 1539
49  44.33285268 1529
50  32.36392051 1520
51  38.6081312  1509
52  46.72974472 1499
53  46.86924313 1489
54  41.75819962 1480
55  40.653891   1469
56  42.00774678 1459
57  48.3534753  1448
58  34.97651894 1438
59  35.50923881 1427
60  31.41705269 1417
61  37.52759271 1407
62  53.34552952 1397
63  34.55188721 1386
64  37.50595681 1376
65  35.80119095 1365
66  25.40296193 1355
67  42.33051465 1345
68  40.78436013 1335
69  40.96636248 1325
70  36.81320321 1314
71  35.06545911 1304
72  42.92119762 1293
73  42.80787736 1283
74  45.74680041 1273
75  63.92437113 1263
76  39.04772475 1253
77  47.50532858 1243
78  43.93093005 1234
79  37.8086155  1224
80  40.87432243 1215
81  38.28122973 1204
82  54.41815255 1195
83  42.98279363 1185
84  52.34176176 1175
85  48.12075562 1167
86  67.03983892 1157
87  56.06999253 1148
88  55.82554367 1139
89  63.99857466 1130
90  43.5713955  1121
91  38.99634076 1112
92  45.61218865 1102
93  57.75757587 1093
94  56.65346763 1084
95  31.68490516 1075
96  39.5796553  1065
97  54.642194   1056
98  31.55264459 1047
99  38.07563737 1038
100 35.46524475 1028
101 50.13590751 1019
102 46.23700802 1009
103 41.46869471 1000
104 38.3328084  991
105 34.37533353 982
106 33.04450699 972
107 29.72062607 963
108 36.65961766 954
109 37.97454672 945
110     936
111 43.2547494  927
112 51.3720819  918
113 37.16644379 909
114 35.23733973 900
115 40.42991199 891
116 36.33514445 882
117 35.50731671 873
118 35.95110736 863
119 53.45682002 854
120 56.25232174 846
121 52.52331843 836
122 16.11117813 828
123 13.48256437 817
124 12.28190052 805
125 14.36869236 792
126 18.38228466 780
127 16.86353293 769
128 16.64908144 758
129 15.60657277 747
130 20.96238654 736
131 19.21359942 726
132 20.09814637 714
133 14.51688002 704
134 14.08610976 694
135 17.88161847 684
136 19.01196925 673
137 12.75014778 662
138 21.42748697 652
139 17.85825048 641
140 23.67381498 630
141 26.18811047 619
142 24.56024674 609
143 24.07220142 598
144 17.60271365 588
145 18.45749045 576
146 25.06268819 566
147 19.06602767 554
148 15.25017996 545
149 20.28120721 534
150 14.42010541 524
151 15.60498601 513
152 14.54320097 503
153 17.12560381 493
154 16.73548986 484
155 24.35256362 474
156 18.28220269 464
157 17.57853612 454
158 13.65532164 447
159 16.3140712  437
160 15.28765645 429
161 11.9022666  419
162 10.51226689 405
163 10.32857134 384
164 7.957472784 366
165 9.538100261 347
166 10.56930427 329
167 11.39246194 309
168 10.66770588 292
169 9.811614778 273
170 9.683892419 256
171 13.85387913 238
172 10.38923973 221
173 14.2538436  204
174 12.80712144 189
175 20.21878244 173
176 12.42281286 156
177 12.4149444  139
178 9.72123096  123
179 14.64517225 106
180 17.25078924 90
181 34.81455229 74
182 26.74203796 58
183 31.456963   44
184 29.93563579 27
185 32.37859266 11
186 35.0808707  -6
187 42.59751538 -21
188 40.59859059 -36
189 33.38130005 -52
190 30.72552461 -66
191 44.20504199 -81
192 34.76921863 -95
193 38.57218793 -111
194 44.12867072 -122
195 45.79884076 -136
196 33.7927477  -147
197 22.67275301 -159
198 25.11255763 -182
199 20.28751658 -210
200 12.3070514  -237
201 13.23309219 -272
202 14.31516665 -300
203 13.02842264 -332
204 11.99484488 -358
205 18.97722259 -386
206 20.26580989 -411
207 17.21680235 -436
208 22.83971585 -462
209 21.13579074 -487
210 19.41398938 -512
211 18.72184648 -538
212 21.48147639 -561
213 21.42284503 -585
214 23.02300262 -610
215 22.83171895 -634
216 22.4359476  -658
217 22.73599372 -684
218 20.57330853 -708
219 23.63307393 -733
220 17.76033102 -757
221 22.37030074 -782
222 16.98950506 -808
223 19.1125426  -831
224 16.38709395 -856
225 21.34471565 -881
226 20.31497761 -906
227 26.9253855  -931
228 21.8169372  -955
229 21.08921444 -980
230 27.00415031 -1006
231 20.8950484  -1032
232 21.4064242  -1057
233 19.44889021 -1082
234 24.31698127 -1108
235 26.39224627 -1133
236 26.10839412 -1158
237 29.45815269 -1183
238 28.11831345 -1207
239 22.71430287 -1233
240 16.31644028 -1255
241 26.16925005 -1279
242 21.26230066 -1301
243 28.58126463 -1325
244 34.32717197 -1346
245 48.61297156 -1364
246 37.93572941 -1384
247 36.80932325 -1404
248 28.97888329 -1426
249 37.69646201 -1445
250 31.09660056 -1466
251 36.23481135 -1486
252 47.22352885 -1506
253 30.13688707 -1527
254 22.90690907 -1547
255 35.40121295 -1567
256 25.87335129 -1588
257 33.60689762 -1607
258 33.94328177 -1627
259 28.04066801 -1646
260 37.43483731 -1668
261 25.61945508 -1687
262 31.401323   -1708
263 31.2229668  -1728
264 33.76406531 -1748
265 33.19905185 -1769
266 38.13566689 -1790
267 33.31604718 -1810
268 30.38464066 -1832
269 40.3949611  -1852
270 36.07203677 -1872
271 27.56042709 -1892
272 26.65505345 -1914
273 26.63817452 -1935
274 23.38027342 -1955
275 21.37883896 -1974
276 26.33838859 -1997
277 32.93112609 -2016
278 25.62589728 -2038
279 30.07774534 -2059
280 39.63383859 -2080
281 31.20960342 -2101
282 32.38174383 -2122
283 28.35986773 -2143
284 30.93514171 -2164
285 28.87887727 -2183
286 28.25529501 -2205
287 28.60944639 -2226
288 38.17443605 -2247
289 34.23027563 -2268
290 26.91670625 -2290
291 32.87170485 -2311
292 25.36387267 -2334
293 22.85946382 -2355
294 21.2898466  -2379
295 24.21861165 -2402
296 29.54733331 -2427
297 28.07835781 -2451
298 40.21149193 -2472
299 44.97005239 -2492
300 65.5866884  -2507

This is the code I am currently plotting with:

CAR <- ggplot() 
  geom_line(data = LHB, mapping = aes(x = depth, y = car), col = 'black')  
   ggtitle("Carbon Accumulation Liffey Head Bog")  
  xlab("Depth (cm)")  
  ylab(bquote(Carbon~Accumulation~(g~C/m^2/yr^-1)))  
  scale_x_reverse()
CAR 

I would appreciate any help or knowledge to help me fix this issue

CodePudding user response:

A way to do it is using geom_text to draw corresponded median at specific depth.

This solution instead of 2nd x-axis is due to the fact that 2nd axis is just a markup of the main axis and as the scale between depth & median is a bit random which not able to transform using a formula.

library(ggplot2)
library(dplyr)

ggplot() 
  geom_line(data = LHB, mapping = aes(x = depth, y = car), col = 'black')  
  geom_text(data = LHB %>% filter(depth %% 100 == 0 | depth == 1),
            aes(x = depth, y = 70, label = median), vjust = 0)  
  ggtitle("Carbon Accumulation Liffey Head Bog")  
  xlab("Depth (cm)")  
  ylab(bquote(Carbon~Accumulation~(g~C/m^2/yr^-1)))

Created on 2022-06-15 by the reprex package (v2.0.1)

I take your data and create a pastebin from dput here: https://pastebin.com/VTk8C6fG

CodePudding user response:

You can do it using the sec.axis argument. Since the relation between median and depth is non-linear, you should use approxfun() to calculate the corresponding median value for a given depth. Then this works, after running @SinhNguyen's code to load the dataset:

LHB <- structure(list(depth = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 
 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 
 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 
 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 
 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 
 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 
 107, 108, 109, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 
 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 
 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 
 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 
 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 
 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 
 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 
 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 
 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 
 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 
 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 
 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 
 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 
 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 
 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300), car = c(35.74243948, 
 42.170871, 42.086289, 37.89447248, 31.20782059, 28.08528395, 
 22.54616601, 32.50899326, 29.01270011, 33.08126927, 36.15984121, 
 28.59021896, 33.00353711, 28.57096032, 36.28325127, 20.88911761, 
 24.79009543, 34.93205204, 30.5315156, 40.64330918, 40.61580559, 
 32.05901443, 41.01255292, 52.41091739, 46.33041878, 33.08084756, 
 32.32787902, 54.02387432, 50.2425805, 46.66406552, 49.66426074, 
 46.20546128, 44.30758835, 43.44632528, 37.35647406, 52.43820402, 
 47.89665099, 49.12036227, 28.30939009, 37.96676039, 30.75896538, 
 32.46178394, 42.04289719, 39.04435104, 52.50130094, 52.20666323, 
 37.96828344, 39.07549843, 44.33285268, 32.36392051, 38.6081312, 
 46.72974472, 46.86924313, 41.75819962, 40.653891, 42.00774678, 
 48.3534753, 34.97651894, 35.50923881, 31.41705269, 37.52759271, 
 53.34552952, 34.55188721, 37.50595681, 35.80119095, 25.40296193, 
 42.33051465, 40.78436013, 40.96636248, 36.81320321, 35.06545911, 
 42.92119762, 42.80787736, 45.74680041, 63.92437113, 39.04772475, 
 47.50532858, 43.93093005, 37.8086155, 40.87432243, 38.28122973, 
 54.41815255, 42.98279363, 52.34176176, 48.12075562, 67.03983892, 
 56.06999253, 55.82554367, 63.99857466, 43.5713955, 38.99634076, 
 45.61218865, 57.75757587, 56.65346763, 31.68490516, 39.5796553, 
 54.642194, 31.55264459, 38.07563737, 35.46524475, 50.13590751, 
 46.23700802, 41.46869471, 38.3328084, 34.37533353, 33.04450699, 
 29.72062607, 36.65961766, 37.97454672, 43.2547494, 51.3720819, 
 37.16644379, 35.23733973, 40.42991199, 36.33514445, 35.50731671, 
 35.95110736, 53.45682002, 56.25232174, 52.52331843, 16.11117813, 
 13.48256437, 12.28190052, 14.36869236, 18.38228466, 16.86353293, 
 16.64908144, 15.60657277, 20.96238654, 19.21359942, 20.09814637, 
 14.51688002, 14.08610976, 17.88161847, 19.01196925, 12.75014778, 
 21.42748697, 17.85825048, 23.67381498, 26.18811047, 24.56024674, 
 24.07220142, 17.60271365, 18.45749045, 25.06268819, 19.06602767, 
 15.25017996, 20.28120721, 14.42010541, 15.60498601, 14.54320097, 
 17.12560381, 16.73548986, 24.35256362, 18.28220269, 17.57853612, 
 13.65532164, 16.3140712, 15.28765645, 11.9022666, 10.51226689, 
 10.32857134, 7.957472784, 9.538100261, 10.56930427, 11.39246194, 
 10.66770588, 9.811614778, 9.683892419, 13.85387913, 10.38923973, 
 14.2538436, 12.80712144, 20.21878244, 12.42281286, 12.4149444, 
 9.72123096, 14.64517225, 17.25078924, 34.81455229, 26.74203796, 
 31.456963, 29.93563579, 32.37859266, 35.0808707, 42.59751538, 
 40.59859059, 33.38130005, 30.72552461, 44.20504199, 34.76921863, 
 38.57218793, 44.12867072, 45.79884076, 33.7927477, 22.67275301, 
 25.11255763, 20.28751658, 12.3070514, 13.23309219, 14.31516665, 
 13.02842264, 11.99484488, 18.97722259, 20.26580989, 17.21680235, 
 22.83971585, 21.13579074, 19.41398938, 18.72184648, 21.48147639, 
 21.42284503, 23.02300262, 22.83171895, 22.4359476, 22.73599372, 
 20.57330853, 23.63307393, 17.76033102, 22.37030074, 16.98950506, 
 19.1125426, 16.38709395, 21.34471565, 20.31497761, 26.9253855, 
 21.8169372, 21.08921444, 27.00415031, 20.8950484, 21.4064242, 
 19.44889021, 24.31698127, 26.39224627, 26.10839412, 29.45815269, 
 28.11831345, 22.71430287, 16.31644028, 26.16925005, 21.26230066, 
 28.58126463, 34.32717197, 48.61297156, 37.93572941, 36.80932325, 
 28.97888329, 37.69646201, 31.09660056, 36.23481135, 47.22352885, 
 30.13688707, 22.90690907, 35.40121295, 25.87335129, 33.60689762, 
 33.94328177, 28.04066801, 37.43483731, 25.61945508, 31.401323, 
 31.2229668, 33.76406531, 33.19905185, 38.13566689, 33.31604718, 
 30.38464066, 40.3949611, 36.07203677, 27.56042709, 26.65505345, 
 26.63817452, 23.38027342, 21.37883896, 26.33838859, 32.93112609, 
 25.62589728, 30.07774534, 39.63383859, 31.20960342, 32.38174383, 
 28.35986773, 30.93514171, 28.87887727, 28.25529501, 28.60944639, 
 38.17443605, 34.23027563, 26.91670625, 32.87170485, 25.36387267, 
 22.85946382, 21.2898466, 24.21861165, 29.54733331, 28.07835781, 
 40.21149193, 44.97005239, 65.5866884), median = c(2020, 2012, 
 2004, 1993, 1983, 1973, 1963, 1952, 1942, 1931, 1921, 1911, 1901, 
 1891, 1880, 1869, 1859, 1848, 1837, 1827, 1818, 1807, 1797, 1786, 
 1775, 1765, 1755, 1744, 1734, 1724, 1713, 1703, 1692, 1682, 1672, 
 1663, 1652, 1642, 1631, 1621, 1611, 1601, 1592, 1581, 1571, 1560, 
 1550, 1539, 1529, 1520, 1509, 1499, 1489, 1480, 1469, 1459, 1448, 
 1438, 1427, 1417, 1407, 1397, 1386, 1376, 1365, 1355, 1345, 1335, 
 1325, 1314, 1304, 1293, 1283, 1273, 1263, 1253, 1243, 1234, 1224, 
 1215, 1204, 1195, 1185, 1175, 1167, 1157, 1148, 1139, 1130, 1121, 
 1112, 1102, 1093, 1084, 1075, 1065, 1056, 1047, 1038, 1028, 1019, 
 1009, 1000, 991, 982, 972, 963, 954, 945, 927, 918, 909, 900, 
 891, 882, 873, 863, 854, 846, 836, 828, 817, 805, 792, 780, 769, 
 758, 747, 736, 726, 714, 704, 694, 684, 673, 662, 652, 641, 630, 
 619, 609, 598, 588, 576, 566, 554, 545, 534, 524, 513, 503, 493, 
 484, 474, 464, 454, 447, 437, 429, 419, 405, 384, 366, 347, 329, 
 309, 292, 273, 256, 238, 221, 204, 189, 173, 156, 139, 123, 106, 
 90, 74, 58, 44, 27, 11, -6, -21, -36, -52, -66, -81, -95, -111, 
 -122, -136, -147, -159, -182, -210, -237, -272, -300, -332, -358, 
 -386, -411, -436, -462, -487, -512, -538, -561, -585, -610, -634, 
 -658, -684, -708, -733, -757, -782, -808, -831, -856, -881, -906, 
 -931, -955, -980, -1006, -1032, -1057, -1082, -1108, -1133, -1158, 
 -1183, -1207, -1233, -1255, -1279, -1301, -1325, -1346, -1364, 
 -1384, -1404, -1426, -1445, -1466, -1486, -1506, -1527, -1547, 
 -1567, -1588, -1607, -1627, -1646, -1668, -1687, -1708, -1728, 
 -1748, -1769, -1790, -1810, -1832, -1852, -1872, -1892, -1914, 
 -1935, -1955, -1974, -1997, -2016, -2038, -2059, -2080, -2101, 
 -2122, -2143, -2164, -2183, -2205, -2226, -2247, -2268, -2290, 
 -2311, -2334, -2355, -2379, -2402, -2427, -2451, -2472, -2492, 
 -2507)), class = "data.frame", row.names = c(NA, -299L)) 

library(ggplot2)
library(dplyr)

ggplot() 
 geom_line(data = LHB, mapping = aes(x = depth, y = car), col = 'black')  
 ggtitle("Carbon Accumulation Liffey Head Bog")  
 xlab("Depth (cm)")  
 ylab(bquote(Carbon~Accumulation~(g~C/m^2/yr^-1)))  
 scale_x_reverse(sec.axis = sec_axis(approxfun(LHB$depth, LHB$median), name = "Median"))

Created on 2022-06-15 by the reprex package (v2.0.1)

  • Related