This may be a bit late, but you can accomplish what you want from the ggside
package. You will need to use the dev
version from the github to use the the full functionality though.
Currently, the CRAN version of ggside
does not have (x|y)side
variants to stat_function()
, but the current dev branch has a version working.
Here we plot the normal density estimates filled in with blue, and then the theoretical distribution is colored red.
library(ggplot2)
library(ggside)
#> Registered S3 method overwritten by 'ggside':
#> method from
#> +.gg ggplot2
x<- rweibull(100, 2.6, 3)
y<- rweibull(100, 1.8, 3)
xy.df<- data.frame(cbind(x,y))
p <- ggplot(xy.df, aes(x, y)) +
geom_point(colour = "blue", size = 0.25) +
geom_density2d() +
geom_xsidedensity(fill = "blue", alpha = .3) +
geom_ysidedensity(fill = "blue", alpha = .3) +
stat_xsidefunction(fun = dweibull, args = list(shape = 1.8, scale = 3), colour = "red") +
stat_ysidefunction(fun = dweibull, args = list(shape = 2.6, scale = 3), colour = "red") +
theme_classic()
p

Created on 2022-02-01 by the reprex package (v2.0.1)