Donbeo
Donbeo

Reputation: 17617

Overlay lines and hist with ggplot2

I'm creating a table with a lot of plot using ggplot

a=rnorm(30)
b=a*a
c=rnorm(30)
d=c
l=runif(30)
m=l+3
data=data.frame(A=a,B=b,ss=1)
data=rbind(data,data.frame(A=c,B=d,ss=2))

ggplot()+ geom_line(data=data,aes(A,B,group=ss),col="red")+facet_wrap(~ ss,as.table=T 

In each of this plots I have to overlap an histogram. How can I do?

Upvotes: 1

Views: 8716

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81693

Here's a way to do it:

ggplot() + 
  geom_line(data = data, aes(x = A, y = B), col = "red") +
  geom_histogram(data = data, aes(x = A), alpha = .5) +
  facet_wrap(~ ss,as.table=T) 

enter image description here

Upvotes: 3

Related Questions