简体   繁体   English

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

[英]How to adjust the space between 2 columns in a plot legend?

I want to have the plot legend for a pie chart as a single plot (6 graphics at a page - 5 plots, 1 legend). 我想将饼图的图例作为单个图(每页6个图形-5个图,1个图例)。 Now I have difficulties adjusting the space between the 2 columns. 现在,我很难调整两列之间的间距。

I used the following code (by try and error): 我使用了以下代码(通过尝试和错误):

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

The plot I get is this: I want to remove the vertical lines and if possible, remove the rest of the chart as I only want to display the legend. 我得到的图是:我想删除垂直线,如果可能,请删除图表的其余部分,因为我只想显示图例。

Legend I get: 我得到的传说:

在此处输入图片说明

Can anybody help me? 有谁能够帮助我?

Add bty="n" to your legend: 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")

As for the other question - how to get rid of the chart itself, this will take some fiddling. 至于另一个问题-如何摆脱图表本身,这将需要一些摆弄。 Basically, what you can do is to make an empty chart, but adjusting the xlim and ylim, as well as margins so that there is enough room for the legend: 基本上,您可以做一个空图表,但要调整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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM