ERROR while rich displaying an object: Error: Aesthetics must be either length 1 or the same as the data (5): fill
目录
仿真数据:
#view first six lines of airquality dataset
head(airquality)
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
问题:
fill参数中的颜色配置要么是单色、要么和箱体的个数一致;
library(ggplot2)
#attempt to create multiple boxplots
ggplot(data = airquality, aes(x=as.character(Month), y=Temp)) +
geom_boxplot(fill=c('steelblue', 'red'))
解决:
#单色填充;
library(ggplot2)
ggplot(data = airquality, aes(x=as.character(Month), y=Temp)) +
geom_boxplot(fill=c('brown'))
#多色填充(和箱体个数一致)
library(ggplot2)
ggplot(data = airquality, aes(x=as.character(Month), y=Temp)) +
geom_boxplot(fill=c('brown', 'grey', 'yellow', 'green', 'orange'))
完整错误:
ERROR while rich displaying an object: Error: Aesthetics must be either length 1 or the same as the data (5): fill Traceback: 1. FUN(X[[i]], ...) 2. tryCatch(withCallingHandlers({ . if (!mime %in% names(repr::mime2repr)) . stop("No repr_* for mimetype ", mime, " in repr::mime2repr") . rpr <- repr::mime2repr[[mime]](obj) . if (is.null(rpr)) . return(NULL) . prepare_content(is.raw(rpr), rpr) . }, error = error_handler), error = outer_handler) 3. tryCatchList(expr, classes, parentenv, handlers) 4. tryCatchOne(expr, names, parentenv, handlers[[1L]]) 5. doTryCatch(return(expr), name, parentenv, handler) 6. withCallingHandlers({ . if (!mime %in% names(repr::mime2repr)) . stop("No repr_* for mimetype ", mime, " in repr::mime2repr") . rpr <- repr::mime2repr[[mime]](obj) . if (is.null(rpr)) . return(NULL) . prepare_content(is.raw(rpr), rpr) . }, error = error_handler) 7. repr::mime2repr[[mime]](obj) 8. repr_text.default(obj) 9. paste(capture.output(print(obj)), collapse = "\n") 10. capture.output(print(obj)) 11. withVisible(...elt(i)) 12. print(obj) 13. print.ggplot(obj) 14. ggplot_build(x) 15. ggplot_build.ggplot(x) 16. by_layer(function(l, d) l$compute_geom_2(d)) 17. f(l = layers[[i]], d = data[[i]]) 18. l$compute_geom_2(d) 19. f(..., self = self) 20. self$geom$use_defaults(data, self$aes_params, modifiers) 21. f(..., self = self) 22. check_aesthetics(params[aes_params], nrow(data)) 23. abort(glue("Aesthetics must be either length 1 or the same as the data ({n}): ", . glue_collapse(names(which(!good)), ", ", last = " and "))) 24. signal_abort(cnd)