使用patchwork的inset_element时内嵌图倒置问题求助
解决patchwork中inset_element内嵌图倒置且位置不符的问题
问题核心是对inset_element的参数坐标逻辑理解偏差:patchwork的布局坐标系里,y轴是从底部(0)到顶部(1)的。你之前设置bottom=1、top=0.7,相当于让内嵌图的底部对齐主图顶部,顶部对齐主图的0.7位置——因为bottom值大于top值,图形被强制上下翻转了。
要在主图右上角放置方向正常的内嵌图,只需要保证bottom小于top,同时参数对应右上角区域即可:
library("ggplot2") library("patchwork") set.seed(0922023) a <- rnorm(1000) b <- rnorm(1000) c <- rnorm(1000) d <- c + rnorm(1000) df <- cbind.data.frame(a, b, c, d) plot1 <- ggplot(df, aes(x = a, y = b)) + geom_point() plot2 <- ggplot(df, aes(x = c, y = d)) + geom_point() # 正确设置:bottom < top,对应右上角区域 inset <- inset_element( plot1, left = 0.7, bottom = 0.7, right = 1, top = 1 ) combined_plot <- plot2 + inset combined_plot
这样设置后,内嵌图的底部对齐主图0.7高度处,顶部对齐主图顶部,左右范围从0.7到1,刚好占据右上角区域,图形方向完全正常,不需要提前翻转原地图或子图。
内容的提问来源于stack exchange,提问作者Julian
相关产品推荐
相关产品推荐

