Home > Blockchain >  Two geom_line()s with an y-axis each
Two geom_line()s with an y-axis each

Time:03-13

I want to make a plot of a time series of two variables -- but the two of them have very different scales. So I want to plot one line following the scale of the left y-axis (say, in millions) and the other line following the secondary right y-axis (in a low percentage scale). Does anybody know how to make it? I have already achieved to put the two y-axis scales as I wish, but I need one of the lines (one of the two variables plotted) to follow the y-axis on the right. Here's some piece of code. It's not super tidy yet but for the sake of my purpose it should work.

europe_data %>% 
  rename(`Inflation Rate` = EA19CPALTT01GYM,
         `Money Supply` = MYAGM2EZM196N) %>% 
  mutate(`Inflation Rate` = (`Inflation Rate` * 100)) %>% 
  ggplot(aes(x = DATE))  
  geom_line(aes(y = `Inflation Rate`),
                linetype = "dashed")  
  geom_line(aes(y = `Money Supply`))  
  scale_y_continuous(name = "Inflation Rate",
                     sec.axis = sec_axis(~ . / 1000000000000,
                                         name = "Money Supply"))

Thanks in advance.

CodePudding user response:

I can give you maybe a solution using base. You can use this code:

First I created a sample dataset:

europe_data <- data.frame(EA19CPALTT01GYM= c(0.1, 1, 1.1, 1.2, 1.4, 1),
                          MYAGM2EZM196N = c(1000000000000, 1102010000200, 1040000607000, 1200030000000, 1000040507808, 1304006060300),
                          DATE = seq(0, 5, 1))

After that you can use this code:

europe_data <- europe_data %>% 
  rename(`Inflation Rate` = EA19CPALTT01GYM,
         `Money Supply` = MYAGM2EZM196N) %>% 
  mutate(`Inflation Rate` = (`Inflation Rate` * 100))

par(mar = c(5, 4, 4, 2)   3, new=TRUE)
## Plot first set of data and draw its axis
plot(europe_data$DATE, europe_data$`Inflation Rate`, pch=16, axes=FALSE, ylim=c(0,200), xlab="", ylab="", 
     type="b",col="black", main="Your title")
axis(2, ylim=c(0,200),col="black",las=1)  ## las=1 makes horizontal labels
mtext("Inflation rate",side=2,line=2.5)
box()

## Allow a second plot on the same graph
par(mar = c(5, 4, 4, 2)   3, new=TRUE)
#par(new=TRUE)

## Plot the second plot and put axis scale on right
plot(europe_data$DATE, europe_data$`Money Supply`, pch=15,  xlab="", ylab="", ylim=c(0,1300000000000), 
     axes=FALSE, type="b", col="red")
## a little farther out (line=4) to make room for labels
mtext("Money supply",side=4,col="red",line=4) 
axis(4, ylim=c(0,1300000000000), col="red",col.axis="red",las=1)

## Draw the time axis
axis(1,pretty(range(europe_data$DATE),10))
mtext("Date",side=1,col="black",line=2.5)  

## Add Legend
legend("bottomright",legend=c("Inflation rate","Money supply"),
       text.col=c("black","red"),pch=c(16,15),col=c("black","red"))

Output: enter image description here

CodePudding user response:

When creating a plot with a second axis, you need to remember that the axis is only there "for decoration". Adding a secondary axis doesn't mean the data will both appear on the same scale. You need to modify the data that is represented on the second axis too, so that it is at approximately the same scale as the data represented on the first axis. Here, your inflation data is in the range of about -1 to 4, but your money supply is about 5 to 12 trillion. You therefore need to divide the money supply by about 5 trillion (2 * 10^13) to get it on the same scale as the inflation. Then, whatever you have done to the data, you reverse on the secondary axis. Because these numbers are so large, and difficult for people to read easily, I would actually just multiply by 5 instead of 5 trillion, and label the axis as showing trillions:

library(ggplot2)
library(dplyr)

europe_data %>% 
  rename(`Inflation Rate` = EA19CPALTT01GYM,
         `Money Supply` = MYAGM2EZM196N) %>% 
  ggplot(aes(x = DATE))  
  geom_line(aes(y = `Inflation Rate`),
                linetype = "dashed")  
  geom_line(aes(y = `Money Supply` / (5 * 10^12)))  
  scale_y_continuous(name = "Inflation Rate",
                     sec.axis = sec_axis(~ . * 5,
                                         name = "Money Supply (trillions)"))

Created on 2022-03-12 by the reprex package (v2.0.1)


Data

europe_data <-
  structure(list(DATE = structure(c(9862, 9893, 9921, 9952, 9982, 
10013, 10043, 10074, 10105, 10135, 10166, 10196, 10227, 10258, 
10286, 10317, 10347, 10378, 10408, 10439, 10470, 10500, 10531, 
10561, 10592, 10623, 10651, 10682, 10712, 10743, 10773, 10804, 
10835, 10865, 10896, 10926, 10957, 10988, 11017, 11048, 11078, 
11109, 11139, 11170, 11201, 11231, 11262, 11292, 11323, 11354, 
11382, 11413, 11443, 11474, 11504, 11535, 11566, 11596, 11627, 
11657, 11688, 11719, 11747, 11778, 11808, 11839, 11869, 11900, 
11931, 11961, 11992, 12022, 12053, 12084, 12112, 12143, 12173, 
12204, 12234, 12265, 12296, 12326, 12357, 12387, 12418, 12449, 
12478, 12509, 12539, 12570, 12600, 12631, 12662, 12692, 12723, 
12753, 12784, 12815, 12843, 12874, 12904, 12935, 12965, 12996, 
13027, 13057, 13088, 13118, 13149, 13180, 13208, 13239, 13269, 
13300, 13330, 13361, 13392, 13422, 13453, 13483, 13514, 13545, 
13573, 13604, 13634, 13665, 13695, 13726, 13757, 13787, 13818, 
13848, 13879, 13910, 13939, 13970, 14000, 14031, 14061, 14092, 
14123, 14153, 14184, 14214, 14245, 14276, 14304, 14335, 14365, 
14396, 14426, 14457, 14488, 14518, 14549, 14579, 14610, 14641, 
14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883, 14914, 
14944, 14975, 15006, 15034, 15065, 15095, 15126, 15156, 15187, 
15218, 15248, 15279, 15309, 15340, 15371, 15400, 15431, 15461, 
15492, 15522, 15553, 15584, 15614, 15645, 15675, 15706, 15737, 
15765, 15796, 15826, 15857, 15887, 15918, 15949, 15979, 16010, 
16040, 16071, 16102, 16130, 16161, 16191, 16222, 16252, 16283, 
16314, 16344, 16375, 16405, 16436, 16467, 16495, 16526, 16556, 
16587, 16617, 16648, 16679, 16709, 16740, 16770, 16801, 16832, 
16861, 16892, 16922, 16953, 16983, 17014, 17045, 17075, 17106, 
17136, 17167, 17198, 17226), class = "Date"), MYAGM2EZM196N = c(3.499192e 12, 
3.493103e 12, 3.500177e 12, 3.498558e 12, 3.516345e 12, 3.549216e 12, 
3.54237e 12, 3.531781e 12, 3.543278e 12, 3.555327e 12, 3.587644e 12, 
3.687146e 12, 3.658509e 12, 3.660611e 12, 3.666889e 12, 3.698172e 12, 
3.727306e 12, 3.758294e 12, 3.725595e 12, 3.718421e 12, 3.725841e 12, 
3.740075e 12, 3.789869e 12, 3.920142e 12, 3.95326e 12, 3.911195e 12, 
3.929382e 12, 3.950654e 12, 3.979502e 12, 4.005344e 12, 4.021979e 12, 
3.991118e 12, 4.000412e 12, 4.019772e 12, 4.049147e 12, 4.142298e 12, 
4.137751e 12, 4.133651e 12, 4.143894e 12, 4.186038e 12, 4.177617e 12, 
4.186421e 12, 4.184938e 12, 4.176869e 12, 4.1825e 12, 4.187284e 12, 
4.210799e 12, 4.29963e 12, 4.348576e 12, 4.355581e 12, 4.38508e 12, 
4.422968e 12, 4.445379e 12, 4.492195e 12, 4.479923e 12, 4.461073e 12, 
4.508739e 12, 4.512933e 12, 4.565041e 12, 4.684363e 12, 4.655663e 12, 
4.644451e 12, 4.670235e 12, 4.706318e 12, 4.728195e 12, 4.768158e 12, 
4.758307e 12, 4.750263e 12, 4.792133e 12, 4.811015e 12, 4.875496e 12, 
4.981449e 12, 4.923614e 12, 4.951523e 12, 5.006352e 12, 5.052347e 12, 
5.109432e 12, 5.130101e 12, 5.124234e 12, 5.125966e 12, 5.136978e 12, 
5.157878e 12, 5.206044e 12, 5.297999e 12, 5.271712e 12, 5.273548e 12, 
5.310219e 12, 5.344467e 12, 5.377365e 12, 5.408012e 12, 5.42844e 12, 
5.397916e 12, 5.451083e 12, 5.490285e 12, 5.528865e 12, 5.632265e 12, 
5.637298e 12, 5.643364e 12, 5.680387e 12, 5.738282e 12, 5.778327e 12, 
5.858475e 12, 5.896526e 12, 5.859423e 12, 5.939643e 12, 5.976941e 12, 
6.002366e 12, 6.168737e 12, 6.134008e 12, 6.157331e 12, 6.212517e 12, 
6.316677e 12, 6.321177e 12, 6.386776e 12, 6.382249e 12, 6.360016e 12, 
6.461105e 12, 6.472185e 12, 6.535927e 12, 6.743791e 12, 6704078690000, 
6707663590000, 6830308980000, 6875257540000, 6928872610000, 7023576910000, 
7063718310000, 7044759300000, 7140136020000, 7229789160000, 7288789240000, 
7436873890000, 7449456190000, 7471674820000, 7544870830000, 7625570920000, 
7688719570000, 7734626560000, 7750323860000, 7759475670000, 7839898820000, 
7.972122e 12, 8018985080000, 8103056980000, 8101892210000, 8093799240000, 
8094023230000, 8164981320000, 8157431590000, 8186152570000, 8170120430000, 
8152961550000, 8153645390000, 8178413500000, 8169984240000, 8275090590000, 
8234925070000, 8210916120000, 8209451530000, 8268951740000, 8301215080000, 
8333155330000, 8337672910000, 8342473540000, 8344372870000, 8378476210000, 
8388300570000, 8472295450000, 8435754600000, 8415889110000, 8441078810000, 
8482013950000, 8488118150000, 8517982800000, 8522260990000, 8530699180000, 
8568031090000, 8555869480000, 8565193560000, 8.670606e 12, 8.64011e 12, 
8.648411e 12, 8.718042e 12, 8.721253e 12, 8.752512e 12, 8.810355e 12, 
8.833836e 12, 8.82621e 12, 8.866345e 12, 8.928497e 12, 8.955965e 12, 
9.044639e 12, 9.017783e 12, 9.052631e 12, 9.081338e 12, 9.103737e 12, 
9.1284e 12, 9.129744e 12, 9.157419e 12, 9.185109e 12, 9.179608e 12, 
9.218195e 12, 9.236345e 12, 9.212147e 12, 9.24422e 12, 9.272606e 12, 
9.27787e 12, 9.289816e 12, 9.328806e 12, 9.355707e 12, 9.393273e 12, 
9.44385e 12, 9.475081e 12, 9.497903e 12, 9.569281e 12, 9.668681e 12, 
9.749062e 12, 9.764289e 12, 9.810907e 12, 9.859662e 12, 9.914221e 12, 
9.946958e 12, 1.0011211e 13, 1.0037605e 13, 1.0068328e 13, 1.0124772e 13, 
1.01861e 13, 1.0215459e 13, 1.0274065e 13, 1.0312438e 13, 1.0348009e 13, 
1.0376779e 13, 1.0416638e 13, 1.0457644e 13, 1.0512054e 13, 1.0547979e 13, 
1.0573312e 13, 1.0590298e 13, 1.0667463e 13, 1.0686288e 13, 1.0744692e 13, 
1.079786e 13, 1.0876141e 13), EA19CPALTT01GYM = c(2.2, 2, 1.7, 
1.5, 1.5, 1.5, 1.6, 1.8, 1.7, 1.6, 1.8, 1.6, 1.2, 1.2, 1.2, 1.5, 
1.5, 1.5, 1.4, 1.3, 1.1, 1, 0.9, 0.8, 0.9, 0.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