0

I want to compare the data between to labeled groups, using box plots and density plots. I made a function for that, to make it easier ust entering the data I need and what feature I would like to use, and what is the label of the two groups (y would be a binary feature):

density_and_boxplot = function(data = data, metadata = metadata, cell.type = 'cell.type', y = c()) {

  combination = merge(data, metadata, by=0, all=TRUE) %>% na.omit()
  p1 = ggplot(data = combination, aes(x= cell.type, fill=as.factor(y))) + geom_boxplot() + coord_flip() + title(paste('Box plot of',cell.type))
  p2 = ggplot(data = combination, aes(x= cell.type, fill=as.factor(y))) + geom_density(alpha=.5) + title(paste('Distribtuion of',cell.type))
 
  return(cowplot::plot_grid(p1,p2))
}

Calling the function:

density_and_boxplot(data = scores, metadata = mydata, cell.type = 'B-cells', y = 'PD')

I get empty plots, why is this happening?

enter image description here

Kev
  • 375
  • 1
  • 7

1 Answers1

0

passing your cell.type variable to the line x = cell.type in the aes() calls leads to a plot with aes(x = "B-cells") NOT aes(x = B-cells) as you might hope - i.e. it passes the string instead of selecting the existing variable in your dataset.

Instead, use: x = .data[[cell.type]]

Paul Stafford Allen
  • 1,840
  • 1
  • 5
  • 16