gpt4 book ai didi

r - 将 ggplot2 aes_string 与箱线图一起使用的问题

转载 作者:行者123 更新时间:2023-12-04 10:11:35 27 4
gpt4 key购买 nike

我似乎在使用 ggplot2 时遇到了问题。

尝试使用 aes_string 绘制箱线图时出现以下错误:

Error: stat_boxplot requires the following missing aesthetics: x, y



下面是一个例子:
x='group'
y='value'
df=data.frame(group=c('A','A','B','B','C','C'),value=c(1,2,3,4,5,6))
ggplot(data=df,aes_string(x,y)) + geom_boxplot() #returns the error
ggplot(data=df,aes(x,y)) + geom_boxplot() #plots nonsense (naturally)
ggplot(data=df,aes(group,value)) + geom_boxplot() #works, but not strings

关于如何使用字符串进行这项工作的任何建议?

最佳答案

aes允许前两个参数未命名,并假定为 x 和 y(分别); aes_string没有这个快捷方式,因此所有参数都必须命名。
尝试:

ggplot(data=df,aes_string(x='group',y='value')) + geom_boxplot()

关于r - 将 ggplot2 aes_string 与箱线图一起使用的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14267562/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com