I am trying to plot 3 lines using geom_line() in my ggplot graph. One of the variables has a different scale so I am trying to use the sec.axis in order to show it as well.
library(ggplot2)
library(reshape2)
tab <- data.frame(year = seq(1979,2010), freq = runif(32, 212,283), max =
runif(32, 962.1, 993.4), med = runif(32, 972.1, 989.3), min = runif(32,
955.7, 978.3))
summary(tab) # the column freq (frequency) has a different scale comparing with the other ones
Some code I've tried.
tab <- melt(tab, id = c("year")) # melt the data
ggplot(tab, aes(x = year, y = value)) +
theme_bw() +
scale_colour_manual(values =c('red','blue', 'green')) +
geom_line() +
scale_y_continuous(limits = c(900,1000), sec.axis = sec_axis(~. *0.3)) # I am using limits in order to control the extent for the other variabiles besides 'freq'.
This shows the second y_axis but the variable 'freq' is missing. So far, I could not find an example showing how to plot multiple lines using a sec.scale as well for one variable. Also, I am not sure if I need to transform the 'freq' variable first or only to control the sec.axis for it so if anyone can explain me, I will be grateful.
Thanks!
