gpt4 book ai didi

r - 使用 geom_tile 和高度改变瓷砖之间的分隔?

转载 作者:行者123 更新时间:2023-12-04 12:31:32 37 4
gpt4 key购买 nike

使用 geom_tile,我想在每行数据之间放置一条水平线。看起来如果我在美学上使用颜色,它会在水平和垂直方向上放置线条,这不是我想要的。过去,我只是让瓷砖的高度变小,所以它们之间有一些间隔。但是,似乎行与行之间的空间量不同?这是为什么,有没有办法使分离始终相同?

enter image description here

library(ggplot2)
gn <- c(rep("gn1", 5), rep("gn2", 5), rep("gn3", 5), rep("gn4", 5), rep("gn5", 5),
rep("gn6", 5), rep("gn7", 5), rep("gn8", 5), rep("gn9", 5), rep("gn10", 5),
rep("gn11", 5), rep("gn12", 5), rep("gn13", 5), rep("gn14", 5), rep("gn15", 5),
rep("gn16", 5), rep("gn17", 5), rep("gn18", 5), rep("gn19", 5))

smpl <- rep(c("smpl1", "smpl2", "smpl3", "smpl4", "smpl5"), 19)

mut1 <- c(0, 2, 3, 3, 0)
mut2 <- c(0, 2, 2, 0, 3)
mut3 <- c(2, 0, 2, 0, 3)
mut4 <- c(0, 0, 2, 2, 3)
mut5 <- c(0, 2, 3, 3, 2)

mut <- c(mut1, mut2, mut3, mut4, mut5, mut2, mut3, mut1, mut4, mut2,
mut5, mut1, mut4, mut5, mut5, mut1, mut3, mut1, mut4)

mut <- factor(mut, levels=c(0,2,3))

tst <- data.frame(gn=gn, smpl=smpl, mut=mut)

ggplot(tst, aes(x=smpl, y=gn, fill=mut, height=0.95)) +
geom_tile() +
guides(fill=F) +
ylab("Genes") +
xlab("Sample") +
scale_fill_manual(values=c("#FFFFFF", "#00BFC4", "#F8766D")) +
theme(panel.grid.major.y=element_blank()) +
theme(panel.grid.major.x=element_blank()) +
theme(panel.grid.minor.y=element_blank()) +
theme(panel.grid.minor.x=element_blank()) +
theme(panel.background=element_rect(fill="#000000")) +
theme(axis.ticks.x=element_blank(),
axis.text.x=element_blank(),
axis.title.x=element_blank())

最佳答案


n1 <- length(unique(tst$gn))
n2 <- length(unique(tst$smpl))

分别为 y 轴和 x 轴的部分数。然后我们可以利用 geom_line达到结果:
ggplot(tst) + 
geom_tile(aes(x = smpl, y = gn, fill = mut)) + # aes for geom_tile
guides(fill = FALSE) + ylab("Genes") + xlab("Sample") +
scale_fill_manual(values = c("#FFFFFF", "#00BFC4", "#F8766D")) +
theme(panel.grid.major.y = element_blank()) +
theme(panel.grid.major.x = element_blank()) +
theme(panel.grid.minor.y = element_blank()) +
theme(panel.grid.minor.x = element_blank()) +
theme(panel.background = element_rect(fill = "#000000")) +
theme(axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank()) +
geom_line(data = data.frame(x = c(0, n2) + 0.5, y = rep(2:n1, each = 2) - 0.5),
aes(x = x, y = y, group = y)) # Horizonal lines

enter image description here

关于r - 使用 geom_tile 和高度改变瓷砖之间的分隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49141038/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com