可能以前有人問過這個問題,但我沒有找到解決方案。我有一個看起來像這樣的 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 軸上繪制每個子組的標簽?
uj5u.com熱心網友回復:
問題是您映射1:nrow(...)并x修復了 x scale 的限制limits=factor(1:nrow(fig13_melt))。按照評論中的建議這樣做scales="free_x"是行不通的。
相反,HLA我x建議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")
uj5u.com熱心網友回復:
有3個部分需要修改:
- 使用
x = factor(1:nrow(fig13_melt))inaes()使 x 軸離散而不是連續。這樣,你也可以通過設定來改變x軸的標簽levels=...順序factor()。
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")
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/491414.html
上一篇:具有超出范圍值(日期)的啞鈴圖
