如何在 R 中使用 cut 函数绘制绘图?

神经网络

这是我需要为 R 解决的问题。我所做的工作在下面,但我目前找不到我的代码有什么问题。请你帮助我好吗?

当给定x,y对象时,根据x区间值改为区间数据创建一个函数plot_user1,绘制不同的y值。

(1) x 和 y 都是作为相同长度的数值向量给出的,并且两个向量都是成对的数据。(2) cut_breaks 是用户在将数值向量x分割成截面数据时指定的截面数。由此,在与具有相同间隔值的x对应的索引位置处以y绘制图形。因此,绘制的图形数量与 x 的部分数量一样多。(提示:使用 cut 函数) (3) plot_name 用对应的 y 值表示要绘制的图形的形状。它具有以下三个值之一:“箱线图”、“直方图”和“散点图”。(4) color表示图形的颜色

(1) 根据x截面值绘制y的所有条件图在一页上。(2)每张图的x轴名称为截面的级别,y轴名称为“y”。(3) 关于图形布局,内边距为 c(4,4,2,2),外边距为 c(2,2,5,2)。(4) 如果plot_name 有上述三个值以外的值,“错误:plot_name 不正确”。字符串是输出。(5) 图集合的标题是“y|x 的条件箱线图(或散点图或直方图)”,取决于输入的 plot_name 值。(提示:标题函数使用外部 =TRUE 参数)

plot_user1<-function(x,y,cut_breaks,plot_name,color) {
if(plot_name=="boxplot") {
    plot_name <- boxplot
    txt <- "The conditional boxplot of y|x"
    }
else if (plot_name == "histogram") {plot_name <- hist
    txt <- "The conditional histogram of y|x"
    }
else if (plot_name == "scatter plot") {plot_name <- plot
    txt <- "The conditional scatter plot of y|x"
    }
else {print("Errors: The plot_name is incorrect.")}
a<-cut(x,cut_breaks)
for (i in 1: cut_breaks) {
    plot_name(a,y,col=color,xlab=levels(a),ylab="y")
    title(main=txt, outer=TRUE)
    par(mar=c(4,4,2,2))
    par(oma=c(2,2,5,2))
}

}在这里输入图像描述 在此处输入图片说明

我必须绘制的情节需要看起来像这样。

迈丁

我不太确定我是否正确理解你,但这给出了预期的情节,

plot_user1<-function(x,y,cut_breaks,plot_name,color) {
    if(plot_name=="boxplot") {
        plot_name <- boxplot
        txt <- "The conditional boxplot of y|x"
        }
    else if (plot_name == "histogram") {plot_name <- hist
        txt <- "The conditional histogram of y|x"
        }
    else if (plot_name == "scatter plot") {plot_name <- plot
        txt <- "The conditional scatter plot of y|x"
        }
    else {print("Errors: The plot_name is incorrect.")}

    a<-cut(x,cut_breaks)
    par(mfrow=c(1,cut_breaks))


    for (i in levels(a)) {

        plot_name(y[levels(a)==i],col=color,xlab=i,ylab="y")
        title(main=txt, outer=TRUE)
    
    }
}

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章