I am working with the R programming language. I made the two following plots:
library(ggplot2)
library(cowplot)
data_1 = data.frame(a = rnorm(100,10,10), b = rnorm(100,10,10), c = as.factor("one"))
data_2 = data.frame(a = rnorm(100,10,10), b = rnorm(100,10,10), c = as.factor("two"))
p1 = ggplot(data_1, aes(x = a, y =
b))
geom_density_2d_filled()
ggtitle("Plot 1")
p2 = ggplot(data_2, aes(x = a, y =
b))
geom_density_2d_filled()
ggtitle("Plot 2")
plot_grid(p1, p2)
My Question: Is there a way to combine both of these plots in a single plot, such that it doesn't look "too messy"?
I tried the following code:
ggplot(final, aes(x = a, y =
b, colour = c))
geom_density_2d_filled()
ggtitle("Combined")
But this is very difficult to read.
Does anyone know how this can be done?
Thanks!
CodePudding user response:
Do you mean sth. like this? Here, a contour plot is used to overlap multiple 2d kernel densities:
library(ggplot2)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
set.seed(1337)
data_1 = data.frame(a = rnorm(100,10,10), b = rnorm(100,10,10), c = as.factor("one"))
data_2 = data.frame(a = rnorm(100,10,10), b = rnorm(100,10,10), c = as.factor("two"))
bind_rows(
data_1,
data_2
) %>%
ggplot(aes(a, b, color = c))
geom_density2d()
Created on 2021-12-07 by the reprex package (v2.0.1)