首页 文章

R多列水平条形图

提问于
浏览
0

我正在寻找水平条形图和两列的帮助 .

例如,这段代码:

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
    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

相关问题