Plotting a word frequency table by descending frequency

I'm doing a word frequency analysis. I've got data that look like this:
word freq
get 1672
just 1639
good 1402
like 1227
know 1175
day 1150
new 110

my ggplot2 code is this:
ggplot('data', aes(x= word, y= freq)) +
geom_bar(stat="identity") +
coord_flip() +
labs(x = NULL, y = "Frequency") +
labs(title="Twitter ")

This plot works, no error messages. I would like to plot the words in order of decreasing frequency, with "get", "just" . . . "new". How to change my code to do this?

You can use reorder() function

df <- data.frame(stringsAsFactors=FALSE,
                 word = c("get", "just", "good", "like", "know", "day", "new"),
                 freq = c(1672, 1639, 1402, 1227, 1175, 1150, 110)
)

library(ggplot2)

ggplot(df, aes(x = reorder(word, freq), y = freq)) +
    geom_col() +
    labs(title="Twitter ",
         x = NULL,
         y = "Frequency") +
    coord_flip()

Created on 2019-04-04 by the reprex package (v0.2.1)

3 Likes

Thank you! It worked! :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.