-
Originally asked by @jeeyunhan The default behavior for density plot is to plot independent densities where each group integrates to 1. suppressPackageStartupMessages(library(BoutrosLab.plotting.general));
set.seed(13);
n1 <- 500;
n2 <- 1000;
my.data <- list(
group1 = rnorm(n1, mean = 2),
group2 = rnorm(n2, mean = 4)
);
create.densityplot(
my.data,
col = default.colours(2),
main = 'Independent densities'
); Created on 2023-03-09 with reprex v2.0.2 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Currently suppressPackageStartupMessages(library(BoutrosLab.plotting.general));
set.seed(13);
n1 <- 500;
n2 <- 1000;
my.data <- list(
group1 = rnorm(n1, mean = 2),
group2 = rnorm(n2, mean = 4)
);
grp1.density <- density(my.data$group1);
grp2.density <- density(my.data$group2);
grp1.df <- as.data.frame(grp1.density[c('x', 'y')]);
grp1.df$group <- 1;
# Scale by multiplying by the ratio of: (# of group 1 )/(total count)
grp1.df$scaled.y <- grp1.df$y * n1 / (n1 + n2);
grp2.df <- as.data.frame(grp2.density[c('x', 'y')]);
grp2.df$group <- 2;
# Scale by multiplying by the ratio of: (# of group 2)/(total count)
grp2.df$scaled.y <- grp2.df$y * n2 / (n1 + n2);
combined.df <- rbind(grp1.df, grp2.df);
# Recreate density plot
create.scatterplot(
y ~ x,
data = combined.df,
groups = combined.df$group,
col = default.colours(2),
type = 'l'
); create.scatterplot(
scaled.y ~ x,
data = combined.df,
groups = combined.df$group,
col = default.colours(2),
type = 'l'
); Created on 2023-03-09 with reprex v2.0.2 |
Beta Was this translation helpful? Give feedback.
-
Yup fully agree with @stefaneng on the solution, I was going to offer the same strategy. I'd also put this as a feature we won't add in the near-term to |
Beta Was this translation helpful? Give feedback.
Currently
create.densityplot
does not support this but we can create this plot usingdensity
function andcreate.scatterplot
. We need to compute the density for each group and then scale by the number of data points in each groups.