user7987707
user7987707

Reputation:

ggplot2: Create second y-axis on the right side for one variable

I have a dataframe with 3 different variables for the same time horizon. The first two variables have the same scaling (stock indice values) and can be illustrated with the y-axis. The third variable is an interest rate and ranges only from 0 to 7%, so that I focused on creating an additional y-axis on the right hand side of the plot to illustrate it. But after now 2 days trying and failing, I look for advice in your community. I first tried to separate my dataset and used to par(new=T) "overwrite" my first plot, but that led to more difficulties than before. I already found cases where people looked for a similar problem, but their questions were to special to replicate with my few skills.

library(reshape2)
library (scales)
library (ggplot2)

df <- data.frame(Variables, Dates)
df <- melt(df, id.vars="Dates") 

ggplot(df, aes(x=Dates, y=value, fill=variable, colour=variable))+geom_line(stat='identity', size=0.5)+ 
      scale_x_date(breaks = date_breaks("3 months"), labels = date_format("%b-%y"))+
      labs(x="Date", y="MSCI Value" )+
      theme_classic()+
      theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))+
      scale_color_manual(values=c("grey0", "orangered2", "royalblue2"))

Here some data (the first 30 observations) to replicate my plot and to illustrate what I mean.

Dates <- structure(c(8766, 8797, 8825, 8856, 8886, 8917, 8947, 8978, 9009, 
9039, 9070, 9100, 9131, 9162, 9190, 9221, 9251, 9282, 9312, 9343, 
9374, 9404, 9435, 9465, 9496, 9527, 9556, 9587, 9617, 9648), class = "Date")


Variables <- structure(c(1405.713, 1498.661, 1479.508, 1415.972, 1459.993, 
1464.001, 1460.193, 1488.212, 1533.288, 1493.268, 1536.017, 1469.67, 
1484.177, 1462.17, 1483.771, 1555.59, 1610.111, 1624.192, 1624.007, 
1705.582, 1667.891, 1716.796, 1690.085, 1749.089, 1800.553, 1833.446, 
1844.949, 1875.988, 1920.44, 1922.445, 3.05, 3.25, 3.34, 3.56, 
4.01, 4.25, 4.26, 4.47, 4.73, 4.76, 5.29, 5.45, 5.53, 5.92, 5.98, 
6.05, 6.01, 6, 5.85, 5.74, 5.8, 5.76, 5.8, 5.6, 5.56, 5.22, 5.31, 
5.22, 5.24, 5.27, 1226.99191666667, 1240.457375, 1253.96166666667, 
1267.07825, 1281.38133333333, 1293.99208333333, 1308.05641666667, 
1323.16016666667, 1338.992625, 1353.58925, 1371.2485, 1385.3055, 
1399.57704166667, 1412.76375, 1425.710875, 1438.80775, 1451.77004166667, 
1464.062625, 1476.80325, 1491.8025, 1502.652, 1516.61345833333, 
1527.86608333333, 1544.914875, 1561.36654166667, 1575.31591666667, 
1590.542625, 1609.70995833333, 1628.89525, 1647.99708333333), .Dim = c(30L, 
3L), .Dimnames = list(NULL, c("MSCI.WORLD", "Funds.Target.Rate", 
"Mean.Rolling")))

The variable "Funds.Target.Rate" should be presented in a more convenient way. Is it possible to create a second y-axis on the right side of the plot that refers to the interest rates of this variable? Thank you for your time and help.

Upvotes: 3

Views: 6307

Answers (1)

shosaco
shosaco

Reputation: 6165

As far as I know, this is not possible in ggplot2, for good reasons. see this discussion. The only thing possible is to add a second axis which is a re-calculation of the first axis, e.g. Celsius -> Fahrenheit or local differences:

ggplot(mpg, aes(displ, hwy)) + 
  geom_point() + 
  scale_y_continuous(
    "mpg (US)", 
    sec.axis = sec_axis(~ . * 1.20, name = "mpg (UK)")
  )

Having said that, you can use that hack for your purposes. First scale the interest rate, so that all variables have the same dimension (1000 - 2000 in your case) - I multiply Interest.Rate by 400. Then add the second axis and make sure the annotations show the unscaled (i.e. divided) values (* 300 and / 300 in your case):

Variables[, 2] <- Variables[, 2] * 300
df <- data.frame(Variables, Dates)
df <- melt(df, id.vars="Dates") 

ggplot(df, aes(x=Dates, y=value, fill=variable, colour=variable))+geom_line(stat='identity', size=0.5)+ 
    scale_x_date(breaks = date_breaks("3 months"), labels = date_format("%b-%y"))+
    labs(x="Date", y="MSCI Value" )+
    theme_classic()+
    theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))+
    scale_color_manual(values=c("grey0", "orangered2", "royalblue2")) + 

    # NEW CODE:
    scale_y_continuous("MSCI Value", sec.axis = sec_axis(~ . /300, name = "Interest Rate")
)

enter image description here

Upvotes: 1

Related Questions