1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
library(cowplot)
p_main = ggplot(df, aes(x = wt, y = mpg, color = cyl))+
geom_point()
p_right = axis_canvas(p_main, axis = "x")+
geom_density(data = df, aes(x = wt, fill = cyl),
alpha = 0.7, size = 0.2)
p_top = axis_canvas(p_main, axis = "y", coord_flip = TRUE)+
geom_density(data = df, aes(x = mpg, fill = cyl),
alpha = 0.7, size = 0.2)+
coord_flip()
p1 = insert_xaxis_grob(p_main, p_right, grid::unit(.2, "null"), position = "top")
p2 = insert_yaxis_grob(p1, p_top, grid::unit(.2, "null"), position = "right")
ggdraw(p2)
|