Search code examples
rwaffle-chart

Error when creating waffle chart - object cannot be coerced


Attempting to make a simple waffle chart with the below vector pie:

  rideable_type number_of_trips
  <ord>                   <dbl>
1 Classic                    58
2 Electric                   36
3 Docked                      6

The function I am using is:

waffle(pie, rows=6)

This function returns the following error:

Error in FUN(X[[i]], ...) : 
  'list' object cannot be coerced to type 'double'

I'm sure that error is because rideable_type is characters, because when I run:

waffle(pie$number_of_trips, rows=6)

It runs just fine, but the legend names are A, B, C. I just want the the legend to correspond to the rideable_type. I'm sure I could do that manually...but it seems unnecessary. Thank you.


Solution

  • The first argument needs to be a named vector, so you can do:

    waffle(setNames(df$number_of_trips, df$rideable_type), rows = 6)
    

    enter image description here