簡體   English   中英

在 R 中使用 ggplot 將 geom_tile 移動到繪圖區域之外?

[英]Moving geom_tile outside of plotting area using ggplot in R?

我最近問了這個關於使用geom_tileggplot在 plot 區域之外繪圖的問題,我得到了一個很好的答案。 但我想知道如何調整前一個答案的結果並手動移動正在繪制的額外條。

例如,下面是我用來創建 plot 的代碼。 我正在使用patchwork而成的 package 將額外的geom_tile條添加到繪圖區域之外,如下所示:

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

# create data
df <- data.frame(
  a = paste0("a", c(1,1,1,2,2,2,3,3,3)),
  b = paste0("b", c(1,2,3,1,2,3,1,2,3)),
  c = c(-10, 3, 5, -2, 9, 1, -5, -2, 0)
)

# create some extra data to plot
extraBars <- rnorm(6)

# From discrete to continuous
df$a <- match(df$a, sort(unique(df$a)))
df$b <- match(df$b, sort(unique(df$b)))

# create labels
labels_a <- paste0("a", 1:length(df$a))
labels_b <- paste0("b", 1:length(df$b))

# set limits and colour palette 
intLims <- range(df$c)
limitsInt <- c(floor(intLims[1]), ceiling(intLims[2]))
intPal = rev(diverging_hcl(palette = "Blue-Red 3", n = 100))

# some preprcessing
NoOfa <- length(unique(df$a))
NoOfb <- length(unique(df$b))
NoOfd<- length(extraBars)

# main heatmap plot
p_main <- ggplot(df, aes(a, b)) +
  geom_tile(aes(fill = c)) +
  scale_y_continuous(breaks = c(1:length(labels_b)), labels = labels_b) +
  scale_x_continuous(breaks = c(1:length(labels_a)), labels = labels_a) +
  scale_fill_gradientn(
    limits = limitsInt,
    colors = intPal,
    guide = guide_colorbar(
      frame.colour = "black",
      ticks.colour = "black"
    ),
    name = "C"
  ) +
  theme_classic() +
  labs(x = "a", y = "b")



# bottom bar
p_bottom <- ggplot() +
  geom_tile(
    data = tibble(a = 1:NoOfa, extraBars = extraBars[(NoOfb + 1):NoOfd]),
    aes(x = a, y = 0, fill = extraBars)
  ) +
  scale_fill_gradientn(
    limits = limitsInt,
    colors = intPal,
    guide = guide_colorbar(
      frame.colour = "black", ticks.colour = "black"
    )
  ) +
  theme_void() +
  theme(legend.position = "none")

# left bar
p_left <- ggplot() +
  geom_tile(
    data = tibble(b = 1:NoOfb, extraBars = extraBars[1:NoOfb]),
    aes(x = 0, y = b, fill = extraBars)
  ) +
  scale_fill_gradientn(
    limits = limitsInt,
    colors = intPal,
    guide = guide_colorbar(
      frame.colour = "black",
      ticks.colour = "black"
    ),
    name = "extraBars"
  ) +
  theme_void()

# add them all together
p <- p_left + p_main + plot_spacer() + p_bottom +
  plot_layout(
    guides = "collect",
    heights = c(1, 0.07),
    widths = c(0.07, 1)
  )
p

這將創建一個 plot ,如下所示: 熱圖

但是,我想做的是將邊緣周圍的額外條與軸標題標簽交換。 下面是我在 Photoshop 中創建的模型,用於解釋我想要實現的目標:

改變的熱圖

我想知道我怎么能做到這一點?

您可以使用的一種方法是使用 grobs(網格對象)將彩色條放在 plot 之外,並使用annotation_custom將它們添加到 plot。 使用您的示例,我將向您展示如何做到這一點。 就像您的示例一樣,您必須手動為酒吧 map 和 colors ,但我不打算在這里介紹。 此處的目的僅是演示如何 go 來創建一般的 grobs 並將它們添加到 plot。

原則上,它與patchwork的方法非常相似。 首先,您要自己定義 grobs,然后通過annotation_custom()將它們添加到 plot 並調整定位。 很難准確地解釋你應該如何以及在哪里移動它們——根據我使用 grobs 的經驗,我發現我必須在准確的位置上擺弄很多。 這是一般代碼和結果。

library(grid)
library(gridExtra)
library(ggpubr)   # we use the convenient text_grob() function from here

# define your grobs
bottom_grob <- rectGrob(x=1:3, y=1, gp=gpar( fill=rainbow(6)[1:3], alpha=0.5 ))
left_grob <- rectGrob(x=1, y=1:3, gp=gpar( fill=rainbow(6)[4:6], alpha=0.5 ))
bottom_textgrob <- text_grob(x=1:3, y=0,label=round(extraBars[1:3],2), size=10)
left_textgrob <- text_grob(x=1, y=1:3, label=round(extraBars[4:6],2), size=10, rot=90)

# add to the plot
the_plot <- 
  ggplot(df, aes(a, b)) +
  geom_tile(aes(fill = c)) +
  scale_y_continuous(breaks = c(1:length(labels_b)), labels = labels_b) +
  scale_x_continuous(breaks = c(1:length(labels_a)), labels = labels_a) +
  scale_fill_gradientn(
    limits = limitsInt,
    colors = intPal,
    guide = guide_colorbar(
      frame.colour = "black",
      ticks.colour = "black"
    ),
    name = "C"
  ) +
  theme_classic() +
  theme(
    axis.title.x = element_text(margin=margin(t=30)),
    axis.title.y = element_text(margin=margin(r=30))
  ) +
  labs(x = "a", y = "b") +
  coord_cartesian(clip="off") +
  annotation_custom(grob=left_grob, xmin=0.02, xmax=0.15, ymin=0, ymax=1) +
  annotation_custom(grob=bottom_grob, xmin=0, xmax=1, ymin=-0.1, ymax=0.1) +
  annotation_custom(grob=bottom_textgrob, xmin=0, xmax=1, ymin=0.1, ymax=0.1) +
  annotation_custom(grob=left_textgrob, xmin=0, xmax=0.15, ymin=0, ymax=1)

the_plot

在此處輸入圖像描述

關於上面代碼的一些重要說明:

  • 請注意,您可以為創建 grob 的值提供向量。 我們這樣做是為了通過為 x 或 y 提供一個位置向量來創建多個矩形。
  • 通過為每個主題元素添加邊距,我將 x 軸標題“向下”和 y 軸標題“向左”移動。 這為 grob 提供了空間。
  • 您必須通過在coord_*函數之一中指示clip="off"來關閉剪輯。 這允許東西(grobs)出現在 plot 的面板之外(最小和最大 x 和 y 值之間的位置)。

Rainbow colors 僅用於更清楚地演示發生了什么,但您可能希望 map 將那些與extraBars或其他色標相關的特定值。 你看到的是沒有第二個傳說......但老實說,額外的傳說讓我的腦海里的事情變得相當混亂。 最好將外欄全部設為一種顏色和/或將文本值放在它們上面,就像我在這里向您展示的那樣。 我認為它傳達了相同的信息,但這也是一個見仁見智的問題。

希望這對您有所幫助並為您提供一些工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM