gpt4 book ai didi

使用 R 中的 fct_reorder 根据值重新排序因子

转载 作者:行者123 更新时间:2023-12-01 18:56:16 25 4
gpt4 key购买 nike

我的数据:

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)不起作用并给我警告消息:因子级别不相等:强制为字符绑定(bind)字符和因子向量、强制为字符向量绑定(bind)字符和因子向量、强制为字符向量绑定(bind)字符和因子向量、强制为字符向量

这是代码:

   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()

它不是按降序绘制的。我错过了什么?

更新:mutate() 之前缺少 ungroup()。谢谢大家

最佳答案

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

您只需 group_byarrange相应地,则 ungroup并创建并索引。这样你就不用担心使用factor了,但您必须创建 breakslabelsscale_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

关于使用 R 中的 fct_reorder 根据值重新排序因子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46977508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com