2017-07-25 101 views
2

在此先感謝您的幫助。我一直在尋找堆棧溢出和谷歌與這個問題,並沒有取得成功。如何做並排條形圖ggplot並保留原始排序

我需要一個並排的單詞水平條形圖和它的頻率在兩個文件中。

我的數據幀是如下:

head(comp,10) 
       WORD FREQ RDFREQ 
    170  project 67  5 
    20  business 64  14 
    117 management 53  13 
    59 development 34  4 
    211  support 27  6 
    215  systems 25  10 
    102 information 22  2 
    201 software 21  6 
    203 solutions 20  2 
    220 technical 20  2 

我已經使用熔體創建如下面的頻率幀:

dfp1 <- melt(comp, value.factor = TRUE) 
    head(dfp1,20) 
       WORD variable value 
    1  project  FREQ 67 
    2  business  FREQ 64 
    3 management  FREQ 53 
    4 development  FREQ 34 
    5  support  FREQ 27 
    6  systems  FREQ 25 
    7 information  FREQ 22 
    8  software  FREQ 21 
    9  solutions  FREQ 20 
    10 technical  FREQ 20 
    11 applications  FREQ 19 
    12  planning  FREQ 18 

我到情節代碼是

g <- ggplot(dfp1, aes(x = WORD, y= value, order=- as.integer(value))) 
    g <- g + geom_bar(aes(fill = variable), position = "dodge", stat="identity") 
    g <- g + coord_flip() 
    g <- g + theme(axis.text.y = element_text(size=12)) 
    g <- g + labs(x = "Keyword", y = "Count", 
       title = paste("File1 vs File2") 
      ) 
    print(g) 

的我得到的圖是按WORD排序的,而不是頻率的降序。再次感謝並期待着答覆。

+1

包* forcats *函數在這裏很有用。見[這個答案](https://stackoverflow.com/a/41417136/2461552) – aosmith

+0

太棒了。非常感謝@aosmith。 –

回答

0

將這項工作對你來說,

comp <- structure(list(WORD = structure(c(5L, 1L, 4L, 2L, 8L, 9L, 3L, 
6L, 7L, 10L), .Label = c("business", "development", "information", 
"management", "project", "software", "solutions", "support", 
"systems", "technical"), class = "factor"), FREQ = c(67L, 64L, 
53L, 34L, 27L, 25L, 22L, 21L, 20L, 20L), RDFREQ = c(5L, 14L, 
13L, 4L, 6L, 10L, 2L, 6L, 2L, 2L)), .Names = c("WORD", "FREQ", 
"RDFREQ"), class = "data.frame", row.names = c("170", "20", "117", 
"59", "211", "215", "102", "201", "203", "220")) 


comp %>% gather(variable, Count, -WORD) %>% 
     mutate(Keyword = fct_reorder(WORD, Count, .desc = FALSE)) %>% 
      ggplot(aes(x = Keyword, y = Count, fill = variable)) + 
      geom_bar(stat = 'identity', position = "dodge") + coord_flip() + 
      theme(axis.text.y = element_text(size=12)) + 
      labs(title = paste("File1 vs File2")) 

ordered geom_bar

如果你想以獨佔訂單上 FREQ你可以做這樣的事情

comp %>% arrange(desc(FREQ)) %>% mutate(id = row_number()) %>% 
     gather(variable, Count, -WORD, -id) %>%    
     mutate(Keyword = fct_reorder(WORD, id, .desc = TRUE)) %>% 
      ggplot(aes(x = Keyword, y = Count, fill = variable)) + 
      geom_bar(stat = 'identity', position = "dodge") + coord_flip() + 
      theme(axis.text.y = element_text(size=12)) 
      + labs(title = paste("File1 vs File2")) 

enter image description here