将共享图例与绘图网格的中心对齐(使用 cowplot)
Align shared legends to the center of the plot grid (with 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
问题是如何调整代码以绘制与底部中心对齐的图例。
您可以这样设置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"))
可以在本教程中为包 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
问题是如何调整代码以绘制与底部中心对齐的图例。
您可以这样设置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"))