user3022875
user3022875

Reputation: 9018

plotting dual axis in ggplot

I have been googling for some time and there is no easy way to do dual axis using ggplot? Seems odd because its a powerful package but cant do dual axis?

My problem is basic. I'd like to plot both data sets on same plot with 2 axis.

d1 = data.frame(x = rnorm(15),y = rnorm(15))
d2 = data.frame(x = rnorm(15),y = rnorm(15))
ggplot(data = d1, aes(x= x,y =y))+geom_line()
ggplot(data = d2, aes(x= x,y =y))+geom_line()

I do not want to use facet_wrap or facet_grid or align charts underneath one another.

Thoughts? Help from ggplot people?

Thank you.

Upvotes: 0

Views: 461

Answers (1)

Tyler Rinker
Tyler Rinker

Reputation: 109874

I added a function to a Github package, plotflow, I maintain that can do this. If you don't want to install the package just use the source code.

devtools::install_github('trinker/plotflow')
library(plotflow)

d1 = data.frame(x = rnorm(15),y = rnorm(15))
d2 = data.frame(x = rnorm(15),y = rnorm(15))

plotflow::ggdual_axis(
    ggplot(data = d1, aes(x= x,y =y))+geom_line(),
    ggplot(data = d2, aes(x= x,y =y))+geom_line()
)

enter image description here

Upvotes: 1

Related Questions