2017-07-25 72 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