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

标签 r ggplot2

我似乎在使用 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/

相关文章:

R Shiny : How not to display plot when NULL

r - 如何增加 ggplot2 map 连续颜色填充的箱数

r - 如何协调 facet_wrap 中的轴并缩放 ="free_y"?

r - ggplot2 多边形 -> 错误 : Discrete value supplied to continuous scale

r - 根据 R 中的空间邻域和时间标准将行分配给组

r - 使用 R 将国家/地区名称转换为其母语?

r - 查找矩阵的所有行等于向量

r - 在R的ggmaps上加载 map PNG的栅格数据

r - 显示方位和距离的圆图

r - 获取ggplot2图例以在R中显示百分比符号