[英]ggplot2 bar chart in order of fill group
我有一個看起來像這樣的數據集:
第一列包含所有唯一值,而第三列包含 3 個因子。 我想像這樣創建一個條形圖:
我一直試圖在 ggplot 中獲取它並且已經完成
ggplot(data, aes(as.factor(GO), pvalue, fill = Process)) +
geom_bar(position = "dodge", width = 0.5, stat = "identity")
我該怎么做才能讓小組按順序排列酒吧?
提前致謝!
這是一個示例數據:
GO <- c("T cell chemotaxis (GO:0010818)", "T cell chemotaxis (GO:0010818)",
"eosinophil chemotaxis (GO:0048245)", "CXCR3 chemokine receptor binding (GO:0048248)",
'hemokine activity (GO:0008009)', 'CXCR chemokine receptor binding (GO:0045236)',"cytoplasmic vesicle lumen (GO:0060205)",
"vesicle lumen (GO:0031983)", "collagen-containing extracellular matrix (GO:0062023)
")
pvalue <- c(2.49e-02, 3.07e-08, 1.90e-06, 2.22e-08, 1.72e-08, 1.63e-08, 6.18e-03, 3.87e-08
, 3.19e-07)
Process <- as.factor(rep(c("BP", "CP", "MP"),c(3,3,3)))
data <- data.frame(GO, pvalue,process)
這能解決您的問題嗎?
GO <- c("T cell chemotaxis (GO:0010818)", "T cell chemotaxis (GO:0010818)",
"eosinophil chemotaxis (GO:0048245)", "CXCR3 chemokine receptor binding (GO:0048248)",
'hemokine activity (GO:0008009)', 'CXCR chemokine receptor binding (GO:0045236)',"cytoplasmic vesicle lumen (GO:0060205)",
"vesicle lumen (GO:0031983)", "collagen-containing extracellular matrix (GO:0062023)
")
pvalue <- c(2.49e-02, 3.07e-08, 1.90e-06, 2.22e-08, 1.72e-08, 1.63e-08, 6.18e-03, 3.87e-08
, 3.19e-07)
Process <- as.factor(rep(c("BP", "CP", "MP"),c(3,3,3)))
data <- data.frame(GO, pvalue, Process)
data$GO <- factor(data$GO, levels=unique(GO[order(Process,GO)]), ordered=TRUE)
ggplot(data, aes(x = GO, y = -log(pvalue), fill = Process)) +
geom_col(position = "dodge", width = 0.5) +
theme_bw() +
coord_flip()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.