The data you posted plus setting fs to 12 does not produce an error for me.
library(ggplot2)
DF <- structure(list(book = structure(c(5L, 4L, 7L, 6L, 3L, 2L, 5L,
1L, 5L, 7L, 7L, 4L, 6L, 4L, 6L, 3L, 2L, 6L, 3L, 5L, 5L, 7L, 5L,
5L, 4L, 5L, 4L, 7L, 5L, 5L, 7L, 1L, 5L, 5L, 3L, 5L, 5L, 4L, 3L,
5L, 7L, 1L, 6L, 6L, 3L, 6L, 4L, 6L, 7L, 4L, 6L, 4L, 7L, 4L, 2L,
4L, 4L, 4L, 7L, 6L, 1L, 7L, 4L, 7L, 7L, 3L, 4L, 7L, 7L, 6L, 6L,
3L, 3L, 6L, 2L, 2L, 6L, 6L, 2L, 1L, 3L, 3L, 3L, 1L, 2L, 2L, 2L,
3L, 3L, 1L, 2L, 1L, 2L, 2L, 2L, 3L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), levels = c("philosophers_stone", "chamber_of_secrets",
"prisoner_of_azkaban", "goblet_of_fire", "order_of_the_phoenix",
"half_blood_prince", "deathly_hallows"), class = "factor"),
word = c("harry","harry", "harry", "harry", "harry", "harry", "hermione", "harry",
"ron", "hermione", "ron", "ron", "dumbledore", "hermione", "ron",
"ron", "ron", "hermione", "hermione", "sirius", "professor",
"wand", "dumbledore", "looked", "dumbledore", "umbridge", "looked",
"dumbledore", "weasley", "voice", "looked", "ron", "time", "hagrid",
"professor", "door", "head", "weasley", "lupin", "harry's", "voldemort",
"hagrid", "looked", "slughorn", "black", "snape", "hagrid", "malfoy",
"eyes", "eyes", "time", "moody", "death", "professor", "hermione",
"crouch", "time", "wand", "time", "professor", "hermione", "voice",
"voice", "harry's", "snape", "looked", "head", "door", "hand",
"ginny", "hand", "hagrid", "snape", "eyes", "malfoy", "lockhart",
"voice", "weasley", "professor", "professor", "harry's", "eyes",
"time", "looked", "weasley", "looked", "time", "dumbledore",
"malfoy", "snape", "eyes", "dumbledore", "harry's", "dobby",
"dumbledore", "door", "hagrid", "door", "uncle", "time", "dudley",
"harry's", "malfoy", "door", "eyes"),
n = c(3730L, 2936L, 2770L,
2581L, 1824L, 1503L, 1220L, 1213L, 1189L, 1077L, 1040L, 980L,
873L, 826L, 791L, 690L, 650L, 640L, 603L, 588L, 586L, 577L, 568L,
537L, 529L, 496L, 483L, 475L, 434L, 433L, 430L, 410L, 410L, 398L,
395L, 376L, 373L, 370L, 369L, 355L, 346L, 336L, 335L, 335L, 332L,
327L, 326L, 325L, 323L, 316L, 313L, 309L, 305L, 290L, 289L, 287L,
278L, 276L, 273L, 272L, 257L, 255L, 253L, 240L, 240L, 235L, 231L,
228L, 226L, 215L, 209L, 206L, 206L, 205L, 202L, 197L, 197L, 197L,
190L, 181L, 180L, 176L, 171L, 169L, 157L, 155L, 148L, 146L, 146L,
145L, 145L, 143L, 142L, 139L, 139L, 134L, 130L, 123L, 121L, 120L,
116L, 113L, 109L, 105L, 105L),
top = c(1L, 1L, 1L, 1L, 1L, 1L,
2L, 1L, 3L, 2L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 4L, 3L, 4L, 5L, 4L,
6L, 7L, 4L, 8L, 5L, 5L, 9L, 10L, 6L, 2L, 11L, 12L, 4L, 13L, 14L,
6L, 5L, 15L, 7L, 3L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 8L, 9L, 9L,
9L, 10L, 3L, 11L, 12L, 13L, 10L, 10L, 4L, 11L, 14L, 12L, 13L,
7L, 15L, 14L, 15L, 11L, 12L, 8L, 9L, 13L, 4L, 5L, 14L, 15L, 6L,
5L, 10L, 11L, 12L, 6L, 7L, 8L, 9L, 13L, 14L, 7L, 10L, 8L, 11L,
12L, 13L, 15L, 14L, 15L, 9L, 10L, 11L, 12L, 13L, 14L, 15L)),
class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -105L), groups = structure(list(
book = structure(1:7, levels = c("philosophers_stone", "chamber_of_secrets",
"prisoner_of_azkaban", "goblet_of_fire", "order_of_the_phoenix",
"half_blood_prince", "deathly_hallows"), class = "factor"),
.rows = structure(list(c(8L, 32L, 42L, 61L, 80L, 84L, 90L,
92L, 99L, 100L, 101L, 102L, 103L, 104L, 105L),
c(6L, 17L,55L, 75L, 76L, 79L, 85L, 86L, 87L, 91L, 93L, 94L, 95L, 97L, 98L),
c(5L, 16L, 19L, 35L, 39L, 45L, 66L, 72L, 73L, 81L, 82L, 83L, 88L, 89L, 96L),
c(2L, 12L, 14L, 25L, 27L, 38L, 47L, 50L, 52L, 54L, 56L, 57L, 58L, 63L, 67L),
c(1L, 7L, 9L, 20L, 21L, 23L, 24L, 26L, 29L, 30L, 33L, 34L, 36L, 37L, 40L),
c(4L, 13L, 15L, 18L, 43L, 44L, 46L, 48L, 51L, 60L, 70L, 71L, 74L, 77L, 78L),
c(3L, 10L, 11L, 22L, 28L, 31L, 41L, 49L, 53L, 59L, 62L, 64L, 65L, 68L, 69L)),
ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), row.names = c(NA, -7L),
class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))
fs <- 12
ggplot(DF,aes(x = -top, fill = book)) +
geom_bar(aes(y = n), stat = 'identity', col = 'black') +
geom_text(aes(y = ifelse(n > max(n) / 2, max(n) / 50, n + max(n) / 50),
label = word), size = fs/3, hjust = "left") +
theme(legend.position = 'none',
text = element_text(size = fs),
axis.text.x = element_text(angle = 45, hjust = 1, size = fs/1.5),
axis.ticks.y = element_blank(),
axis.text.y = element_blank()) +
labs(y = "Word count", x = "", title = "Harry Plotter: Most frequent words throughout the saga") +
facet_grid(. ~ book) + coord_flip()
Created on 2022-09-08 with reprex v2.0.2