1

Are there any ways to create a line chart that has multiple (≥ 3) y-axes using ggplot2? I managed to make it using base graphic. Any way to make this using ggplot2?

library(WDI)
c_name <- "PHL"
st <- 2000
en <- 2017

gdp <- WDI(country = c_name, indicator = "SL.GDP.PCAP.EM.KD", start = st, end = en, extra = FALSE, cache = NULL)
life.exp <- WDI(country = c_name, indicator = "SP.DYN.LE00.IN", start = st, end = en, extra = FALSE, cache = NULL)
health.exp <- WDI(country = c_name, indicator = "SH.XPD.CHEX.PC.CD", start = st, end = en, extra = FALSE, cache = NULL)

tab <- cbind(gdp, life.exp, health.exp)[,c("year", "SL.GDP.PCAP.EM.KD", "SP.DYN.LE00.IN", "SH.XPD.CHEX.PC.CD")]
names(tab) <- c("year","gdp", "life.exp", "health.exp")

plot(tab$year, tab$gdp, type="l", xlab="year", ylab="", bty="n")
par(new=TRUE, mgp = c(0, 3, 2)) 
plot(tab$year, tab$life.exp, type="l", xlab="", ylab="", bty="n", col="blue", axes=F)
axis(2, col="blue", col.axis="blue")
par(new=TRUE, mgp = c(0, -2, -3))
plot(tab$year, tab$health.exp, type="l", xlab="", ylab="", bty="n", col="red", axes=F)
axis(2, col="red", col.axis="red")
legend(2001, max(tab$health.exp, na.rm=T), legend=c("GDP per capita (constant 2011 PPP$)", "Life expectancy at birth (years)", "Current health expenditure per capita (current US$)"), col=c("black", "blue", "red"), lty=1, cex=0.8)
Fukushi
  • 87
  • 3
  • 1
    I don't think that there is an easy solution with `ggplot2`. [Here](http://www.steffi.ca/thinkR/tutorials/twoplots/) is an option for 2 axes. Maybe you can push this further. – prosoitos Oct 17 '19 at 09:03
  • 1
    Also, have a look [here](https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales): in particular, look at Hadley's answer. – prosoitos Oct 17 '19 at 09:05
  • Possible duplicate of [ggplot with 2 y axes on each side and different scales](https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales) – prosoitos Oct 17 '19 at 09:07

0 Answers0