Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8123255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:07:10+00:00 2026-06-06T06:07:10+00:00

I would like to rotate a histogram in R, plotted by hist(). The question

  • 0

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 back to 2010 or even later.

Has anyone found a solution meanwhile?

One way to get around the problem is to plot the histogram via barplot() that offers the option “horiz=TRUE”. The plot works fine but I fail to overlay a density in the barplots. The problem probably lies in the x-axis since in the vertical plot, the density is centered in the first bin, while in the horizontal plot the density curve is messed up.

Any help is very much appreciated!

Thanks,

Niels

Code:

require(MASS)
Sigma <- matrix(c(2.25, 0.8, 0.8, 1), 2, 2)
mvnorm <- mvrnorm(1000, c(0,0), Sigma)

scatterHist.Norm <- function(x,y) {
 zones <- matrix(c(2,0,1,3), ncol=2, byrow=TRUE)
 layout(zones, widths=c(2/3,1/3), heights=c(1/3,2/3))
 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$counts, yhist$counts))
 par(mar=c(0,3,1,1))
 plot(xhist, axes=FALSE, ylim=c(0,top), main="", col="grey")
 x.xfit <- seq(min(x),max(x),length.out=40)
 x.yfit <- dnorm(x.xfit,mean=mean(x),sd=sd(x))
 x.yfit <- x.yfit*diff(xhist$mids[1:2])*length(x)
 lines(x.xfit, x.yfit, col="red")
 par(mar=c(0,3,1,1))
 plot(yhist, axes=FALSE, ylim=c(0,top), main="", col="grey", horiz=TRUE)
 y.xfit <- seq(min(x),max(x),length.out=40)
 y.yfit <- dnorm(y.xfit,mean=mean(x),sd=sd(x))
 y.yfit <- y.yfit*diff(yhist$mids[1:2])*length(x)
 lines(y.xfit, y.yfit, col="red")
}
scatterHist.Norm(mvnorm[,1], mvnorm[,2])


scatterBar.Norm <- function(x,y) {
 zones <- matrix(c(2,0,1,3), ncol=2, byrow=TRUE)
 layout(zones, widths=c(2/3,1/3), heights=c(1/3,2/3))
 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$counts, yhist$counts))
 par(mar=c(0,3,1,1))
 barplot(xhist$counts, 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.yfit <- x.yfit*diff(xhist$mids[1:2])*length(x)
 lines(x.xfit, x.yfit, col="red")
 par(mar=c(3,0,1,1))
 barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE)
 y.xfit <- seq(min(x),max(x),length.out=40)
 y.yfit <- dnorm(y.xfit,mean=mean(x),sd=sd(x))
 y.yfit <- y.yfit*diff(yhist$mids[1:2])*length(x)
 lines(y.xfit, y.yfit, col="red")
}
scatterBar.Norm(mvnorm[,1], mvnorm[,2])

#

Source of scatter plot with marginal histograms (click first link after “adapted from…”):

http://r.789695.n4.nabble.com/newbie-scatterplot-with-marginal-histograms-done-and-axes-labels-td872589.html

Source of density in a scatter plot:

http://www.statmethods.net/graphs/density.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:07:19+00:00Added an answer on June 6, 2026 at 6:07 am
    scatterBarNorm <- function(x, dcol="blue", lhist=20, num.dnorm=5*lhist, ...){
        ## check input
        stopifnot(ncol(x)==2)
        ## set up layout and graphical parameters
        layMat <- matrix(c(2,0,1,3), ncol=2, byrow=TRUE)
        layout(layMat, widths=c(5/7, 2/7), heights=c(2/7, 5/7))
        ospc <- 0.5 # outer space
        pext <- 4 # par extension down and to the left
        bspc <- 1 # space between scatter plot and bar plots
        par. <- par(mar=c(pext, pext, bspc, bspc),
                    oma=rep(ospc, 4)) # plot parameters
        ## scatter plot
        plot(x, xlim=range(x[,1]), ylim=range(x[,2]), ...)
        ## 3) determine barplot and height parameter
        ## histogram (for barplot-ting the density)
        xhist <- hist(x[,1], plot=FALSE, breaks=seq(from=min(x[,1]), to=max(x[,1]),
                                         length.out=lhist))
        yhist <- hist(x[,2], plot=FALSE, breaks=seq(from=min(x[,2]), to=max(x[,2]),
                                         length.out=lhist)) # note: this uses probability=TRUE
        ## determine the plot range and all the things needed for the barplots and lines
        xx <- seq(min(x[,1]), max(x[,1]), length.out=num.dnorm) # evaluation points for the overlaid density
        xy <- dnorm(xx, mean=mean(x[,1]), sd=sd(x[,1])) # density points
        yx <- seq(min(x[,2]), max(x[,2]), length.out=num.dnorm)
        yy <- dnorm(yx, mean=mean(x[,2]), sd=sd(x[,2]))
        ## barplot and line for x (top)
        par(mar=c(0, pext, 0, 0))
        barplot(xhist$density, axes=FALSE, ylim=c(0, max(xhist$density, xy)),
                space=0) # barplot
        lines(seq(from=0, to=lhist-1, length.out=num.dnorm), xy, col=dcol) # line
        ## barplot and line for y (right)
        par(mar=c(pext, 0, 0, 0))
        barplot(yhist$density, axes=FALSE, xlim=c(0, max(yhist$density, yy)),
                space=0, horiz=TRUE) # barplot
        lines(yy, seq(from=0, to=lhist-1, length.out=num.dnorm), col=dcol) # line
        ## restore parameters
        par(par.)
    }
    
    require(mvtnorm)
    X <- rmvnorm(1000, c(0,0), matrix(c(1, 0.8, 0.8, 1), 2, 2))
    scatterBarNorm(X, xlab=expression(italic(X[1])), ylab=expression(italic(X[2])))
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to rotate an image in Love2D. I have found a documentation
I'm on cocos2d and I have a sprite that I would like to rotate
I have a TShape on my form. and would like to rotate it. Can
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to build turret, that can rotate in all axis. So the
Is it possible to rotate a view using a UIPanGestureRecognizer? I would like similar
I would like to rotate the content of a Form (WinForms). Is it possible
I would like to rotate the orientation of an array so that rows and
I have any image which I would like to rotate and move from right
I have 4 contact forms we would like to rotate for testing. I don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.