simon.t
simon.t

Reputation: 21

R multiple-column horizontal barplot

I am looking for help with a horizontal barplot and two columns.

For example, this code:

set.seed(112)
data <- t(matrix(sample(1:30,8) , nrow=))
colnames(data) <- c("H","G","F","E","D", "C", "B", "A")

barplot(data, border="white", space=0.04, font.axis=2,
        main = "Barplot",
        horiz = TRUE, las = 1)

Produces this barplot: enter image description here

I am looking for code that would get me something like this (note the fixed scale): enter image description here

Motivation: I want a bar plot with horizontal text (which is easier to read), but with more observations, this results in a very high image. Splitting the plot to two columns would allow horizontal text on a wide image.

Any tips appreciated!

Upvotes: 1

Views: 424

Answers (1)

Lennyy
Lennyy

Reputation: 6132

set.seed(112)
data <- t(matrix(sample(1:30,8) , nrow=))
colnames(data) <- c("H","G","F","E","D", "C", "B", "A")

par(mfrow = c(1,2)) #to plot on 1 row in 2 columns
barplot(data[,5:8, drop = F], border="white", space=0.04, font.axis=2, #subset data, and drop  = F to prevent your matrix from losing a dimension, i.e. getting converted to a vector
        main = "Barplot",
        horiz = TRUE, las = 1, xlim = c(0,25)) #set the xlimits

barplot(data[,1:4, drop = F], border="white", space=0.04, font.axis=2,
        main = "Barplot",
        horiz = TRUE, las = 1, xlim = c(0,25)) # set the same xlimits

enter image description here

Upvotes: 2

Related Questions