重新排序级别

时间:2016-01-25 15:27:31

标签: r ggplot2

我想知道如何修改下面的代码,根据每个级别的年级频率对单元格进行重新排序。

Year      <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))
Category  <- c(rep(c("A", "B", "C", "D"), times = 4))
Frequency <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)
Data      <- data.frame(Year, Category, Frequency)

library(dplyr)  
Data <- group_by(Data,Year) %>%
 mutate(pos = cumsum(Frequency) - (0.5 * Frequency))

library(ggplot2)
p <- ggplot(Data, aes(x = Year, y = Frequency)) +
  geom_bar(aes(fill = Category), stat="identity", show.legend = FALSE) +
  geom_text(aes(label = Frequency, y = pos), size = 3, nudge_y = -25) +
  geom_text(aes(label = Category, y = pos), size = 3, nudge_y = 25)

1 个答案:

答案 0 :(得分:5)

尝试在分组后添加功能#library(dplyr) Data <- Data %>% group_by(Year) %>% arrange(Frequency) %>% #<---- Add this call mutate(pos = cumsum(Frequency) - (0.5 * Frequency)) #library(ggplot2) ggplot(Data, aes(x = Year, y = Frequency)) + geom_bar(aes(fill = Category), stat="identity", show.legend=FALSE) + geom_text(aes(label = Frequency, y = pos), size = 3, nudge_y = -25) + geom_text(aes(label = Category, y = pos), size = 3, nudge_y = 25)

{{1}}

https://github.com/syncany/syncany/blob/59cf87c72de4322c737f0073ce8a7ddd992fd898/syncany-lib/src/main/java/org/syncany/operations/watch/RecursiveWatcher.java