如何减少 x 轴上两个离散值之间的空间?

Cht*_*ect 2 r

首先,我想在没有 ggplot 的情况下做到这一点。

我有一个用于箱线图的脚本:

data <- data.frame(
  u    = c(0.522, 0.488, 0.474, 0.443, 0.510, 0.443, 0.420, 0.554, 0.333, 0.414,
           0.467, 0.606, 0.588),
  ub   = c(0.594, 0.568, 0.578, 0.523, 0.577, 0.501, 0.522, 0.623, 0.498, 0.511,
           0.544, 0.654, 0.639),
  nu   = c(0.646, 0.620, 0.644, 0.607, 0.667, 0.614, 0.631, 0.701, 0.586, 0.589,
           0.664, 0.739, 0.744),
  nub  = c(0.733, 0.701, 0.700, 0.698, 0.724, 0.701, 0.722, 0.777, 0.630, 0.663,
           0.703, 0.78, 0.787),
  wnub = c(0.815, 0.782, 0.757, 0.764, 0.792, 0.771, 0.838, 0.860, 0.700, 0.703,
           0.774, 0.837, 0.863))
boxplot(data, las=1, par(mar=c(7.5, 4.2, 0.25, 0.15)), axes=FALSE, xlab=NA,
        ylab=NA, boxwex=.4, outline=FALSE, ylim=c(0:1))
box()
labnames = c("Unigrams",
             "Unigrams\n& Bigrams",
             "Noun Unigrams",
             "Noun Unigrams\n& Noun Bigrams",
             "Noun Unigrams\n& Noun Bigrams\n(positional weights)")
axis(side=1, tck=-0.025, labels=FALSE)
axis(side=1, lwd=0, labels=FALSE, at=c(1:5), cex.axis=1)
text(seq(1, 5, by=1), par("usr")[3]-.2, labels=labnames, srt=-90,
     xpd=TRUE, adj=.25)
axis(side=2, tck=-0.02, labels=NA)
axis(side=2, lwd=0, cex.axis=1, las=1)
mtext(side=1, "Feature Sets", line=6.5, cex=1.5)
mtext(side=2, "F-Score", line=3, cex=1.5)
Run Code Online (Sandbox Code Playgroud)

产生下图:

箱形图

我对浪费的空白数量感到恼火。我一直试图减少空间的一种方法是使用“asp”将离散的 x 轴值靠得更近,但它不会改变任何东西。我怎样才能做到这一点,以便减少盒子之间的巨大差距?

另外,如果有任何其他方法可以减少此图片中的空白,请分享您的想法。

Jul*_*ano 7

Option 1: If you're saving images as PDF for Latex, you can set the width and height with pdf(file=file, width=width, height=height). Then you can ignore the boxwex parameter altogether.

Option 2: Alternatively, you can use the at parameter to tell boxplot where in the X axis it should place the boxes:

at.x <- seq(1,by=.5,length.out=5) # set here the X-axis positions
boxplot(data, par(mar=c(8.5, 4.2, 0.25, 0.15)), outline=FALSE,
        cex.lab=1.5, ylab="F-score", xlab=NA, las=3, names=labnames,
        boxwex=.4, ylim=c(0:1),
        at=at.x)
# display the X-axis name
mtext(side=1, "Feature Sets", line=7.2, cex=1.5)
Run Code Online (Sandbox Code Playgroud)

See that the code is also heavily simplified. You can further play with the xlim parameter to boxplot to reduce the inner margins of the plot. Try for instance xlim=c(0.8,3.2). Result:

在此处输入图片说明

选项 3:如果您确实想节省空间,则可以使用颜色和图例:

cols <- c("white","red","yellow","green","blue")
boxplot(data, par(mar=c(0.2, 3.2, 0.25, 0.15)), outline=FALSE,
        axes=F, ylab=NA, xlab=NA, names=NA, col=cols)
box()
axis(2)
mtext(side=2,"F-score",line=2.2)
legend("bottomright", cex=.8, fill=cols,
       c("Unigrams", "Unigrams & Bigrams", "Noun Unigrams",
         "Noun Unigrams & Noun Bigrams",
         "Noun Unigrams & Noun Bigrams (positional weights)"))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

最后,为什么将 Y 轴的限制设置为 0 和 1?那在那里浪费了很多空间。另外,为什么要删除异常值?只是作为一个 IR 研究员问自己 :-)