1

I'm trying to make a scatterplot with vertical histogram/plot of the distribution at every measurement occasion. I can make the Confidence interval lines.

enter image description here

This is an example. How do I do this in R/ggplot2?

This is a package request specifically if there existed one.

If there isn't a package, how do I custom code this?

1 Answers1

0

I don't know of a package in R that can do this. I tried with ggplot2, but couldn't overlay histograms onto a scatterplot.

I was able to overlay a basic scatterplot on a ggplot using par(new=TRUE). I think you can probably adapt this code to change the margins and axes to get a better overlay and closer to what you want.

df <- data.frame(y=rnorm(1000),x=sample(c(1:4),1000,T),stringsAsFactors = F)

ggplot(df,aes(y)) + geom_histogram(alpha=0.2) + facet_grid(~x,) + coord_flip() +
  theme_minimal() + theme(axis.title = element_blank(),axis.text = element_blank(),
                          strip.text = element_blank())

par(new=T,mai=c(0,0,0,0))

plot(y~x,df)

enter image description here

kstew
  • 1,104
  • 6
  • 21