gpt4 book ai didi

r - 我如何说服 fct_infreq 我正在使用数值向量?

转载 作者:行者123 更新时间:2023-12-04 10:26:32 25 4
gpt4 key购买 nike

我付出了很多努力来创建一个新变量andel,我想使用ggplot2按频率顺序对其进行可视化。

我写的:

ggplot(df,aes())+geom_col(mapping=aes(x=NYA_REGION, y=fct_infreq(andel)))

我收到一条错误消息,声称:
Error: `f` must be a factor (or character vector or numeric vector).

我已经使用了所有诊断工具作为我的处置,“andel”似乎是一个数值向量。问题究竟是什么,我该如何解决?

复制品:
structure(list(NYA_REGION = structure(2:11, .Label = c("UTANFÖR SVERIGE", 
"ÖVRIGA LANDET", "STORGÖTEBORG", "oklart", "SÖDRA BOHUSLÄN",
"VÄSTERGÖTLAND", "VÄNERSBORG", "UDDEVALLA", "TROLLHÄTTAN", "DALSLAND",
"NORRA BOHUSLÄN"), class = c("ordered", "factor")), totstatus_tri = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("pågående studier",
"tidigt avbrott eller återbud", "sent avbrott"), class = c("ordered",
"factor")), ongoing = c(10L, 107L, 128L, 32L, 36L, 14L, 41L,
63L, 8L, 15L), antal = c(10L, 107L, 128L, 32L, 36L, 14L, 41L,
63L, 8L, 15L), andel = c(0.144927536231884, 0.36518771331058,
0.457142857142857, 0.450704225352113, 0.48, 0.4, 0.577464788732394,
0.456521739130435, 0.727272727272727, 0.681818181818182)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), groups = structure(list(
NYA_REGION = structure(2:11, .Label = c("UTANFÖR SVERIGE",
"ÖVRIGA LANDET", "STORGÖTEBORG", "oklart", "SÖDRA BOHUSLÄN",
"VÄSTERGÖTLAND", "VÄNERSBORG", "UDDEVALLA", "TROLLHÄTTAN",
"DALSLAND", "NORRA BOHUSLÄN"), class = c("ordered", "factor"
)), .rows = list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L)), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))

最佳答案

documentation fct_infreq()适用于因子,而不是数字。不过没关系,您可以使用 as.factor 进行转换

ggplot(df, aes()) + 
geom_col(aes(x = NYA_REGION, y = fct_infreq(as.factor(andel))))

也就是说,在您的示例中, andel 的每个值仅出现一次。

enter image description here

另外,当我运行你的例子时,我得到了一个稍微不同的错误:

Error: f must be a factor (or character vector).



这是我的 session 信息:
R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
other attached packages:
forcats_0.5.0 stringr_1.4.0 dplyr_0.8.3 purrr_0.3.3 readr_1.3.1
tidyr_1.0.2 tibble_2.1.3 ggplot2_3.3.0 tidyverse_1.3.0

关于r - 我如何说服 fct_infreq 我正在使用数值向量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60620562/

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