ggplot-多个图例排列 [英] ggplot - Multiple legends arrangement

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

问题描述

我想在ggplot中安排多行和多行的图例.但是目前,从文档中,我只能在1个图例中确定方向或操作行/列.我忽略了什么吗?感谢您对解决方案的任何参考. 这是示例代码,我做了什么以及预期的结果.

I want to arrange mutiple legend in ggplot with multiple rows and columns. However currently, from the documentation I can only decide there direction or manipulate rows/columns within 1 legend. Am I overlook something? Thanks for any reference point to the solution. Here is the sample code and what I have done and the expect result.

data <- seq(1000, 4000, by=1000)
colorScales <- c("#c43b3b", "#80c43b", "#3bc4c4", "#7f3bc4")
names(colorScales) <- data
ggplot() +
    geom_point(aes(x=data, y=data, color=as.character(data), fill=data, size=data),
               shape=21) +
    scale_color_manual(name="Legend 1",
                      values=colorScales) +
    scale_fill_gradientn(name="Legend 2",
                          labels=comma, limits=c(0, max(data)),
                          colours=rev(c("#000000", "#FFFFFF", "#BA0000")),
                         values=c(0, 0.5, 1)) +
    scale_size_continuous(name="Legend 3") +
    theme(legend.direction = "vertical", legend.box = "vertical")

输出垂直图例:

ggplot() +
    geom_point(aes(x=data, y=data, color=as.character(data), fill=data, size=data),
               shape=21) +
    scale_color_manual(name="Legend 1",
                       values=colorScales) +
    scale_fill_gradientn(name="Legend 2",
                         labels=comma, limits=c(0, max(data)),
                         colours=rev(c("#000000", "#FFFFFF", "#BA0000")), 
                         values=c(0, 0.5, 1)) +
    scale_size_continuous(name="Legend 3") +
    theme(legend.direction = "vertical", legend.box = "horizontal")

输出水平图例

ggplot() +
    geom_point(aes(x=data, y=data, color=as.character(data), fill=data, size=data),
               shape=21) +
    scale_color_manual(name="Legend 1",
                       values=colorScales) +
    scale_fill_gradientn(name="Legend 2",
                         labels=comma, limits=c(0, max(data)),
                         colours=rev(c("#000000", "#FFFFFF", "#BA0000")), 
                         values=c(0, 0.5, 1)) +
    guides(colour = guide_legend(nrow = 2, byrow = T, override.aes=list(size=4))) +
    guides(size = guide_legend(nrow = 2, byrow = T)) +
    scale_size_continuous(name="Legend 3") +
    theme(legend.direction = "vertical", legend.box = "vertical")

具有垂直布局的输出图例,每个图例中有2列:

Output legend with vertical layout, 2 columns within each legend:

这是我想要的:

推荐答案

想法是分别创建每个图(colorfill& size),然后提取其图例并以所需方式组合它们与主要情节一起.

The idea is to create each plot individually (color, fill & size) then extract their legends and combine them in a desired way together with the main plot.

此处中了解更多有关软件包的信息& patchwork此处

See more about the cowplot package here & the patchwork package here

library(ggplot2)
library(cowplot)   # get_legend() & plot_grid() functions
library(patchwork) # blank plot: plot_spacer()

data <- seq(1000, 4000, by = 1000)
colorScales <- c("#c43b3b", "#80c43b", "#3bc4c4", "#7f3bc4")
names(colorScales) <- data

# Original plot without legend
p0 <- ggplot() +
  geom_point(aes(x = data, y = data, 
                 color = as.character(data), fill = data, size = data),
             shape = 21
  ) +
  scale_color_manual(
    name = "Legend 1",
    values = colorScales
  ) +
  scale_fill_gradientn(
    name = "Legend 2",
    limits = c(0, max(data)),
    colours = rev(c("#000000", "#FFFFFF", "#BA0000")),
    values = c(0, 0.5, 1)
  ) +
  scale_size_continuous(name = "Legend 3") +
  theme(legend.direction = "vertical", legend.box = "horizontal") +
  theme(legend.position = "none")

# color only
p1 <- ggplot() +
  geom_point(aes(x = data, y = data, color = as.character(data)),
             shape = 21
  ) +
  scale_color_manual(
    name = "Legend 1",
    values = colorScales
  ) +
  theme(legend.direction = "vertical", legend.box = "vertical")

# fill only
p2 <- ggplot() +
  geom_point(aes(x = data, y = data, fill = data),
             shape = 21
  ) +
  scale_fill_gradientn(
    name = "Legend 2",
    limits = c(0, max(data)),
    colours = rev(c("#000000", "#FFFFFF", "#BA0000")),
    values = c(0, 0.5, 1)
  ) +
  theme(legend.direction = "vertical", legend.box = "vertical")

# size only
p3 <- ggplot() +
  geom_point(aes(x = data, y = data, size = data),
             shape = 21
  ) +
  scale_size_continuous(name = "Legend 3") +
  theme(legend.direction = "vertical", legend.box = "vertical")

获取所有图例

leg1 <- get_legend(p1)
leg2 <- get_legend(p2)
leg3 <- get_legend(p3)

# create a blank plot for legend alignment 
blank_p <- plot_spacer() + theme_void()

合并图例

# combine legend 1 & 2
leg12 <- plot_grid(leg1, leg2,
                   blank_p,
                   nrow = 3
)

# combine legend 3 & blank plot
leg30 <- plot_grid(leg3, blank_p,
                   blank_p, 
                   nrow = 3
)

# combine all legends
leg123 <- plot_grid(leg12, leg30,
                    ncol = 2
)

将所有内容放在一起

final_p <- plot_grid(p0,
                     leg123,
                     nrow = 1,
                     align = "h",
                     axis = "t",
                     rel_widths = c(1, 0.3)
)

print(final_p)

reprex软件包(v0.2.0.9000)创建.

Created on 2018-08-28 by the reprex package (v0.2.0.9000).

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

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