我正在使用ggplot
创建这个“barplot”,但我希望能够重新定位每个类别中的条形图,以便最高的条形图位于顶部。简而言之,每个类别都有一个从高到低排序。
以下是我的代码 - 欢迎任何提示 - 谢谢
library("ggplot2")
d <- read.csv('http://db.tt/EOtR3uh', header = F)
d$V4 <- factor(d$V2, levels=d$V2)
base_size <- 11
ggplot(d, aes(d$V4, -log10(d$V3), fill=d$V1)) +
geom_bar(stat="identity") +
coord_flip() +
labs(y = "-log10(Pvalues)",x = "",fill="") +
theme_grey(base_size = base_size) +
scale_x_discrete(expand = c(0, 0))
答案 0 :(得分:4)
相应地对您的等级进行排序
d <- read.csv('http://db.tt/EOtR3uh', header = F, stringsAsFactors=FALSE)
lvls <- d$V2[order(d$V1,-d$V3)]
d$V4 <- factor(d$V2, levels=lvls)
答案 1 :(得分:2)
实现同一目标的另一种方式
require(ggplot2)
d = arrange(d, V1, -V3) # arrange d by V1 and -V3
d = transform(d, V2 = factor(V2, as.character(V2))) # order V2 as in d
qplot(V2, -log10(V3), fill = V1, geom = 'bar', data = d) +
coord_flip()
答案 2 :(得分:0)
我在这里回答我的问题:
要改变的行是
d$V4 <- ordered(d$V2, levels=d$V2[order(d$V1,-d$V3)])
完整代码:
library("ggplot2")
d <- read.csv('http://db.tt/EOtR3uh', header = F)
d$V4 <- ordered(d$V2, levels=d$V2[order(d$V1,-d$V3)])
base_size <- 11
ggplot(d, aes(d$V4, -log10(d$V3), fill=d$V1)) +
geom_bar(stat="identity") +
coord_flip() +
labs(y = "-log10(Pvalues)",x = "",fill="") +
theme_grey(base_size = base_size) +
scale_x_discrete(expand = c(0, 0))