R中的条形图图例位置(避免operlap)

时间:2014-12-29 12:19:21

标签: r graph bar-chart

我有以下类型的数据:

           A         B           C      D           E      F
Series1 681968620 814707019 689302814 827844038 778849469 826532174
Series2  41507149  53403451  52857261  52319991  59246699 104253758
Series3 869316619 722165946 858134539 716641489 759754131 668183913
Series4  12642153  15158215   5140017   8629111   7170466   6464783

当我绘制条形图时,使用以下命令:

barplot(height = m, 
    beside=T, ylab = "Area (m^2)", col=colorcode,
    legend.text = c("Series1", "Series2","Series3",
                    "Series4"),
    args.legend = list(x = "topright"))

图例与图表中的条形图重叠。如何正确放置我的图例,以便我的图表看起来很好。

1 个答案:

答案 0 :(得分:4)

par(mfrow=c(1, 1), mar=c(5, 5, 4, 10))
barplot(height = m, 
beside=T, ylab = "Area (m^2)", col=1:4,
legend.text = c("Series1", "Series2","Series3",
                "Series4"),
args.legend = list(x ='topright', bty='n', inset=c(-0.25,0)))

enter image description here

如果您不想scientific notation上的y-axis,则可以在运行代码之前更改options,例如

op <- options(scipen=999)

数据

m <- structure(c(681968620L, 41507149L, 869316619L, 12642153L, 814707019L, 
53403451L, 722165946L, 15158215L, 689302814L, 52857261L, 858134539L, 
5140017L, 827844038L, 52319991L, 716641489L, 8629111L, 778849469L, 
59246699L, 759754131L, 7170466L, 826532174L, 104253758L, 668183913L, 
6464783L), .Dim = c(4L, 6L), .Dimnames = list(c("Series1", "Series2", 
"Series3", "Series4"), c("A", "B", "C", "D", "E", "F")))