使用R

时间:2017-10-27 14:33:07

标签: r ggplot2 forcats

我的数据:

structure(list(LoB = c("C", "C", "C", "A", 
"A", "B", "C", "A", "A", "C", 
"A", "B", "C", "B", "A", "C", "B", 
"A", "B", "C", "A", "B", "B", "A", 
"B", "C", "A", "B", "C", "B"), word = c("speed", 
"connection", "call", "bt", "reliable", "reliable", "reliable", 
"expensive", "cheaper", "uk", "customer", "customer", "customer", 
"network", "broadband", "broadband", "signal", "price", "price", 
"price", "poor", "poor", "ee", "service", "service", "service", 
"excellent", "excellent", "excellent", "coverage"), word_total = c(68L, 
46L, 44L, 3138L, 3479L, 906L, 71L, 6096L, 2967L, 39L, 10405L, 
1429L, 113L, 676L, 5193L, 73L, 868L, 8763L, 814L, 139L, 4708L, 
659L, 530L, 19185L, 2253L, 136L, 7180L, 1227L, 69L, 1453L), word_prop_by_total_feedbacks = c(0.0656370656370656, 
0.0444015444015444, 0.0424710424710425, 0.0343378635677237, 0.0380692885124636, 
0.101603678367164, 0.0685328185328185, 0.0667060600091918, 0.0324666797977808, 
0.0376447876447876, 0.113857702492723, 0.160255691376023, 0.109073359073359, 
0.075810250084109, 0.0568248965924759, 0.0704633204633205, 0.0973421554334417, 
0.0958899612632132, 0.0912863070539419, 0.134169884169884, 0.0515177379467314, 
0.0739037792979702, 0.0594370303913872, 0.209933687873416, 0.252663451833576, 
0.131274131274131, 0.0785678331473092, 0.137602332623079, 0.0666023166023166, 
0.16294717954469)), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -30L), vars = "LoB", drop = TRUE, indices = list(
    c(3L, 4L, 7L, 8L, 10L, 14L, 17L, 20L, 23L, 26L), c(5L, 11L, 
    13L, 16L, 18L, 21L, 22L, 24L, 27L, 29L), c(0L, 1L, 2L, 6L, 
    9L, 12L, 15L, 19L, 25L, 28L)), group_sizes = c(10L, 10L, 
10L), biggest_group_size = 10L, labels = structure(list(LoB = c("A", 
"B", "C")), class = "data.frame", row.names = c(NA, -3L
), vars = "LoB", drop = TRUE, .Names = "LoB"), .Names = c("LoB", 
"word", "word_total", "word_prop_by_total_feedbacks"))

我正在尝试使用ggplot2进行投影,但fct_reorderdrlib::reorder_within(word, word_total, LoB)无效,并向我发出警告消息:Unequal factor levels: coercing to characterbinding character and factor vector, coercing into character vectorbinding character and factor vector, coercing into character vectorbinding character and factor vector, coercing into character vector

以下是代码:

   mutate(word = drlib::reorder_within(word, word_total, LoB)) %>% 

  mutate(word = forcats::fct_reorder(word, word_total)) %>%
  ggplot(aes(word, word_prop_by_total_feedbacks, fill = LoB)) +
  geom_col() +
#  drlib::scale_x_reordered()+
  facet_wrap(~ LoB, scales = "free") +
  coord_flip()

它没有按降序绘制。我错过了什么?

更新:ungroup()之前遗失mutate()。谢谢所有

3 个答案:

答案 0 :(得分:2)

与@austensen类似,但对factor厌恶使用不同的方法。

您可以相应地group_byarrange,然后ungroup并创建和索引。这样您就不必担心使用factor,但必须在breaks中创建labelsscale_x_continuous

library(ggplot2)
library(dplyr)

plot_data <- df %>% 
  group_by(LoB) %>% 
  arrange(word_total) %>% 
  ungroup() %>% 
  mutate(order = row_number())

ggplot(plot_data, aes(order, word_prop_by_total_feedbacks, fill = LoB)) +
  geom_col() +
  facet_wrap(~ LoB, scales = "free") +
  scale_x_continuous(breaks = plot_data$order, labels = plot_data$word) +
  coord_flip()

enter image description here

答案 1 :(得分:1)

这有点尴尬,因为你可以根据LoB的值以不同的顺序显示相同的单词。书中Tidy Text Mining就是一个很好的例子(参见本章8.4.3部分)。我按照这个来创建下面的内容。

基本上,您必须订购word作为wordLoB的串联,然后在图表上显示单词标签时删除LoB部分。


library(tidyverse)
library(forcats)

df %>% 
  group_by(LoB) %>% 
  arrange(desc(word_total)) %>% 
  ungroup() %>% 
  mutate(word = factor(paste(word, LoB, sep = "__"), 
                       levels = rev(paste(word, LoB, sep = "__")))) %>%
  ggplot(aes(word, word_prop_by_total_feedbacks, fill = LoB)) +
  geom_col() +
  scale_x_discrete(labels = function(x) gsub("__.+$", "", x)) +
  facet_wrap(~ LoB, scales = "free") +
  coord_flip()

答案 2 :(得分:0)

您可能会考虑tidytext::reorder_within

df %>% 
  group_by(LoB) %>% 
  arrange(desc(word_total)) %>% 
  ungroup() %>% 
  ggplot(aes(word_prop_by_total_feedbacks, tidytext::reorder_within(word, word_prop_by_total_feedbacks, LoB), fill = LoB)) +
  geom_col() +
  facet_wrap(~ LoB, scales = "free") +
  scale_y_reordered()