【问题标题】:Grouped barplot with cut y axis带切割 y 轴的分组条形图
【发布时间】:2014-06-13 09:39:57
【问题描述】:

我正在尝试使用分组条形图和切割 y 轴制作图。但是,我似乎无法同时获得两者。使用这些数据:

d = t(matrix( c(7,3,2,3,2,2,852,268,128,150,
              127,74,5140,1681,860,963,866,
              470,26419,8795,4521,5375,4514,2487),
            nrow=6, ncol=4 ))
colnames(d)=c("A", "B", "C", "D", "E", "F")

我可以得到如下分组的条形图:

barplot( d, beside = TRUE)

然后我可以使用以下方法获得切割的 y 轴:

# install.packages('plotrix', dependencies = TRUE)
require(plotrix)
gap.barplot( as.matrix(d), 
             beside = TRUE, 
             gap=c(9600,23400), 
             ytics=c(0,3000,6000,9000,24000,25200,26400) )

但是,我放弃了分组和 A、B、C... 标签。我怎样才能得到两者?

【问题讨论】:

    标签: r plot


    【解决方案1】:

    您可以手动执行此操作。与barplot 一样,?gap.barplot 返回条的中心位置。使用这些来添加标签。

    像常规barplot 那样使用space 来设置组之间的间距似乎不起作用。我们可以使用一行 NA 来破解一个空间。

    d = t(matrix( c(7,3,2,3,2,2,852,268,128,150,
                                    127,74,5140,1681,860,963,866,
                                    470,26419,8795,4521,5375,4514,2487),
                                nrow=6, ncol=4 ))
    colnames(d)=c("A", "B", "C", "D", "E", "F")
    
    # add row of NAs for spacing
    d=rbind(NA,d)
    
    # install.packages('plotrix', dependencies = TRUE)
    require(plotrix)
    
    # create barplot and store returned value in 'a'
    a = gap.barplot(as.matrix(d), 
                    gap=c(9600,23400), 
                    ytics=c(0,3000,6000,9000,24000,25200,26400),
                    xaxt='n') # disable the default x-axis
    
    # calculate mean x-position for each group, omitting the first row 
    # first row (NAs) is only there for spacing between groups
    aa = matrix(a, nrow=nrow(d))
    xticks = colMeans(aa[2:nrow(d),])
    
    # add axis labels at mean position
    axis(1, at=xticks, lab=LETTERS[1:6])
    

    【讨论】:

    • 非常好,我们也可以破解组之间的空间吗?
    • 是的,原来 NA 只是一个空格
    【解决方案2】:

    在 koekenbakker 的回答的帮助下,我终于想到了这个:

    # install.packages('plotrix', dependencies = TRUE)
    require(plotrix)
    
    d = t(matrix( c(7,3,2,3,2,2,852,268,128,150,
                    127,74,5140,1681,860,963,866,
                    470,26419,8795,4521,5375,4514,2487),
                  nrow=6, ncol=4 ))
    
    # Hack for grouping (leaves the extra space at the end)
    e = as.vector(rbind(d, rep(NA, 6)))[1:29]
    
    a = gap.barplot(ceiling(as.matrix(e/60)), 
                    gap=c(160,390),
                    col=rep(c(grey.colors(4), 1), 6),
                    #space=rep(c(rep(0,3), 1), 6),
                    ytics=c(0,50,100,150,400,420,440),
                    xaxt='n') # disable the default x-axis
    
    xticks=c(2.5, 7.5, 12.5, 17.5, 22.5, 27.5)
    
    # add axis labels at mean position
    axis(1, at=xticks, LETTERS[1:6] )
    
    legend("topright", LETTERS[7:10],
           bty="n",  
           fill=grey.colors(4)) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-01
      • 2014-06-06
      • 2022-06-22
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 2020-04-20
      相关资源
      最近更新 更多