Soheil
Soheil

Reputation: 333

Rotating histogram horizontally in r

Can anyone help me how to rotate histogram just by 90 degrees in r? I know there is an option (horiz=T) in boxplot but I have no idea if there is a similar one for the histogram.

Upvotes: 8

Views: 8561

Answers (2)

s__
s__

Reputation: 9495

If you use ggplot2, you can use coord_flip():

    # here with @PKumar data
    x <- pmin(3, pmax(-3, stats::rnorm(50)))
    library(ggplot2)
    qplot(x, geom="histogram",binwidth = 0.3) + coord_flip()

enter image description here

Upvotes: 1

PKumar
PKumar

Reputation: 11128

I think you have to use hist with barplot to do it like below (its straight from documentation), you can check it here ?layout.

x <- pmin(3, pmax(-3, stats::rnorm(50)))
xhist <- hist(x, breaks = seq(-3,3,0.5), plot = FALSE)
barplot(xhist$counts, axes = TRUE, space = 0, horiz=TRUE, xlab= "Counts", ylab="Bins")

Upvotes: 5

Related Questions