【问题标题】:determine the position of a legend in R, so legend does not overlap the piecharts确定图例在 R 中的位置,因此图例不会与饼图重叠
【发布时间】:2017-04-13 14:00:40
【问题描述】:

我需要这两个饼图和中间的图例,我如何确定一个图例的位置正好在中间,所以它不会与饼图重叠。

par(mfrow = c(1,2)) 
pie(frman, main="Men", col=cols, labels=pielabelsman, cex=0.85)
pie(frwoman, main="Women", col=cols,labels=pielabelswoman,cex=0.85)
legend("center", uniq, cex=0.5,fill=cols)

【问题讨论】:

    标签: r plot legend


    【解决方案1】:

    默认命令可以将图例放置在不同的地方:

    legend("center", uniq, cex=0.5,fill=cols)    
    legend("bottomright", uniq, cex=0.5,fill=cols)
    legend("bottom", uniq, cex=0.5,fill=cols)
    legend("bottomleft", uniq, cex=0.5,fill=cols)
    legend("left", uniq, cex=0.5,fill=cols)
    legend("topleft", uniq, cex=0.5,fill=cols)
    legend("top", uniq, cex=0.5,fill=cols)
    legend("topright", uniq, cex=0.5,fill=cols)
    legend("right", uniq, cex=0.5,fill=cols)
    legend("center", uniq, cex=0.5,fill=cols)
    

    否则您可以通过输入 x 和 y 坐标自行指定,例如:

    legend(x = 2, y = 5 , uniq, cex=0.5,fill=cols)
    

    您可以找到更多信息herehereherehere,仅举一些例子。

    更新

    你也可以试试:

    layout(matrix(c(1,2,3,3), ncol=2, byrow=TRUE), heights=c(4, 1))
    par(mai=rep(0.5, 4)) 
    pie(frman, main="Men", col=cols, labels=pielabelsman, cex=0.85)
    pie(frwoman, main="Women", col=cols,labels=pielabelswoman,cex=0.85)
    par(mai=c(0,0,0,0))
    plot.new()
    legend("center", uniq, cex=0.5,fill=cols)
    

    由于我没有您的数据,我无法检查它是否正确,但我认为应该。

    【讨论】:

    • 感谢您的回复。我已经用不同的 x 和 y 值尝试了你的最后一行,但我找不到合适的 x,y 来观察图例。
    • 问题是我使用的是 par(mfrow = c(1,2)),所以 x,y 每次都对应两个图之一
    • @GiorgosVoultsios 我更新了答案。如果可行,请不要忘记投票并接受答案!谢谢!
    猜你喜欢
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    相关资源
    最近更新 更多