Struggling to knit chunk to HTML or Word

I am trying to knit a chunk of code into an HTML file and a Word document, however, I keep receiving the following error: "Error: data must be a data frame, or other object coercible by fortify(), not an S3 object with class uneval Did you accidentally pass aes() to the data argument?"

Also there seems to be an issue with lines 38 through 42 of my code, but I am not sure what it is. My code is as follows:

library(ggplot2)
## Plot 1

p1 <- qplot(data = airquality,Temp,fill = Month,geom = "histogram", bins = 20)
p1


##Plot 2

p2 <- airquality %>%
+     ggplot(aes(x=Temp, fill=Month)) +
+     geom_histogram(position="identity", alpha=0.5, binwidth = 5, color = "white")+
+     scale_fill_discrete(name = "Month", labels = c("May", "June","July", "August", "September"))
p2


## Plot 3

p3 <- airquality %>%
+     ggplot(aes(Month, Temp, fill = Month)) + 
+     ggtitle("Temperatures") +
+     xlab("Months") +
+     ylab("Frequency") +
+     geom_boxplot() +
+     scale_fill_discrete(name = "Month", labels = c("May", "June","July", "August", "September"))
p3

## Plot 4

p4 <- airquality %>%
+     ggplot(aes(Month, Temp, fill = Month)) + 
+     ggtitle("Temperatures") +
+     xlab("Temperatures") +
+     ylab("Frequency") +
+     geom_boxplot()+
+     scale_fill_grey(name = "Month", labels = c("May", "June","July", "August", "September"))
p4

## Plot 5

p5 <- qplot(data = airquality,Ozone,fill = Month,geom = "histogram", bins = 20)
p5

I made two small corrections to the code and after that the code gives no longer an error (but warnings appear in the code):

  • because you use the pipe (%>%) I inserted library(magrittr). You probably used it already (otherwise a different error would have appeared).
  • I removed the + characters in front of the lines. A continuation of ggplot statements must be coded with + characters at the end of each 'command'. E.g. after ggplot.
    It is confusing that in the console window continued statements get + in front of them. But do not code these. Below I show your code with the corrections in a reprex. In the reprex output the front + characters are not shown.
library(ggplot2)
library(magrittr)

## Plot 1

p1 <- qplot(data = airquality,Temp,fill = Month,geom = "histogram", bins = 20)
p1



##Plot 2

p2 <- airquality %>%
       ggplot(aes(x=Temp, fill=Month)) +
       geom_histogram(position="identity", alpha=0.5, binwidth = 5, color = "white")+
       scale_fill_discrete(name = "Month", labels = c("May", "June","July", "August", "September"))
p2



## Plot 3

p3 <- airquality %>%
       ggplot(aes(Month, Temp, fill = Month)) + 
       ggtitle("Temperatures") +
       xlab("Months") +
       ylab("Frequency") +
       geom_boxplot() +
       scale_fill_discrete(name = "Month", labels = c("May", "June","July", "August", "September"))
p3
#> Warning: Continuous x aesthetic -- did you forget aes(group=...)?


## Plot 4

p4 <- airquality %>%
       ggplot(aes(Month, Temp, fill = Month)) + 
       ggtitle("Temperatures") +
       xlab("Temperatures") +
       ylab("Frequency") +
       geom_boxplot()+
       scale_fill_grey(name = "Month", labels = c("May", "June","July", "August", "September"))
p4
#> Warning: Continuous x aesthetic -- did you forget aes(group=...)?


## Plot 5

p5 <- qplot(data = airquality,Ozone,fill = Month,geom = "histogram", bins = 20)
p5
#> Warning: Removed 37 rows containing non-finite values (stat_bin).

Created on 2020-06-15 by the reprex package (v0.3.0)

Thank/ for the response. As far as the warnings, does the x axis always need to be a discrete variable as opposed to being continuous?

I think that the x variable should indeed by a discrete one.
But I do not use ggplot that often.
However Month is in fact a discrete variable. By using factor(Month) we make that explicit. Try

p3 <- airquality %>%
       ggplot() + 
       ggtitle("Temperatures") +
       xlab("Months") +
       ylab("Frequency") +
       geom_boxplot(aes(x=factor(Month), y=Temp,  fill = factor(Month))) +
       scale_fill_discrete(name = "Month", labels = c("May", "June","July", "August", "September"))
       
p3

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