position legend of a stacked bar plot

后端 未结 4 905
余生分开走
余生分开走 2020-12-29 04:57

I am trying to create a stacked bar plot, but I cannot position the legend so that it does not overlap any of the bars. I have tried adjusting the margins, setting xl

4条回答
  •  灰色年华
    2020-12-29 05:40

    How about this:

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

提交回复
热议问题