R多列水平条形图

时间:2018-07-17 15:17:57

标签: r plot bar-chart multiple-columns

我正在寻求有关水平栏和两列的帮助。

例如,此代码:

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)

产生此条形图: enter image description here

我正在寻找使我像这样的代码(请注意固定比例): enter image description here

动机:我想要一个带有水平文本的条形图(更易于阅读),但是观察更多的结果会得到很高的图像。将图分成两列,可以在宽图像上显示水平文本。

任何提示表示赞赏!

1 个答案:

答案 0 :(得分:0)

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