非二进制运算符的非数字参数 [英] non-numeric argument to binary operator

查看:965
本文介绍了非二进制运算符的非数字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在R中创建我的第一个函数。函数应该包含一个数据框,来自数据框的x系列,来自数据框的y系列以及绘制散点图。看起来很简单,但当我尝试检查可选的布尔参数时遇到麻烦。

R Script



I'm attempting to create my first function in R. The function should take in a data frame, x-series from data frame, y-series from data frame, and plot a scatter plot. Seems simple enough, but I run into trouble when I attempt to check for an optional boolean argument.

plotScatterChart <- function(data,x,y,scale=y,line=FALSE) {

    require(ggplot2)
    data$x <- as.numeric(x)
    data$y <- as.numeric(y)

    plot <- ggplot(data, aes(x, y)) + 
            geom_point() +  # aes(alpha=0.3,color=scale)
            #scale_color_gradient(high="red")

    if(line) {
        plot <- plot + geom_smooth(method="lm") 
    }

    ggsave(file="plot.svg", plot=plot, height=10, width=10)

    return(plot)
}

plotScatterChart(data=iris,x=iris$Petal.Length,y=iris$Petal.Width,line=TRUE)



错误



Error

non-numeric argument to binary operator



额外



欢迎提供其他改善此功能的建议。

Extra

Other suggestions for improving this function are welcome.

推荐答案

错误是因为<$ c c>后面的 + $ C> geom_point()。删除它,它应该工作。

The error is because of the trailing + after geom_point(). Remove that and it should work.

这篇关于非二进制运算符的非数字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆