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

How can I draw a plot by using cut function in R?

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

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

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

(1) y根据x截面值的所有条件图绘制在一页上。 (2) 每个图的x-axis名称为截面的层级,y-axis名称为“y”。 (3) 关于图形布局,内边距为c(4,4,2,2),外边距为c(2,2,5,2)。 (4)如果plot_name有上述三个值以外的值, “错误:plot_name 不正确。”字符串被输出。 (5) 图的标题collection是“The conditional boxplot (or scatter plot or 直方图)的 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))
}

} enter image description here

我要画的情节需要像这样。

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

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)
    
    }
}