1

I'm trying this code:

crowdfund_tweets_clean %>%
  top_n(15) %>%
  mutate(word = reorder(word, n)) %>%
  ggplot(aes(x = word, y = n)) +
  geom_col() +
  xlab(NULL) +
  coord_flip() +
    labs(x = "Count",
         y = "Unique words",
         title = "Count of Unique Words found in Tweets")

And getting the following error:

Error: Problem with mutate() column word. i word = reorder(word, n). x arguments must have same length

What am I doing wrong?

user438383
  • 4,338
  • 6
  • 23
  • 35
deb
  • 11
  • 1
  • Welcome to SO! To help us to help would you mind sharing [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Jan 17 '22 at 08:58
  • ... this said. One likely reason which gives rise to this error is that your dataframe does not contain a column named `n`. – stefan Jan 17 '22 at 09:00

1 Answers1

1

You could use fct_reorder from forcats package: Here is an example:

library(forcats)
library(dplyr)
library(ggplot2)

mtcars %>%
  top_n(15) %>%
  mutate(gear = factor(gear)) %>% 
  add_count(gear) %>% 
  mutate(mpg = fct_reorder(gear, n)) %>%
  ggplot(aes(x = gear, y = n)) +
  geom_col() +
  xlab(NULL) +
  coord_flip() +
  labs(x = "Count",
       y = "Unique words",
       title = "Count of Unique Words found in Tweets")

enter image description here

TarJae
  • 43,365
  • 4
  • 14
  • 40