尝试在ggplot中使用stat_function时收到错误

mmyoung77

所以我的示例数据是:

x <- runif(1000, min = 0, max = 5)
y <- (2 / pi) * atan(x)
z <- floor(x)
df <- data.frame(x, y, z)

我画出x的箱线图,并由z合并:

library(ggplot2)
g <- ggplot(df, aes(x = x, y = y, group = z)) + 
  geom_boxplot()
g

在此处输入图片说明

但事实是,在我的实际数据中,我不确定y值是否遵循(2 / pi) * atan(x)那里有一个随机元素。因此,如何在图形上方绘制函数以自己查看?根据ggplot2文档,我尝试过...

g + stat_function(fun = (2 / pi) * atan(x), colour = "red")

...但是收到错误消息Warning message: Computation failed in 'stat_function()': 'what' must be a function or character string

吉米

错误是在说:

'what' must be a function or character string

所以它只是要求您定义函数。

您需要将函数定义为 func

func<-function(x){  (2 / pi) * atan(x)}

然后在ggplot中调用它

library(ggplot2)
g <- ggplot(df, aes(x = x, y = y, group = z)) + 
  geom_boxplot()
g+stat_function(fun = func, colour = "red")

这是结果

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用stat_function时向ggplot添加图例

ggplot2:在for循环中使用stat_function()获取颜色图例显示

无法使用ggplot2覆盖多个stat_function

在自定义函数中使用 ggplots stat_function

使用stat_function在ggplot2中绘制部分阴影的法线

使用ggplot2在分组数据上具有多个stat_function

使用ggplot2的stat_function绘制泊松分布

如何使用for循环在ggplot2中添加'stat_function()'层?

使用ggplot在网格中绘制两个stat_function()

ggplot2:为特定域添加stat_function?

用stat_function和ggplot阴影区域

将ggplot2与用户定义的stat_function()集成

尝试在Java SpringBoot中使用REST API时收到错误请求

R:将时序数据与'splinefun'和ggplot2'stat_function'一起使用

使用数据帧中的系数进入stat_function

更改使用stat_function绘制的线条的大小

R 中的 stat_function() 未使用警告消息“映射”

尝试在Python中使用GSM调制解调器拨打电话时收到“ NO CARRIER”错误

如何将stat_function美学映射到ggplot2中的数据?

ggplot2:将stat_function扩展到geom_violin

将ggplot2 stat_function添加到现有图形

将 ggplot aes 中的颜色分配给 stat_function 的颜色参数

ggplot2中的stat_function()是否可用于除矢量以外的args?

在我的代码中使用LISTAGG时收到错误

在 SSMS 中使用子查询进行更新时收到错误

stat_function 与 R 中的循环冲突

尝试更新Intellij时收到OutOfMemory错误

尝试调用方法时收到名称错误

尝试删除文件时收到错误?