【问题标题】:position legend of a stacked bar plot堆积条形图的位置图例
【发布时间】:2013-09-12 09:18:48
【问题描述】:

我正在尝试创建堆叠条形图,但我无法定位图例以使其不与任何条形重叠。我尝试调整边距,设置xlim,使用单独的legend 语句并在此处搜索,均未成功。实际上,示例数据集的代码来自 StackOverflow。我正在使用基础 R 并且更喜欢基础 R 解决方案。谢谢你的建议。

setwd('c:/users/mmiller21/simple R programs/')

jpeg(filename = "barplot.jpeg")

set.seed(1234)
x <- replicate(8, round(10 * rexp(2000, 10)))
y <- apply(x, 2, function(column) table(factor(column, levels = 0:9)))
colnames(y) <- paste('A', seq(1,ncol(y),1), sep='')
rownames(y) <- paste('R', seq(1,nrow(y),1), sep='')
y

#      A1  A2  A3  A4  A5  A6  A7  A8
# R1  769 800 790 806 792 787 834 801
# R2  779 733 793 757 786 744 731 776
# R3  284 297 278 272 263 301 280 275
# R4  112 106  91 124 106 103 104  96
# R5   33  38  37  26  36  37  30  36
# R6   11  18   7  11  10  20  11   9
# R7    8   8   3   2   3   3   9   5
# R8    4   0   1   2   4   4   0   2
# R9    0   0   0   0   0   1   1   0
# R10   0   0   0   0   0   0   0   0

par(mfrow=c(1, 1), mar=c(5, 5, 4, 2) + 0.1)
barplot(y, col = 1:nrow(y), ylab="My Variables", legend.text = TRUE, 
        args.legend = list(x = "topright", bty = "n"))

dev.off()

【问题讨论】:

    标签: r plot bar-chart


    【解决方案1】:

    这个怎么样:

    library(RColorBrewer)
    
    barplot(
        y,
        xlim=c(0, ncol(y) + 3),
        col=brewer.pal(nrow(y), "Paired"),
        ylab="My Variables",
        legend.text=TRUE,
        args.legend=list(
          x=ncol(y) + 3,
          y=max(colSums(y)),
          bty = "n"
        )
    )
    

    【讨论】:

    • 我也面临同样的问题,请您帮忙。我已经问过this link
    • 我不明白你是怎么把它变成这样的。我复制并粘贴了您在此处的内容,图例与最右侧的条重叠。
    【解决方案2】:

    你应该在par()中添加xpd=TRUE

    par(mfrow=.., mar=...,xpd=TRUE)
    

    【讨论】:

      【解决方案3】:

      只是为了好玩,这里是ggplot2,没有重叠的图例。

      set.seed(1234)
      x <- replicate(8, round(10 * rexp(2000, 10)))
      y <- apply(x, 2, function(column) table(factor(column, levels = 0:9)))
      y <- as.data.frame(y)
      colnames(y) <- paste('A', seq(1,ncol(y),1), sep='')
      rownames(y) <- paste('R', seq(1,nrow(y),1), sep='')
      
      library(ggplot2)
      library(reshape)
      y$ID <- rownames(y)
      y.melt <- melt(y, id.var = 'ID')
      
      y.melt <- within(y.melt, ID <- factor(ID, 
          c('R10','R9','R8','R7','R6','R5','R4','R3','R2','R1'), 
          ordered = TRUE))
      
      ggplot(y.melt, aes(x = variable, y = value, fill = ID)) +
          geom_bar(stat = 'identity') +
          xlab("") +
          ylab("My variable") +
          theme(legend.title=element_blank())
      

      【讨论】:

        【解决方案4】:

        有边距

        par(mfrow=c(1, 1), mar=c(5, 5, 4, 8))
        barplot(y, col = 1:nrow(y), ylab="My Variables", legend.text = TRUE, 
                args.legend = list(x = "topright", bty = "n", inset=c(-0.15, 0)))
        

        【讨论】:

        • 谢谢。在我的计算机上,图例仍然与使用您的代码的条形之一重叠,但如果我将 par(mfrow=c(1, 1), mar=c(5, 5, 4, 4)) 与您的代码一起使用,则不会。
        • @MarkMiller:因为右边距很重要,当你在右边有图例时。
        猜你喜欢
        • 2017-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-26
        • 2017-10-14
        • 1970-01-01
        • 2014-02-09
        相关资源
        最近更新 更多