Rotate histogram in R or overlay a density in a barplot

后端 未结 5 835
一个人的身影
一个人的身影 2021-01-31 12:01

I would like to rotate a histogram in R, plotted by hist(). The question is not new, and in several forums I have found that it is not possible. However, all these answers date

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-31 12:44

    Thank you, Tim and Paul. You made me think harder and use what hist() actually provides.

    This is my solution now (with great help from Alex Pl.):

    scatterBar.Norm <- function(x,y) {
     zones <- matrix(c(2,0,1,3), ncol=2, byrow=TRUE)
     layout(zones, widths=c(5/7,2/7), heights=c(2/7,5/7))
     xrange <- range(x)
     yrange <- range(y)
     par(mar=c(3,3,1,1))
     plot(x, y, xlim=xrange, ylim=yrange, xlab="", ylab="", cex=0.5)
     xhist <- hist(x, plot=FALSE, breaks=seq(from=min(x), to=max(x), length.out=20))
     yhist <- hist(y, plot=FALSE, breaks=seq(from=min(y), to=max(y), length.out=20))
     top <- max(c(xhist$density, yhist$density))
     par(mar=c(0,3,1,1))
     barplot(xhist$density, axes=FALSE, ylim=c(0, top), space=0)
     x.xfit <- seq(min(x),max(x),length.out=40)
     x.yfit <- dnorm(x.xfit, mean=mean(x), sd=sd(x))
     x.xscalefactor <- x.xfit / seq(from=0, to=19, length.out=40)
     lines(x.xfit/x.xscalefactor, x.yfit, col="red")
     par(mar=c(3,0,1,1))
     barplot(yhist$density, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE)
     y.xfit <- seq(min(y),max(y),length.out=40)
     y.yfit <- dnorm(y.xfit, mean=mean(y), sd=sd(y))
     y.xscalefactor <- y.xfit / seq(from=0, to=19, length.out=40)
     lines(y.yfit, y.xfit/y.xscalefactor, col="red")
    }
    

    For examples:

    require(MASS)
    #Sigma <- matrix(c(2.25, 0.8, 0.8, 1), 2, 2)
    Sigma <- matrix(c(1, 0.8, 0.8, 1), 2, 2)
    mvnorm <- mvrnorm(1000, c(0,0), Sigma) ; scatterBar.Norm(mvnorm[,1], mvnorm[,2])
    

    An asymmetric Sigma leads to a somewhat bulkier histogram of the respective axis.

    The code is left deliberately "unelegant" in order to increase comprehensibility (for myself when I revisit it later...).

    Niels

提交回复
热议问题