添加普通图例 [英] Add a common legend

查看:346
本文介绍了添加普通图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ggplot2做多图. 这是我的初始代码

I was trying to do a multiplot with ggplot2. This was my initial code

nucmer_s1 <- ggarrange(eight_uniform, ten_uniform, twelve_uniform, fourteen_uniform, sixteen_uniform, 
                       ncol=3, nrow=2, common.legend = TRUE, legend="bottom") 

遇到此错误

plot $ scales中的错误:$运算符对于原子向量无效

Error in plot$scales : $ operator is invalid for atomic vectors

然后.

annotate_figure(nucmer_s1, 
                top = text_grob("Genomas validados con distribución de datos equilibrada",
                color = "black", face = "bold", size = 12))

但是我获得图形 但是我需要在每个情节中都添加一个标题,因此我将其更改为

however I obtain the graphic But I need to put a title in the each plot a title so I changed to this one

nucmer_s1 <-grid.arrange(
  eight_uniform + ggtitle("8 genomas"), 
  ten_uniform +  ggtitle("10 genomas"), 
  twelve_uniform + ggtitle("12 genomas"), 
  fourteen_uniform + ggtitle("14 genomas"), 
  sixteen_uniform + ggtitle("16 genomas"), 
  ncol=3, nrow=2, common.legend = TRUE, legend="bottom")

但我知道了

gList(list(grobs = list(list(x = 0.5,y = 0.5,width = 1,height = 1,:
"gList"中仅允许使用"grobs"
Además:警告消息:
1:在grob $ wrapvp<-vp中:Realizando强制执行LHD并没有列表
2:在grob $ wrapvp<-vp中:对左手执政的强制执行无必要

Error in gList(list(grobs = list(list(x = 0.5, y = 0.5, width = 1, height = 1, :
only 'grobs' allowed in "gList"
Además: Warning messages:
1: In grob$wrapvp <- vp : Realizando coercion de LHD a una lista
2: In grob$wrapvp <- vp : Realizando coercion de LHD a una lista

所以我删除了common.legend部分 并得到了这个情节

so I erase the common.legend part and got this plot

所以我有两个问题:

So I have two questions:

  • 是否可以在不使用facet_grid的情况下使用灰色框在每个图中添加标题(因为我的数据中没有该信息)?和

  • Is there a way to put a title in each plot with the grey box without using facet_grid (cause I don't have that info in the data)? and

有什么方法可以将图例放在多图的空白处?

Is there any way to put the legend in the blank side of a multi-plot?

非常感谢您的帮助

推荐答案

cowplot 软件包具有非常好的内置函数,可以处理地块之间的共享图例

lemon or cowplot packages have really nice built-in functions to deal with shared legend between plots

library(ggplot2)
library(grid)
library(gtable)
library(lemon)

dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
d <- ggplot(dsamp, aes(carat, price)) +
  geom_point(aes(colour = clarity)) +
  theme(legend.position = c(0.06, 0.75))

d3 <- d + 
  facet_wrap(~cut, ncol=3) + 
  scale_color_discrete(guide=guide_legend(ncol=3))

# Use gtable_show_names to display the names of the facetted panels
gtable_show_names(d3)

# So to place the legend in a specific panel, give its name:
reposition_legend(d3, 'center', panel='panel-3-2')

library(cowplot)

# 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
           )

# extract the legend from one of the plots
# (clearly the whole thing only makes sense if all plots
# have the same legend, so we can arbitrarily pick one.)
legend <- get_legend(p1)

# add the legend to the row we made earlier. Give it one-third of the width
# of one plot (via rel_widths).
p <- plot_grid( prow, legend, rel_widths = c(3, .3))
p

reprex程序包(v0.2.0)创建于2018-04-14.

Created on 2018-04-14 by the reprex package (v0.2.0).

这篇关于添加普通图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆