【问题标题】:Graph with ordered bars and using facets带有有序条形和使用构面的图形
【发布时间】:2018-10-26 19:46:16
【问题描述】:

我正在尝试根据频率制作一个带有有序条形图的图表,并且还使用一个变量,两个独立的两个变量使用构面。 单词必须按 'n' 变量中给出的值排序。 所以,我的图表应该看起来像 this one,它出现在 tidytext book 中:

我的图表如下,单词没有按值排序,我的错误是什么?: 我的数据看起来像示例中的数据:

> d
# A tibble: 20 x 3
   word    u_c            n
   <chr>   <chr>      <dbl>
 1 apples  candidate 0.567 
 2 apples  user      0.274 
 3 melon   user      0.191 
 4 curcuma candidate 0.105 
 5 banana  user      0.0914
 6 kiwi    candidate 0.0565
 ...

按照书中提供的代码,根据我的数据进行修改,代码如下:

d %>% 
  mutate(word = reorder(word, n)) %>%
  ggplot(aes(word, n, fill = u_c)) + 
  geom_col(show.legend = F) +
  facet_wrap(~u_c, scales = "free_y") +
  coord_flip()

这是dputd

d <- structure(list(word = c("apples", "apples", "melon", "curcuma", 
                             "banana", "kiwi", "grape", "curcuma", "grape", 
                             "wood", "satsuma", "melon", "raisin", "papaya", "plum", 
                             "plum", "papaya", "banana", "satsuma", "peach"), u_c = c("candidate", 
                                                                                      "user", "user", "candidate", "user", "candidate", "user", "user", 
                                                                                      "candidate", "user", "user", "candidate", "user", "candidate", 
                                                                                      "candidate", "user", "user", "candidate", "candidate", "candidate"
                             ), n = c(0.56704584625991, 0.273789875549109, 0.190633674260422, 
                                         0.105308514305412, 0.0914084706627656, 0.0565322302654257, 0.0562029558128485, 
                                         0.0547338017954938, 0.0498104102033781, 0.0439600056682254, 0.0393399851625864, 
                                         0.0380903136849362, 0.0370685271783074, 0.0370561875215443, 0.035849706997587, 
                                         0.0352763676677753, 0.0325506180866405, 0.0206825232678387, 0.0198207514650121, 
                                         0.0113753877973113)), class = c("tbl_df", "tbl", "data.frame"
                                         ), row.names = c(NA, -20L))

【问题讨论】:

标签: r ggplot2 tidytext


【解决方案1】:

除了 GordonShumway 非常相关的评论(因为那里提供的解决方案有点黑魔法) - 您可以创建一个临时值来重新排序您的方面 - 请注意我使用的是 forcats::fct_reorder 而不是 reorder

library(tidyverse)
d %>%
  mutate(temp = paste0(word, u_c)) %>%
  ggplot(aes(x = fct_reorder(temp, n), y = n, fill = u_c)) +
  geom_col(show.legend = F) +
  facet_wrap(~u_c, scales = "free_y") + 
  coord_flip()

【讨论】:

  • 唯一的问题是您的 x 轴刻度标签不太正确 - 您必须通过主题对其进行编辑
  • 添加这个:scale_x_discrete(labels = function(x) str_replace(x,"_candidate|_user", "")) 以一种快速但不优雅的方式解决了问题......
【解决方案2】:

鉴于@GordonShumway 提供的信息并遵循@CPak 给出的答案,我在下面提供了解决此问题的完整且“棘手”且不优雅的方法。几乎完整的答案(@CPak)和另外一行终于解决了这个问题:

d %>%
  mutate(temp = paste(word, u_c, sep = "_")) %>%
  ggplot(aes(x = fct_reorder(temp, n), y = n, fill = u_c)) +
  geom_col(show.legend = F) +
  scale_x_discrete(labels = function(x) str_replace(x,"_candidate|_user", "")) +
  facet_wrap(~u_c, scales = "free_y") + 
  coord_flip()

感谢您的回答!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    相关资源
    最近更新 更多