我目前正在使用igraph,并用颜色标记了我的顶点。我想添加一个图例,指出每种颜色代表什么。
此时,我能想到的是使用ggplot2仅打印图例并隐藏条形图。有什么方法可以直接输出图例吗?
发布于 2017-05-31 17:25:28
Cowplot很方便地添加了一个函数来提取图例。以下内容直接取自手册。
library(ggplot2)
library(cowplot)
p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_line()
plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + geom_point(size=2.5)
# Note that these cannot be aligned vertically due to the legend in the plot.mpg
ggdraw(plot_grid(p1, plot.mpg, ncol=1, align='v'))
# now extract the legend
legend <- get_legend(plot.mpg)
# and replot suppressing the legend
plot.mpg <- plot.mpg + theme(legend.position='none')
# Now plots are aligned vertically with the legend to the right
ggdraw(plot_grid(plot_grid(p1, plot.mpg, ncol=1, align='v'),
plot_grid(NULL, legend, ncol=1),
rel_widths=c(1, 0.2)))https://stackoverflow.com/questions/12041042
复制相似问题