Error non-numeric argument to binary operator

Hello!
I am trying to run this code but keep getting the error "Error in default + theme : non-numeric argument to binary operator"
Can anyone help?

hp_words%>%
group_by(book, word)%>%
anti_join(stop_words, by = "word")%>%
count()%>%
arrange(desc(n))%>%
group_by(book)%>%
mutate(top = seq_along(word))%>%
filter(top <= 15)%>%
ggplot(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()

Can you run this code

DF <- hp_words%>%
group_by(book, word)%>%
anti_join(stop_words, by = "word")%>%
count()%>%
arrange(desc(n))%>%
group_by(book)%>%
mutate(top = seq_along(word))%>%
filter(top <= 15)

dput(DF)

and then post here the output of dput(DF)?
Please put a line with three back ticks just before and after the pasted output, like this
```
Output of dput(DF) goes here
```
Forum users will then be able to run your ggplot() code and look for the problem.

My first thought is that the variable fs is not defined in your example. What is fs?

Hello! Thank you for your reply. Yes I can run that code. Here is the output for (DF):

tructure(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))

I think the issue might be that the section starting with "theme" is non numeric but the code is trying to add it by using the + and the software doesn't like that. I'm just not sure how to fix it.
And to answer your last question Fs = 12

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

That is so strange. Maybe there is something wrong in my settings?

Try copying the code from my post and running it on your system. If you can, delete all of the objects from your environment before you run the code. Do you get an error?

I still get the same error

I do not have any ideas at the moment. As a shot in the dark. What version of ggplot are you running? You can get this by running

sessionInfo()

The full output of that on my system is below. It is late for me now, so I will be off line for several hours. I hope someone else will have a suggestion.

sessionInfo()
R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] ggplot2_3.3.6

loaded via a namespace (and not attached):
 [1] rstudioapi_0.14  magrittr_2.0.3   tidyselect_1.1.2 munsell_0.5.0   
 [5] colorspace_2.0-3 R6_2.5.1         rlang_1.0.4      fansi_1.0.3     
 [9] dplyr_1.0.9      tools_4.2.1      grid_4.2.1       gtable_0.3.0    
[13] utf8_1.2.2       cli_3.3.0        DBI_1.1.3        withr_2.5.0     
[17] digest_0.6.29    assertthat_0.2.1 tibble_3.1.8     lifecycle_1.0.1 
[21] farver_2.1.1     purrr_0.3.4      vctrs_0.4.1      glue_1.6.2      
[25] labeling_0.4.2   compiler_4.2.1   pillar_1.8.1     generics_0.1.3  
[29] scales_1.2.1     pkgconfig_2.0.3 

I was able to fix the error! Earlier in my code I had written "theme_set(theme_light)" instead of "theme_set(theme_light())"
Amazing how that one small detail was so important.

This topic was automatically closed 21 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.