Вы можете сделать это с помощью ggplot2
и facet_grid
:
library(ggplot2)
library(dplyr)
# user data
v1 <- (rnorm(1e3, 3.5))
v2 <- (rnorm(1e3, 3))
v3 <- (rnorm(1e3, 2.75))
# Make df1 from user data and associate with add gear value in `mtcars`
df1 <- data.frame(wt = c(v1, v2, v3),
gear = as.factor(c(rep(3, 1000),
rep(4, 1000),
rep(5, 1000))))
# select data from mtcars and add user defined values (`val`)
df2 <- mtcars %>%
mutate(val = 1:32 * 0.01) %>%
remove_rownames() %>%
mutate(gear = as.factor(gear)) %>%
select(c(val, gear, wt))
ggplot(df2, aes(x = wt, y = val, #set up mapping with df2
fill = gear)) +
geom_density(data = df1, #make density plots of df1
aes(x = wt,
fill = gear),
inherit.aes = FALSE) + #next add points from df1
geom_point(data = df2, aes(x = wt, y = val), inherit.aes = FALSE) +
facet_grid(cols = vars(gear))
Вы можете сделать цвет заливки более прозрачным, добавив альфа-значение к geom_density
.