r - 如何使用 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有上述三个值以外的值,“Errors: The plot_name is wrong.” 输出字符串。(5) 图形集合的标题是“y|x 的条件箱线图(或散点图或直方图)”,取决于输入的 plot_name 值。(提示:在 title 函数中使用 external =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)
}
}
推荐阅读
- apache-kafka - Jaeger/Opentracing 与 Kafka 和 docker compose
- azureportal - 指定时区以在 Azure 门户中查看信息?
- javascript - 如何将配置选项传递给 Rails 中的平面选取器?
- android - 在 Android Studio 上检查 CheckBox 时激活 EditText
- c# - GRCP拦截客户端
- reactjs - 调用表中的函数反应
- javascript - 用 else if 放宽 ESLint 规则
- python - Python - 将二进制(列表)转换为十进制
- sql - 向 Oracle 数据提取添加新列并填充数据
- python - 如何将numpy数组存储到csv文件