r - ggplot2 在同一个图块中绘制组变量的所有级别
问题描述
我想为每个基因绘制“x”的每个值,以及它所属的三个非排他“类型”(组变量)的每个级别。是否可以将正方形或平铺拆分为组变量的三个级别,或者将每个级别彼此相邻显示,而不是如下所示的不同方面?
例如,如果它是另一个图,我可以使用“对”并抖动组变量。
library(ggplot2)
dat <- data.frame(gene = factor(c(rep('gene1',2), rep('gene2',2), rep('gene2',2),
rep('gene3',2), rep('gene3',2), rep('gene1',2))),
x = factor(c(rep('a',3), rep('a',3), rep('b',3), rep('c',3))))
dat$type <- factor(rep(1:3))
library(ggplot2)
ggplot(dat, aes(gene, x, fill=type)) + geom_tile() + facet_grid(type ~ .)
解决方案
我是否理解正确你想要这个:
library(ggplot2)
ggplot(dat, aes(gene, type, fill=type)) + geom_tile() + facet_grid(x~.)
编辑:也许你需要做一个马赛克图。在这种情况下,“ggmosaic”包可能是解决方案。它会给你一个看起来像这样的结果:
#To install:
devtools::install_github("haleyjeppson/ggmosaic")
library(ggmosaic)
ggplot(data=dat) + geom_mosaic(aes(x=product(x, gene), fill = type), divider = ddecker())
推荐阅读
- php - 输入复选框togle
- python-3.x - 根据另一个数据框中的值查找熊猫数据框中的间隔
- c - C中的类型声明问题(斐波那契数)
- cygwin - cygwin sshd 在挂起/恢复后旋转一个完整的 CPU 内核(Windows 10)
- excel - 删除旧的行日期改为添加另一个 Tue
- yii - yii2名称字段属性使用变量
- python - Softmax 输出层。哪个维度?
- javascript - 如何在 javascript 文件中获取数组列表?
- elasticsearch - Elasticsearch - 提供多字段值
- storybook - 如何在侧边栏页脚中显示我的故事书的自定义版本?