在基础 R 中创建带有顶部水平箱线图的直方图。
要在基础 R 中创建带有顶部水平箱线图的直方图,我们首先需要使用 layout 函数和 par 函数的 margin (mar) 定义绘图区域的布局,然后创建箱线图,之后创建直方图。在创建箱线图和直方图时,我们需要确保箱线图的 ylim 和直方图的 xlim 相同。
查看下面的示例以了解如何实现。
示例
要在基础 R 中创建带有顶部水平箱线图的直方图,请使用以下代码片段:
x<-rnorm(100) layout(mat=matrix(c(1,2),2,1,byrow=TRUE),height=c(2,4)) par(mar=c(4,3,1,2)) boxplot(x,horizontal=TRUE,outline=FALSE,ylim=c(-4,4))
输出
如果执行上面给出的代码片段,它将生成以下输出:
要在基础 R 中创建带有顶部水平箱线图的直方图,请将以下代码添加到上述代码片段中:
x<-rnorm(100) layout(mat=matrix(c(1,2),2,1,byrow=TRUE),height=c(2,4)) par(mar=c(4,3,1,2)) hist(x,xlim=c(-4,4))
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输出
如果将上面给出的所有代码片段作为一个程序执行,它将生成以下输出:
广告