如何调整绘图图例中两列之间的间距?

时间:2018-09-26 06:37:13

标签: r legend

我想将饼图的图例作为单个图(每页6个图形-5个图,1个图例)。现在我很难调整两列之间的间隔。

我使用了以下代码(通过尝试和错误):

 #Colors
 colors=c("blue","green","yellow","orange","red","purple","pink","grey")

 legtext <- c("G","G*E","E","Source","Source*E",
 "Table*E","Table*Block*E","Residual Error") 

 #Code for chart
 pie3D(#data for pie
 rooting1,
 #specify labels vector
 #labels=labels,
 #specify labels size
 labelcex=0.9,
 #how much different pies go from each other
 explode=0.1,
 #height of chart
 height=0.1,
 #Main title
 theta=pi/3,
 #Colors
 col=colors
 )

 #Code for legend
 xcoords <- c(0.9,1,1.1,1.2)
 secondvector <- (1:length(legtext))-1
 textwidths <- xcoords/secondvector # this works for all but the first element
 textwidths[1] <- 0

legend(-1, 0.9,ncol=2,
   c("G","G*E","E","Source","Source*E","Table*E","Table*Block*E","Residual Error"), 
   cex = 0.8, 
   fill = colors,
   text.width=textwidths)

我得到的图是:我想删除垂直线,如果可能,请删除图表的其余部分,因为我只想显示图例。

我得到的传说:

enter image description here

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

bty="n"添加到您的图例:

legend(-1, 0.9,ncol=2,
   c("G","G*E","E","Source","Source*E","Table*E","Table*Block*E","Residual Error"), 
   cex = 0.8, 
   fill = colors,
   text.width=textwidths,
   bty="n")

对于另一个问题-如何摆脱图表本身,这将需要一些摆弄。基本上,您可以做一个空图表,但要调整xlim和ylim以及边距,以便为图例留出足够的空间:

par(mar=c(0.1,0.1,0.1,0.1))  # you don't need large margins
# but maybe you need more than 0.1
plot(NA, xlim=c(-1,1), ylim=c(-1,1), axes=FALSE, xlab="", ylab="")
# this makes an empty plot
# you may need to change xlim and ylim (or the x and y of your legend)
# ... so that the legend would start from the left/upper corner