Tdebeus
Tdebeus

Reputation: 1599

ggplot doesn't sort values like it supposed to

I always like to sort my charts in the dataframe instead of in ggplot with the reorder() function this tactic works most of the time but sometimes, even if I won't change anything, the charts order chages to an alphabetic order...

Dataframe:

library(tidyverse)

most_used_words %>%
    arrange(desc(times_used)) %>%
    top_n(5)

 A tibble: 20 x 2
         word times_used
        <chr>      <int>
 1       news        148
 2       fake        147
 3     people        133
 4    country         95
 5        tax         92

most_used_words %>%
  arrange(desc(times_used)) %>%
  top_n(5) %>%
    ggplot(aes(x = word, y = times_used)) +
    geom_col(fill = "#03A9F4") +
    coord_flip()

While the following code sorts the times_used variable correctly in ggplot...

most_used_words_candidate %>%
      arrange(desc(times_used)) %>%
      top_n(5)

    # A tibble: 20 x 2
              word times_used
            <fctr>      <int>
 1 realdonaldtrump        965
 2           trump        762
 3          people        489
 4         hillary        435
 5         america        350

most_used_words_candidate %>%
  arrange(desc(times_used)) %>%
  top_n(5) %>%
    ggplot(aes(x = word, y = times_used)) +
    geom_col(fill = "#03A9F4") +
    coord_flip()

Upvotes: 1

Views: 954

Answers (1)

user5249203
user5249203

Reputation: 4648

@aosmith is right. ggplot expects the inputs as factors with levels. That is the reason your second code is properly ordered. If you try this

df$word  <- factor(df$word , levels=unique(df$word ))

and re-plot the 1st code. It will order by alphabetical order like you prefer.

Some resource here

Upvotes: 2

Related Questions