英文:
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]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论