0 votes
1 view
in R Programming by (4.6k points)

I would like to reproduce this plot, but ggplot2 it is very slow to finish and the plot returns empty at the end.

plot

My code:

library(ggplot2)

g <- ggplot(data = bigram_tf_idf2, aes(x = tf_idf, y = bigram)) + 

  geom_bar(stat = 'identity') +

  coord_flip()

g + facet_wrap(~ book, ncol = 2)

Sample data set:

bigram_tf_idf2 <- data.frame(book = c('Persuasion','Mansfield Park','Mansfield Park','Persuasion','Persuasion','Emma','Northanger Abbey','Sense & Sensibility','Emma','Pride & Prejudice'),

                            bigram = c('captain wentworth','sir thomas','miss crawford','lady russell','sir walter', 'miss woodhouse', 'miss tilney', 'colonel brandon', 'frank churchill', 'lady catherine'),

                            tf_idf = c(0.0535, 0.0515, 0.0386, 0.0371, 0.0356, 0.0305, 0.0286, 0.0269, 0.0248, 0.0247))

1 Answer

0 votes
by (22.4k points)

The error in your code is that you have mixed up the x and y-axis variables. Remember, on the x-axis, you plot the categorical variable and the y-axis should be the continuous variable.

To plot the data:

bigram_tf_idf2 <- data.frame(book = c('Persuasion','Mansfield Park','Mansfield Park','Persuasion','Persuasion','Emma','Northanger Abbey','Sense & Sensibility','Emma','Pride & Prejudice'),

                             bigram = c('captain wentworth','sir thomas','miss crawford','lady russell','sir walter', 'miss woodhouse', 'miss tilney', 'colonel brandon', 'frank churchill', 'lady catherine'),

                             tf_idf = c(0.0535, 0.0515, 0.0386, 0.0371, 0.0356, 0.0305, 0.0286, 0.0269, 0.0248, 0.0247))

p <- ggplot(data = bigram_tf_idf2, aes(x = bigram, y = tf_idf,

                                       fill=bigram)) + 

  geom_bar(stat = 'identity') +

  coord_flip()+

  theme_bw()

p + facet_wrap(~ book, ncol = 2)

Output:

image

...