Search code examples
rplotcut

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


This is the question that I need to solve for R. What I have worked is down below, but I currently can't find what's wrong with my code. Could you please help me?

When x, y objects are given, according to the x interval value changed to interval data Create a function plot_user1 that plots the different y values.

(1) Both x and y are given as numeric vectors of the same length, and both vectors are paired data. (2) cut_breaks is the number of sections designated by the user when dividing the numeric vector x into section data. Through this, a graph is drawn with y at the index position corresponding to x having the same interval value. Therefore, the number of graphs is drawn as much as the number of sections of x. (hint: use cut function) (3) plot_name indicates the shape of the graph to be drawn with the corresponding y values. It has one of three values: “boxplot”, “histogram”, and “scatter plot”. (4) color indicates the color of the graph

(1) All conditional graphs of y according to the x section value are drawn on one page. (2) The name of the x-axis of each graph is the level of the section, and the name of the y-axis is “y”. (3) In relation to the graph layout, the inner margin is c(4,4,2,2) and the outer margin is c(2,2,5,2). (4) If plot_name has a value other than the three values ​​mentioned above, "Errors: The plot_name is incorrect." string is output. (5) The title of the graph collection is “The conditional boxplot (or scatter plot or histogram) of y|x” and depends on the input plot_name value. (hint: use outer =TRUE argument to title function)

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 enter image description here

The plot that I have to draw needs to look like this.


Solution

  • I am not so sure if I understand you correctly but this gives the expected plots,

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