生成一个函数,其中一个部分是箱线图,另一部分是密度图。

huangapple go评论58阅读模式
英文:

Ploting a box plot with a density plot side by side in a function

问题

我想要比较两个标记组之间的数据,使用箱线图和密度图。我创建了一个函数,以便更轻松地输入我需要的数据和要使用的特征,以及两个组的标签是什么(y将是一个二进制特征):

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() + labs(title = paste('Box plot of', cell.type))
  p2 = ggplot(data = combination, aes(x= cell.type, fill=as.factor(y))) + geom_density(alpha=.5) + labs(title = paste('Distribution of', cell.type))

  return(cowplot::plot_grid(p1, p2))
}

调用函数:

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

我得到空的图,这是为什么?

生成一个函数,其中一个部分是箱线图,另一部分是密度图。

英文:

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?

生成一个函数,其中一个部分是箱线图,另一部分是密度图。

答案1

得分: 0

将您的cell.type变量传递到aes()调用中的x = cell.type行将导致一个带有aes(x = "B-cells")而不是您可能希望的aes(x = B-cells)的图 - 即它传递了字符串而不是选择数据集中的现有变量。

请改用:x = .data[[cell.type]]

英文:

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]]

huangapple
  • 本文由 发表于 2023年2月23日 22:01:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545819.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定