[英]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.