如何在轴外添加geom_tile? [英] How to add geom_tile outside of axis?

查看:63
本文介绍了如何在轴外添加geom_tile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近问了

但是,我想知道是否可以将彩条移动到情节之外.例如,在这个我在photoshop中制作的模型中:

下面的@teunbrand答案很好...但是,我不得不编辑我的问题以更准确地反映我的问题...因此,我似乎无法应用@teunbrand答案

解决方案

您可以将栏放置在外部"通过超出限制绘制图形,然后不裁剪数据.这有两个缺点:

  • 您需要连续的刻度,而不是离散的刻度.以后,您可以根据需要重新标记连续刻度,就好像它们是离散的一样.
  • 您必须手动平衡条形图的精确位置和绘图边距.如何平衡这取决于您设备窗口的大小,我还没有找到一种适用于每种情况的开箱即用的解决方案.

以下示例:

  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))#从离散到连续df $ a<-匹配(df $ a,sort(unique(df $ a)))df $ b<-匹配(df $ b,sort(unique(df $ b)))z<-c(2,4,1,7,9,9)图书馆(tidyverse)图书馆(ggnewscale)ggplot(df)+geom_tile(aes(a,b,fill = c))+scale_fill_continuous(guide = guide_legend(order = 1))+new_scale_fill()+geom_tile(data = tibble(a = 1:3,#<-连续d = z [1:3]),aes(x = a,y = 0,填充= d,高度= 0.1))+geom_tile(data = tibble(b = 1:3,#<-连续d = z [4:6]),aes(x = 0,y = b,填充= d,宽度= 0.1))+scale_fill_viridis_c(option ="D",指南= guide_legend(order = 2))+#这是多余的coord_cartesian(clip ="off",xlim = c(0.5,NA),ylim = c(0.5,NA))+主题(Aspect.ratio = 1,plot.margin = margin(5.5,5.5,25,55,"pt")) 

更新示例的代码:

 库(ggnewscale)图书馆(tidyverse)库(色彩空间)库(ggplot2)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))df $ a<-匹配(df $ a,sort(unique(df $ a)))df $ b<-匹配(df $ b,sort(unique(df $ b)))z<-c(2,4,1,7,9,9)pal<-rev(diverging_hcl(palette ="Blue-Red",n = 11))palEdge<-rev(sequential_hcl(palette ="Plasma",n = 11))ggplot(df,aes(a,b))+geom_tile(aes(fill = c))+scale_fill_gradientn(颜色=好朋友,指南= guide_colorbar(frame.colour =黑色",ticks.colour =黑色";),名称="c";)+theme_classic()+实验室(x ="A",y ="B")+new_scale_fill()+geom_tile(数据= tibble(a = 1:3,z = z [1:3]),aes(x = a,y = 0,填充= z,高度= 0.1))+geom_tile(数据= tibble(b = 1:3,z = z [4:6]),aes(x = 0,y = b,填充= z,宽度= 0.1))+scale_fill_gradientn(颜色= palEdge,指南= guide_colorbar(frame.colour =黑色",ticks.colour =黑色";),名称="Main \ neffects"#(用于beta帽子符号))+coord_cartesian(clip ="off",xlim = c(0.5,NA),ylim = c(0.5,NA))+主题(Aspect.ratio = 1,plot.margin = margin(5.5,5.5,25,55,"pt")) 

I recently asked this question about how to add a custom bar along the axis of a ggplot. The code I used to produce this plot was:

library(ggnewscale)
library(tidyverse)

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

z <- c(2, 4, 1, 7, 9, 3)

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

# plot
library(ggplot2)

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", y = "B") +
  new_scale_fill() +
  geom_tile(
    data = tibble(a = paste0("a", 1:3), z = z[1:3]),
    aes(x = a, y = 0.5, fill = z, height = 0.1)
  ) +
  geom_tile(
    data = tibble(b = paste0("b", 1:3), z = z[4:6]),
    aes(x = 0.45, y = b, fill = z, width = 0.1)
  ) +
  scale_fill_gradientn(
    colors = palEdge,
    guide = guide_colorbar(
      frame.colour = "black",
      ticks.colour = "black"
    ),
    name = "Main\neffects"# for beta hat symbol
  )

This produces something that looks like this:

However, I was wondering if it's possible to move the coloured bar outside of the plot. For example, in this mock up I made in photoshop:

[EDIT] @teunbrand answer below is good... however, I have had to edit my question to more accurately reflect my problem... and as a result I cant seem to apply @teunbrand answer

解决方案

You can place the bars "outside" the plot by drawing beyond the limits and then not clipping the data. This has two drawbacks:

  • You need continuous scales instead of discrete ones. You can later re-label the continuous scales as if they were discrete if necessary.
  • You have to balance the precise placement of the bars against the plot margins manually. How to balance this depends on the size of your device window and I haven't found a solution that works out of the box for every case.

Example below:

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

# From discrete to continuous
df$a <- match(df$a, sort(unique(df$a)))
df$b <- match(df$b, sort(unique(df$b)))
z <- c(2, 4, 1, 7, 9, 3)

library(tidyverse)
library(ggnewscale)

ggplot(df) +
  geom_tile(aes(a, b, fill = c)) +
  scale_fill_continuous(guide = guide_legend(order = 1)) +
  new_scale_fill() +
  geom_tile(data = tibble(a = 1:3, # <- continuous
                          d = z[1:3]),
            aes(x = a, y = 0, fill = d, height = 0.1)) +
  geom_tile(data = tibble(b = 1:3, # <- continuous
                          d = z[4:6]),
            aes(x = 0, y = b, fill = d, width = 0.1)) +
  scale_fill_viridis_c(option = "D", 
                       guide = guide_legend(order = 2)) +
  # Here be the extra bit
  coord_cartesian(clip = "off", xlim = c(0.5, NA), ylim = c(0.5, NA)) +
  theme(
    aspect.ratio = 1,
    plot.margin = margin(5.5, 5.5, 25, 55, "pt")
  )

EDIT: Code for updated example:

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

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

df$a <- match(df$a, sort(unique(df$a)))
df$b <- match(df$b, sort(unique(df$b)))

z <- c(2, 4, 1, 7, 9, 3)

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

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", y = "B") +
  new_scale_fill() +
  geom_tile(
    data = tibble(a = 1:3, z = z[1:3]),
    aes(x = a, y = 0, fill = z, height = 0.1)
  ) +
  geom_tile(
    data = tibble(b = 1:3, z = z[4:6]),
    aes(x = 0, y = b, fill = z, width = 0.1)
  ) +
  scale_fill_gradientn(
    colors = palEdge,
    guide = guide_colorbar(
      frame.colour = "black",
      ticks.colour = "black"
    ),
    name = "Main\neffects"# for beta hat symbol
  ) +
  coord_cartesian(clip = "off", xlim = c(0.5, NA), ylim = c(0.5, NA)) +
  theme(
    aspect.ratio = 1,
    plot.margin = margin(5.5, 5.5, 25, 55, "pt")
  )

这篇关于如何在轴外添加geom_tile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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