2017-10-09 43 views
1

可再现例子的中心可以在该教程包cowplot找到。对齐共享传说的情节网格(与cowplot)

https://cran.r-project.org/web/packages/cowplot/vignettes/shared_legends.html

复制示例代码:

library(ggplot2) 
library(cowplot) 
#down-sampled diamonds data set 
dsamp <- diamonds[sample(nrow(diamonds), 1000), ] 

# Make three plots. 
# We set left and right margins to 0 to remove unnecessary spacing in the 
# final plot arrangement. 
p1 <- qplot(carat, price, data=dsamp, colour=clarity) + 
    theme(plot.margin = unit(c(6,0,6,0), "pt")) 
p2 <- qplot(depth, price, data=dsamp, colour=clarity) + 
    theme(plot.margin = unit(c(6,0,6,0), "pt")) + ylab("") 
p3 <- qplot(color, price, data=dsamp, colour=clarity) + 
    theme(plot.margin = unit(c(6,0,6,0), "pt")) + ylab("") 

# arrange the three plots in a single row 
prow <- plot_grid(p1 + theme(legend.position="none"), 
      p2 + theme(legend.position="none"), 
      p3 + theme(legend.position="none"), 
      align = 'vh', 
      labels = c("A", "B", "C"), 
      hjust = -1, 
      nrow = 1 
      ) 
legend_b <- get_legend(p1 + theme(legend.position="bottom")) 

# add the legend underneath the row we made earlier. Give it 10% of the height 
# of one plot (via rel_heights). 
p <- plot_grid(prow, legend_b, ncol = 1, rel_heights = c(1, .2)) 
p 

该实施例示出了其中的图例被吸入与网格对齐的左下方的曲线图。 但是,它使用的是不同的,因为传说中,然后拉伸对准情节的底部中心。以下是几个月前由我的个人代码生成的示例。 https://s1.postimg.org/8pf2en1zen/Untitled.png(上传工具目前没有工作对我来说)

在任一套餐变化的未知量之后重新运行我的旧代码提供对齐左下角的传奇人物(如教程还显示,从上述第三图): https://s1.postimg.org/3agjw7n9gf/Untitled2.png

问题是如何调整代码以绘制图例对齐底部中心。

回答

1

你可以设置legend_b这样:

legend_b <- get_legend(p1 + theme(legend.position=c(0.3,0.8),legend.direction = "horizontal")) 

一个更好的办法:

legend_b <- get_legend(p1 + theme(legend.direction = "horizontal",legend.justification="center" ,legend.box.just = "bottom")) 
+0

谢谢。我猜,这确实需要对其他图例大小进行“试验和错误”。 – nouse

+0

'legend_b < - get_legend(p1 + theme(legend.position =“none”,legend.direction =“horizo​​ntal”))' 这对我也适用。在get_legend –

+0

错误(P1 +主题(legend.position = “无”,legend.direction = “水平”)): 剧情必须包含一个图例 – nouse