如何在R中使用ggplot绘制绘图区域的“外部"? [英] How to plot 'outside' of plotting area using ggplot in R?
问题描述
我最近问了
但是,我试图找到一种一致的方式来绘制类似这样的东西(我很快在photoshop中制作了):
主要问题是能够操纵绘图区域外部"的新比例尺的坐标.有没有办法移动外面的瓷砖,以便我可以将它们放置在有意义的区域?
在绘图区域外进行绘图时,总是有两个经典选项:
- 使用
coord _...(剪辑="off")注释/绘图
- 制作不同的情节并将它们组合起来.
在我的拙见中,后一种选择通常可以提供更大的灵活性,并且可以减少头痛.
库(色彩空间)图书馆(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 =样本(20,81,替换= T))#从离散到连续df $ a<-匹配(df $ a,sort(unique(df $ a)))df $ b<-匹配(df $ b,sort(unique(df $ b)))z<-sample(10,18,T)#设置调色板pal<-rev(diverging_hcl(palette ="Blue-Red",n = 11))palEdge<-rev(sequential_hcl(palette ="Plasma",n = 11))# 阴谋p_main<-ggplot(df,aes(a,b))+geom_tile(aes(fill = c))+scale_fill_gradientn("C",颜色= pal,guide = guide_colorbar(frame.colour ="black",ticks.colour =黑色"))+theme_classic()+labs(x ="A轴",y ="B轴")p_bottom<-ggplot()+geom_tile(data = tibble(a = 1:9,z = z [1:9]),aes(x = a,y = 0,填充= z,高度= 0.3))+theme_void()+scale_fill_gradientn("Z",限制= c(0,10),颜色= palEdge,指南= 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,填充= z,宽度= 0.3))+scale_fill_gradientn("Z",限制= c(0,10),颜色= palEdge,guide = guide_colorbar(frame.colour ="black",ticks.colour ="black"))p_left + p_main + plot_spacer()+ p_bottom +plot_layout(guides ="collect",高度= c(1,.1),宽度= c(.1,1))
由 reprex软件包(v1.0.0)
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.
I am trying to create a heatmap in ggplot... however, outside of the axis I am trying to 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:
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):
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?
There are always the two classic options when plotting outside the plot area:
- annotate/ plot with
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)
这篇关于如何在R中使用ggplot绘制绘图区域的“外部"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!