Skip to content

How to create a density plot where density is over all groups? #104

Answered by stefaneng
stefaneng asked this question in Q&A
Discussion options

You must be logged in to vote

Currently create.densityplot does not support this but we can create this plot using density function and create.scatterplot. We need to compute the density for each group and then scale by the number of data points in each groups.

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);

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by stefaneng
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
wontfix This will not be worked on
2 participants