0

I'm trying to create a geom bar, and order it in descending order. I'm not assigning any Y value to aesthetics, just X. Y value automatically takes the number of times that appear in the rows, as the variable count (showin in the geom bar).

Where I can find the value of 'count'? When I try to order the bars with reoder() function, it asks for a Y value. If I put count, it says 'arguments must have the same lenght'

  #unordered
    ggplot(data=gtd_top, aes( x = country_txt)) +
    geom_bar()

  #ordered, does not work
    ggplot(data=gtd_top, aes( x = reorder(country_txt, count), y = count)) +
      geom_bar()
  • 1
    By default the bars are in factor order. You can use the `forcats` package (which deals with factors) and try `aes(x = forcats::fct_infreq(country_txt))` – Paul Stafford Allen Feb 28 '23 at 19:28
  • As in the top answer at the linked FAQ, when you don't have a count variable defined you can have `reorder` compute it on the fly. In your case `x = reorder(country_txt ,country_txt, function(x) -length(x))` – Gregor Thomas Feb 28 '23 at 19:48

0 Answers0