【问题标题】:Add a common legend添加一个共同的图例
【发布时间】:2018-09-24 08:14:20
【问题描述】:

我试图用ggplot2 做一个多图。 这是我的初始代码

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

收到此错误

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

那么。

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

但是我得到了图形 但是我需要在每个情节中添加一个标题,所以我改为这个

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 2:在 grob$wrapvp

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

所以我有两个问题:

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

  • 有没有办法将图例放在多图的空白侧?

非常感谢您的帮助

【问题讨论】:

  • 将“该信息”添加到您的数据中并使用facet_wrapfacet_grid 似乎是显而易见的解决方案。其他任何事情都会做得更多。
  • 但是我有 read.csv 的初始代码,所以如果我添加文本我会感到困惑,因为我只有数字。即使我不能用灰色框做到这一点,有什么方法可以在每个情节中获得标题和共同的传说?
  • AFAIK,要将图例添加到空白的多图方块中,您将创建所有这些没有图例的图,然后使用图例创建虚拟图,从图中提取图例并将图例推送到视口到它自己的情节,并将其放在最后一个位置。 This answer does something a little similar。相信我,还有很多工作要做。在您的数据中添加一些文本非常容易。如果您不知道如何操作,请询问如何操作(并展示足够多的示例以便我们可以帮助您)。
  • 你能用dput(eight_uniform)分享你的数据吗?

标签: r ggplot2 gridextra cowplot r-lemon


【解决方案1】:

lemoncowplot 包具有非常好的内置函数来处理绘图之间的共享图例

柠檬包示例

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

cowplot 包中的示例

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 package (v0.2.0) 于 2018 年 4 月 14 日创建。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 2017-05-18
    • 2021-08-17
    • 2013-07-16
    • 2016-05-21
    相关资源
    最近更新 更多