在ggplot2中排序geom_bar的堆栈

时间:2017-07-24 20:28:55

标签: r ggplot2 geom-bar

我正在尝试重新排序geom_bar中的堆栈,这样“时间帧1”更接近x轴(见下文)。我已经查看了SO中的其他类似帖子,他们建议重新排序数据框以重新排序堆栈。然而,这对我没有用,我想知道是否还有其他办法。

这是一个可重复的例子:

name <- c("A", "B", "C", "A", "B", "C")
variable <- c("Time frame 1", "Time frame 1", "Time frame 1", "Time frame 2", "Time frame 2", "Time frame 2")
value <- rnorm(6, 20, 8)

practicedf <- data.frame(name, variable, value)

ggplot(data = rbind(pdf2, pdf1), mapping = aes(x = name, y = value, fill = variable)) + geom_bar(stat = "identity")

这给出了以下结果:

Instead of this order, I want time frame 1 to be closer to the x axis

我希望时间帧1更接近x轴

而不是这个顺序

正如我前面提到的,我读到改变数据帧的顺序可以实现这一点。下面是我尝试过的,但它没有成功,产生完全相同的情节。以下是代码:

pdf1 <- practicedf[1:3,]
pdf2 <- practicedf[4:6,]
revpdf <- rbind(pdf2, pdf1)

ggplot(data = revpdf, mapping = aes(x = name, y = value, fill = variable)) + geom_bar(stat = "identity")

0 个答案:

没有答案