Error: Aesthetics must be either length 1 or the same as the data (5): fill

本文介绍了在使用R语言的ggplot2包创建箱型图时遇到的`Aesthetics must be either length 1 or the same as the data (5): fill`错误。问题在于`fill`参数的颜色配置长度不正确。解决方案包括两种情况:一是使用单色填充,二是确保颜色配置长度与箱体数量一致。示例代码展示了如何修正这一错误,从而成功绘制箱型图。
摘要由CSDN通过智能技术生成

ERROR while rich displaying an object: Error: Aesthetics must be either length 1 or the same as the data (5): fill

目录

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)
美学必须是长度为1或者与数据相同。 美学是关于审美和艺术的理论和实践。在这个简短的表述中,我们可以理解为美学的作用和限制。美学既可以被视为对个体审美感受的个人主观态度,也可以是社会和文化对艺术品和审美价值的共同认同。然而,不论是个人的还是社会的美学观点,它们的长度只能是1或者与所涉及的数据相同,这是因为美学观点的表达和理解需要简明扼要、一致性和连贯性。 首先,长度为1的美学观点意味着对于一个人或者一个社会来说,他们对于审美价值的理解只能是一个简洁而准确的表达或者共识。美学观点的表达需要简洁明了,以便传递和交流对于艺术和审美的认知和情感。过于复杂或冗长的表达会使美学观点难以理解和沟通。所以,美学观点长度为1可以视为一种简短而有效的表达形式。 其次,美学观点也必须与数据相同。这意味着美学观点必须与所涉及的艺术作品、审美经验和文化背景保持一致。美学观点需要基于与数据相匹配的事实和证据。否则,美学观点将变得虚无缥缈和不可信。因此,美学观点与数据相同可被视为一种理性和客观的表达方式,确保了其可靠性和可信度。 总而言之,美学观点必须是长度为1或者与数据相同。这个简洁的表述指出了美学的表达方式应当简练而具体,同时也要基于与事实相符的数据。通过这种方式,美学观点可以更好地在个人和社会之间传达和理解,促进艺术和审美价值的交流和共享。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Data+Science+Insight

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值