Search code examples
rggplot2forcats

How do I convince fct_infreq I'm working with a numerical vector?


I have put a great deal of effort into creating a new variable, andel, which i want to visualize on order of frequency using ggplot2.

I write:

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

I get an error message claiming that:

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

I've used every diagnostic tool as my disposal and "andel" seems to be a numerical vector. What exacly is the problem and how do I solve it?

Repex:

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

Solution

  • Per the documentation fct_infreq() works on factors, not numerics. That's okay though, you can just convert with as.factor

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

    That said, in your example there's only one occurrence of each value of andel

    enter image description here

    Also, when I run your example I get a slightly different error:

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

    This is my session info:

    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