简体   繁体   English

如何在 R 中使用 ggplot 在绘图区域的 plot“外部”?

[英]How to plot 'outside' of plotting area using ggplot in R?

I recently asked this question.我最近问了这个问题。 However, I am asking a separate question now as the scope of my new question falls outside the range of the last question.但是,我现在要问一个单独的问题,因为我的新问题的 scope 超出了最后一个问题的范围。

I am trying to create a heatmap in ggplot... however, outside of the axis I am trying to plot geom_tile .我正在尝试在 ggplot 中创建热图……但是,在轴之外,我正在尝试 plot geom_tile The issue is I cannot find a consistent way to get it to work.问题是我找不到一致的方法来让它工作。 For example, the code I am using to plot is:例如,我用于 plot 的代码是:

library(colorspace)
library(ggplot2)
library(ggnewscale)
library(tidyverse)

asd <- expand_grid(paste0("a", 1:9), paste0("b", 1:9))

df <- data.frame(
  a = asd$`paste0("a", 1:9)`,
  b = asd$`paste0("b", 1:9)`,
  c = sample(20, 81, replace = T)
)


# From discrete to continuous
df$a <- match(df$a, sort(unique(df$a)))
df$b <- match(df$b, sort(unique(df$b)))
z <- sample(10, 18, T)


# set color palettes
pal <- rev(diverging_hcl(palette = "Blue-Red", n = 11))
palEdge <- rev(sequential_hcl(palette = "Plasma", n = 11))

# plot
ggplot(df, aes(a, b)) +
  geom_tile(aes(fill = c)) +
  scale_fill_gradientn(
    colors = pal,
    guide = guide_colorbar(
      frame.colour = "black",
      ticks.colour = "black"
    ),
    name = "C"
  ) +
  theme_classic() +
  labs(x = "A axis", y = "B axis") +
  new_scale_fill() +
  geom_tile(data = tibble(a = 1:9, 
                          z = z[1:9]),
            aes(x = a, y = 0, fill = z, height = 0.3)) +
  geom_tile(data = tibble(b = 1:9, 
                          z = z[10:18]),
            aes(x = 0, y = b, fill = z, width = 0.3))  +
  scale_fill_gradientn(
    colors = palEdge,
    guide = guide_colorbar(
      frame.colour = "black",
      ticks.colour = "black"
    ),
    name = "Z"
  )+
  coord_cartesian(clip = "off", xlim = c(0.5, NA), ylim = c(0.5, NA)) +
  theme(aspect.ratio = 1,
        plot.margin = margin(10, 15.5, 25, 25, "pt")
  )

This produces something like this:这会产生如下内容: 热图

However, I am trying to find a consistent way to plot something more like this (which I quickly made in photoshop):但是,我试图找到一种一致的方法来 plot 更像这样(我很快在 Photoshop 中制作): 外面有瓷砖的热图

The main issue im having is being able to manipulate the coordinates of the new scale 'outside' of the plotting area.我遇到的主要问题是能够操纵绘图区域“外部”的新比例的坐标。 Is there a way to move the tiles that are outside so I can position them in an area that makes sense?有没有办法移动外面的瓷砖,这样我就可以在一个有意义的区域 position ?

There are always the two classic options when plotting outside the plot area:在 plot 区域之外绘图时,总是有两个经典选项:

  • annotate/ plot with coord_...(clip = "off")用坐标注释/ plot coord_...(clip = "off")
  • make different plots and combine them.制作不同的情节并将它们组合起来。

The latter option usually gives much more flexibility and way less headaches, in my humble opinion.以我的拙见,后一种选择通常会提供更大的灵活性和更少的麻烦。

library(colorspace)
library(tidyverse)
library(patchwork)

asd <- expand_grid(paste0("a", 1:9), paste0("b", 1:9))

df <- data.frame(
  a = asd$`paste0("a", 1:9)`,
  b = asd$`paste0("b", 1:9)`,
  c = sample(20, 81, replace = T)
)


# From discrete to continuous
df$a <- match(df$a, sort(unique(df$a)))
df$b <- match(df$b, sort(unique(df$b)))
z <- sample(10, 18, T)


# set color palettes
pal <- rev(diverging_hcl(palette = "Blue-Red", n = 11))
palEdge <- rev(sequential_hcl(palette = "Plasma", n = 11))

# plot
p_main <- ggplot(df, aes(a, b)) +
  geom_tile(aes(fill = c)) +
  scale_fill_gradientn("C",colors = pal,
    guide = guide_colorbar(frame.colour = "black",
      ticks.colour = "black")) +
  theme_classic() +
  labs(x = "A axis", y = "B axis") 
p_bottom <- ggplot() +
  geom_tile(data = tibble(a = 1:9, z = z[1:9]),
            aes(x = a, y = 0, fill = z, height = 0.3)) +
  theme_void() +
  scale_fill_gradientn("Z",limits = c(0,10),
                       colors = palEdge,
                       guide = guide_colorbar( 
                         frame.colour = "black", ticks.colour = "black"))
p_left <- ggplot() +
  theme_void()+
  geom_tile(data = tibble(b = 1:9, z = z[10:18]),
            aes(x = 0, y = b, fill = z, width = 0.3))  +
  scale_fill_gradientn("Z",limits = c(0,10),
    colors = palEdge,
    guide = guide_colorbar( frame.colour = "black", ticks.colour = "black"))

p_left + p_main +plot_spacer()+ p_bottom +
  plot_layout(guides = "collect",
              heights = c(1, .1),
              widths = c(.1, 1)) 

Created on 2021-02-21 by the reprex package (v1.0.0)代表 package (v1.0.0) 于 2021 年 2 月 21 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM