R统计分析(72页)。
图示结果:
过程:
> #-- Create a scatterplot with marginal histograms --
> x <- pmin(3, pmax(-3, stats::rnorm(50))) # 取(-3,3)中的随机数50个
> y <- pmin(3, pmax(-3, stats::rnorm(50)))
> xhist <- hist(x, breaks = seq(-3, 3, 0.5), plot = FALSE) # 直方图向量,具体的变量值见后面
> yhist <- hist(y, breaks = seq(-3, 3, 0.5), plot = FALSE)
> top <- max(c(xhist$counts, yhist$counts)) #此时值17
> xrange <- c(-3, 3) #x轴取值范围
> yrange <- c(-3, 3)
> layout(matrix(c(2,0,1,3), 2, 2, byrow = TRUE), c(3,1), c(1,3), TRUE) #把画图区域分块
> layout.show(3)
>
> par(mar=c(3,3,1,1))
#mar
A numerical vector of the form c(bottom, left, top, right)
which gives the number of lines of margin to be specified on the four sides of the plot. The default is c(5, 4, 4, 2) + 0.1
.
> plot(x,y, xlim=xrange, ylim=yrange, xlab="", ylab="")
>
> par(mar=c(0, 3, 1, 1))
> barplot(xhist$counts, axes = FALSE, ylim = c(0, top), space = 0 )
>
> par(mar=c(3, 0, 1, 1))
> barplot(yhist$counts, axes = FALSE, xlim = c(0, top), space = 0, horiz = TRUE )
>
> x
[1] 0.02243276 -0.18802935 0.96635157 -0.06337091 -0.05320779 -0.25764274 -0.25904247 -0.16897359 0.99286435 -0.55518188 1.13752121 1.56176444 0.14995988 -0.60777977
[15] 0.02268866 -0.15339282 0.41942717 1.23640912 1.45951744 -0.08922257 -0.36184747 0.54985445 -0.74640503 0.31641000 -0.18868209 -0.38435261 0.59866851 0.37183951
[29] 0.15795068 0.05646891 -0.98648389 -0.90242599 -0.12530699 -0.97079945 0.14046142 0.37242721 0.40159553 -1.27605505 -0.88641508 -0.19047387 -1.46677651 -0.70993679
[43] -0.47961001 1.29435831 -0.90633420 -1.11204818 -0.25549535 -0.10933170 0.78662451 -0.29346732
> y
[1] 0.01929088 0.83942150 -2.29704677 0.99042037 0.42855322 -0.38710509 0.04268235 -0.94027175 -0.42339996 0.63979947 -0.96425531 -1.27256363 0.11811349 1.02663524
[15] -1.42605569 -0.78948625 0.40814836 -1.06689005 0.09741875 0.64487464 -0.70083433 0.77933314 -0.63003075 -1.21861028 0.98110932 1.75623956 1.47086212 0.47224524
[29] 0.31256526 0.96391536 -0.63679697 -0.26752053 1.85726370 -1.26925445 0.19747309 0.70264365 -0.88407886 -1.62868142 -0.39982539 1.40772579 1.14357730 -1.12897447
[43] 0.62227522 1.74595794 -0.73986816 0.92358474 -0.95305925 -0.72233704 -1.43089255 -0.47765949
> xhist
$breaks
[1] -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0
$counts
[1] 0 0 0 3 9 17 11 5 4 1 0 0
$intensities
[1] 0.00 0.00 0.00 0.12 0.36 0.68 0.44 0.20 0.16 0.04 0.00 0.00
$density
[1] 0.00 0.00 0.00 0.12 0.36 0.68 0.44 0.20 0.16 0.04 0.00 0.00
$mids
[1] -2.75 -2.25 -1.75 -1.25 -0.75 -0.25 0.25 0.75 1.25 1.75 2.25 2.75
$xname
[1] "x"
$equidist
[1] TRUE
attr(,"class")
[1] "histogram"
> yhist
$breaks
[1] -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0
$counts
[1] 0 1 1 7 10 5 9 10 4 3 0 0
$intensities
[1] 0.00 0.04 0.04 0.28 0.40 0.20 0.36 0.40 0.16 0.12 0.00 0.00
$density
[1] 0.00 0.04 0.04 0.28 0.40 0.20 0.36 0.40 0.16 0.12 0.00 0.00
$mids
[1] -2.75 -2.25 -1.75 -1.25 -0.75 -0.25 0.25 0.75 1.25 1.75 2.25 2.75
$xname
[1] "y"
$equidist
[1] TRUE
attr(,"class")
[1] "histogram"
> xrange
[1] -3 3
> yrange
[1] -3 3