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

编程入门 行业动态 更新时间:2024-10-26 23:32:26
本文介绍了如何在R中使用ggplot绘制绘图区域的“外部"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我最近问了

但是,我试图找到一种一致的方式来绘制类似这样的东西(我很快在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绘制绘图区域的“外部"?

本文发布于:2023-07-18 05:08:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141292.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区域   如何在   ggplot   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!