[英]subset x axis when using facet_wrap
可能以前有人问过这个问题,但我没有找到解决方案。 我有一个看起来像这样的 TSV (test.tsv):
TPM variants NOI_ID HLA NAL
324.28 several p1 one 2
169.21 NA p1 two 15
154.78 NA p1 three 0
143.31 NA p1 four 2
468.7 NA p2 five 0
322.76 several p2 six 2
620.98 NA p2 two 0
591.17 NA p2 seven 0
637.74 NA p3 eight 4
519.8 NA p3 nine 10
1439.58 NA p3 ten 23
122.05 NA p4 five 14
149.74 NA p4 eleven 77
213.7 NA p4 twelve 100
162.53 NA p4 one 15
使用此代码:
fig13 <- read.table("test.tsv", sep="\t", header=T, check.names=FALSE)
fig13 = fig13[-which(grepl("_w8", fig13$NOI_ID)),]
fig13_melt <- melt(fig13[c("TPM", "NOI_ID", "HLA", "variants")], id=c("NOI_ID", "HLA", "variants"))
hla_barplot = ggplot(fig13_melt, aes(x=1:nrow(fig13_melt), y=value, fill=variants)) +
geom_bar(stat="identity", position="dodge") + ggtitle("HLA status", subtitle = "all samples") + theme_bw() +
theme(axis.title.y=element_blank(), axis.title.x=element_blank(),
plot.title = element_text(face = "bold", size = 15, hjust = 0.5),
plot.subtitle=element_text(size=12, hjust=0.5, face="italic"),
axis.text.y = element_text(size=6),
axis.text.x = element_text(angle = 90, hjust=1, size=6)) +
geom_text(aes(label = round(value,0), vjust = 1, size = 1)) +
scale_x_discrete(labels=fig13_melt$HLA, breaks=1:nrow(fig13_melt), limits=factor(1:nrow(fig13_melt)), name='HLA')
hla_barplot
但是,当我尝试使用 facet_wrap 分割情节时:
hla_barplot + facet_wrap(.~ NOI_ID, nrow = 1)
我得到以下信息:
所以,据我所知,每个子图都有整个数据集的所有 x 标签。 有没有办法在 x 轴上绘制每个子组的标签?
问题是您在x
上映射了1:nrow(...)
并修复了 x scale limits=factor(1:nrow(fig13_melt))
的限制。 按照评论中的建议这样做scales="free_x"
将不起作用。
相反,我建议在x
上映射HLA
,摆脱scale_x_discrete
并使用scales="free_x"
释放你的比例。
如果您想重新排列条形图,我建议您相应地设置HLA
列的因子水平。 为此,我将一个帮助列 HLA2 添加到您的 df 中,我将行号添加到其中,以便您的两个“一个”类别变得唯一。 之后,我对scale_x_discrete
的标签参数使用函数来删除行号:
library(ggplot2)
fig13_melt$HLA2 <- paste0(fig13_melt$HLA, seq(nrow(fig13_melt)))
fig13_melt$HLA2 <- forcats::fct_inorder(fig13_melt$HLA2)
ggplot(fig13_melt, aes(x = HLA2, y = value, fill = variants)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(label = round(value, 0), vjust = 1, size = 1), show.legend = FALSE) +
labs(title = "HLA status", subtitle = "all samples", x = "HLA") +
theme_bw() +
theme(
axis.title.y = element_blank(), axis.title.x = element_blank(),
plot.title = element_text(face = "bold", size = 15, hjust = 0.5),
plot.subtitle = element_text(size = 12, hjust = 0.5, face = "italic"),
axis.text.y = element_text(size = 6),
axis.text.x = element_text(angle = 90, hjust = 1, size = 6)
) +
scale_x_discrete(labels = ~ gsub("\\d+$", "", .x)) +
facet_wrap(~NOI_ID, nrow = 1, scales = "free_x")
数据
fig13_melt <- structure(list(NOI_ID = c(
"p1", "p1", "p1", "p1", "p2", "p2",
"p2", "p2", "p3", "p3", "p3", "p4", "p4", "p4", "p4"
), HLA = c(
"one",
"two", "three", "four", "five", "six", "two", "seven", "eight",
"nine", "ten", "five", "eleven", "twelve", "one"
), variants = c(
"several",
NA, NA, NA, NA, "several", NA, NA, NA, NA, NA, NA, NA, NA, NA
), variable = structure(c(
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L
), levels = "TPM", class = "factor"), value = c(
324.28,
169.21, 154.78, 143.31, 468.7, 322.76, 620.98, 591.17, 637.74,
519.8, 1439.58, 122.05, 149.74, 213.7, 162.53
)), row.names = c(
NA,
-15L
), class = "data.frame")
有3个部分需要修改:
aes()
中使用x = factor(1:nrow(fig13_melt))
使 x 轴离散而不是连续。 这样,您还可以通过在factor()
中设置levels=...
来更改 x 轴的标签顺序。ggplot(fig13_melt, aes(x = factor(1:nrow(fig13_melt)), y = value, fill = variants))
limits = factor(1:nrow(fig13_melt))
scale_x_discrete()
; 否则,将显示 x 轴中的所有类别。scale_x_discrete(labels = fig13_melt$HLA,
breaks = factor(1:nrow(fig13_melt)),
name='HLA')
scales = "free_x"
添加到facet_wrap()
:facet_wrap(.~ NOI_ID, nrow = 1, scales = "free_x")
fig13_melt <- structure(list(NOI_ID = c("p1", "p1", "p1", "p1", "p2", "p2",
"p2", "p2", "p3", "p3", "p3", "p4", "p4", "p4", "p4"), HLA = c("one",
"two", "three", "four", "five", "six", "two", "seven", "eight",
"nine", "ten", "five", "eleven", "twelve", "one"), variants = c("several",
NA, NA, NA, NA, "several", NA, NA, NA, NA, NA, NA, NA, NA, NA
), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L), levels = "TPM", class = "factor"), value = c(324.28,
169.21, 154.78, 143.31, 468.7, 322.76, 620.98, 591.17, 637.74,
519.8, 1439.58, 122.05, 149.74, 213.7, 162.53)), row.names = c(NA,
-15L), class = "data.frame")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.