2017-03-17 234 views
2

以下是一些示例代碼,它提供了具有2列的圖例。我想減少圖例的兩個圓柱之間的空間(見下文)。ggplot2中的圖例列之間的空間縮小

library(ggplot2) 

labels <- c(expression(""^13*CH[4]), 
      expression(""^13*CH[4]~"+"~SO[4]^{2-''}), 
      expression(""^13*CH[4]~"+"~MoO[4]^{2-''})) 

ggplot(aes(mpg, wt, colour = factor(cyl), shape=factor(cyl)), 
     data = mtcars) + 
     geom_point() + 
     scale_colour_manual(values=c("red", "green", "blue"), label=labels)+ 
     scale_shape_manual(values = c(4,5,6), label=labels)+ 
     theme(legend.position = "bottom", 
      legend.text.align = 0, 
      legend.text = element_text(size=8), 
      legend.key.size = unit(0.8, 'lines')) + 
     guides(col = guide_legend("", ncol=2), shape=guide_legend("", col=2)) 

這裏是我的現實生活中的問題:需要對劇情的右側 enter image description here

額外的空間,因爲這三個因子水平有包含更多的字符。但是,我真的被限制在劇情的大小。因此,我想減少圖例的兩行之間的空間。 我也想保持左手邊最底層的因素水平,不需要增加額外的線。

+0

請提供可重複的例子,我們可以得到您的問題。您可以提供一個虛擬數據框,其中包含一個簡單的圖形,用於創建對您而言存在問題的圖例,以便其他人可以幫助您提供解決方案,而不必擔心重現問題。請參閱http://stackoverflow.com/q/5963269/446149瞭解如何提供一個很好的重現性示例 – zeehio

+1

已經有可重現的玩具代碼和虛擬數據集。它編碼一個有兩列的圖例。我不認爲發佈所有標籤表達式會有所幫助,而是會誇大問題。 – nouse

+1

我添加了一些表達式來澄清。 – nouse

回答

3

根據你的榜樣,我簡化了一點:

創建有問題的情節:

library(ggplot2) 

labels <- c("short1", "loooooooooooooooooong", "short2") 

plt <- ggplot(aes(mpg, wt, colour = factor(cyl), shape=factor(cyl)), 
     data = mtcars) + 
    geom_point() + 
    scale_colour_manual(values=c("red", "green", "blue"), label=labels)+ 
    scale_shape_manual(values = c(4,5,6), label=labels)+ 
    theme(legend.position = "bottom", 
     legend.text.align = 0, 
     legend.text = element_text(size=8), 
     legend.key.size = unit(0.8, 'lines')) + 
    guides(col = guide_legend("", ncol=2), shape=guide_legend("", col=2)) 
plot(plt) 

plot with legend separated

提取的傳說,並調整它

我用this answer來從情節中提取圖例:

#Extract Legend 
g_legend<-function(a.gplot){ 
    tmp <- ggplot_gtable(ggplot_build(a.gplot)) 
    leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") 
    legend <- tmp$grobs[[leg]] 
    return(legend)} 

legend <- g_legend(plt) 

和打印:

grid.newpage() 
grid.draw(legend) 

legend of the plot

然後我探索傳說中的grobs,我發現了寬領域:

legend$grobs[[1]]$widths 
[1] 0.2cm    0cm    0.1524cm   0.4064cm   0.0762cm   3.22791666666667cm 0.0762cm   0.4064cm   0.0762cm   
[10] 0.79375cm   0.2cm    
> 

顯然那些3.227釐米是太多所以我只是改變它們:

legend$grobs[[1]]$widths[6] <- unit(1.5, "cm") 

和情節是:

grid.newpage() 
grid.draw(legend) 

legend but more compact

應用修復全球情節:

最後的步驟是複製,關於ggplot:

應用相同的手動校正到全局圖:

# this is how the legend was extracted: 
plt_gtable <- ggplot_gtable(ggplot_build(plt)) 
leg <- which(sapply(plt_gtable$grobs, function(x) x$name) == "guide-box") 

# Replace the legend with our modified legend: 
plt_gtable$grobs[[leg]] <- legend 

而重製:

grid.newpage() 
grid.draw(plt_gtable) 

done

+0

謝謝你的努力! – nouse

+0

作爲次要旁註,內部圖「Id」需要被替換,例如,名稱(傳奇$ grobs) – nouse

+0

我用'$ grobs [[1]]'替換它,這是普遍的。如果情節有多個傳說,你可以使用'$ grobs [[2]]' – zeehio

相關問題