重新排序无法正确排序栏

时间:2019-03-29 11:41:11

标签: r ggplot2

我正在尝试通过reorder()按数值对分面条进行排序,但似乎无法使它起作用。我在下面粘贴了我的方法。

enter image description here

    dat <- structure(list(item1 = c("word 2", "word 2", "word 2", "word 2", 
                                    "word 1", "word 1", "word 1", "word 2", "word 1", "word 1", "word 1", 
                                    "word 2", "word 2", "word 2", "word 1", "word 2", "word 1", "word 2", 
                                    "word 1", "word 1"), item2 = c("ir", "pr", "no", "mi", "wi", 
                                                                   "pr", "fe", "pa", "ti", "la", "pa", "ex",
                                                                   "cy", "se", "pe", "ti", 
                                                                   "qu", "qu", "se", "pr"), 
                          correlation = c(0.25456134079712, 0.2519936013674, 0.235943369717533, 
                                          0.227471817443391, 0.226966823585789, 0.211693332876911,
                                          0.209447353365894, 0.207110281022818, 0.19877845972944, 
                                          0.191606462402359, 0.186883665554187, 0.179251580064878, 
                                          0.17330384364747, 0.163131910906122, 0.163131910906122, 
                                          0.154238168542876, 0.153535076033027, 0.146798885015777, 
                                          0.144380422722292, 0.142845908676349)), 
                     class = c("tbl_df", "tbl", 
                               "data.frame"), row.names = c(NA, -20L))

    library(tidyverse)
    ggplot(dat, aes(x = reorder(item2, -correlation), y = correlation)) +
      geom_bar(stat = "identity") +
      facet_wrap(~ item1, scales = "free") +
      theme_classic() +
      coord_flip() +
      theme(axis.title.y=element_blank())

更新1:

我简化了示例item2变量,但保留了两个重复值“ a”和“ b”,它们与“单词1”和“单词2”相关。我还采纳了将max添加到reorder()的建议。

每个构面都按其correlationitem2进行排序,但是“单词1”构面中的“ b”显示为乱序。 “ b”出现在“ word 2”构面中,似乎正在推动订单。

enter image description here

dat <- structure(list(item1 = c("word 2", "word 2", "word 2", "word 2", 
                                "word 1", "word 1", "word 1", "word 2", "word 1", "word 1", "word 1", 
                                "word 2", "word 2", "word 2", "word 1", "word 2", "word 1", "word 2", 
                                "word 1", "word 1"), item2 = c("a", "b", "c", "d", "a", 
                                                               "f", "g", "h", "i", "j", "k", "l",
                                                               "m", "n", "o", "p", 
                                                               "q", "r", "s", "b"), 
                      correlation = c(0.25456134079712, 0.2519936013674, 0.235943369717533, 
                                      0.227471817443391, 0.226966823585789, 0.211693332876911,
                                      0.209447353365894, 0.207110281022818, 0.19877845972944, 
                                      0.191606462402359, 0.186883665554187, 0.179251580064878, 
                                      0.17330384364747, 0.163131910906122, 0.163131910906122, 
                                      0.154238168542876, 0.153535076033027, 0.146798885015777, 
                                      0.144380422722292, 0.142845908676349)), 
                 class = c("tbl_df", "tbl", 
                           "data.frame"), row.names = c(NA, -20L))

library(tidyverse)
ggplot(dat, aes(x = reorder(item2, correlation, max), y = correlation)) +
  geom_bar(stat = "identity") +
  facet_wrap(~ item1, scales = "free") +
  theme_classic() +
  coord_flip() +
  theme(axis.title.y=element_blank())

1 个答案:

答案 0 :(得分:2)

我可以帮助您,希望能帮助我们弄清楚...

我认为问题在于您需要创建一些额外的字段来帮助您进行排序。我发现了这篇帖子Ordering Categories Within ggplot2 Facets,似乎也正在使用tidytext。看起来好像作者西蒙·杰克逊(Simon Jackson)根据相关值(在本例中为correlation)排列了数据框。

在您的情况下,我首先将item1复制到一个名为word的新字段中,然后旋转数据,然后排列行。
现在唯一悬而未决的问题是您的y轴标签为order

library(tidyverse)
dat %>% 
  mutate(word = item1) %>% 
  spread(item1, item2) %>%
  arrange(desc(`word 2`),  correlation) %>% 
  mutate(order = row_number()) %>% 
  ggplot(aes(x = order, correlation)) + 
  geom_col() + 
  theme_classic() +
  coord_flip() +
  theme(axis.title.y=element_blank()) + 
  facet_wrap(~ word, scales = "free")

ordered facet plot

编辑:

想通了...
最终,将两者 item1item2(分别复制到wordletter),然后保存数据帧最有利于获得所需的结果。

dat2 <- dat %>% 
  mutate(word = item1, 
         letter = item2) %>% 
  spread(item1, item2) %>%
  arrange(desc(`word 2`),  correlation) %>% 
  mutate(order = row_number())

dat2 %>% 
  ggplot(aes(x = order, correlation)) + 
  geom_col() + 
  theme_classic() +
  coord_flip() +
  theme(axis.title.y=element_blank()) + 
  facet_wrap(~ word, scales = "free") + 
  scale_x_continuous(
    breaks = dat2$order,
    labels = dat2$letter
  )

enter image description here