我想將 geom_point() 和 geom_boxplot() 合并到一個圖中作為附加影像。下面的代碼不起作用。任何人都可以幫忙嗎?謝謝!
library(grid)
library(ggplot2)
grid.newpage()
vp <- viewport(x=0.5,y=0.5,width = 1,height = 1)
push.Viewport(vp)
ggplot(mtcars) geom_point(aes(mpg, disp))
vp_sub <- viewport(x=0.5,y=0.7,width=0.3,height=0.3)
push.viewport(vp_sub)
ggplot(mtcars) geom_boxplot(aes(gear, disp, group = gear))

uj5u.com熱心網友回復:
除了patchwork::inset_element第二個選項是通過添加您的箱線圖ggplot2::annotation_custom。但是,與您相反,patchwork::inset_element您必須在主圖的資料范圍的絕對坐標中設定位置:
library(ggplot2)
bp <- ggplot(mtcars) geom_boxplot(aes(gear, disp, group = gear))
base <- ggplot(mtcars)
geom_point(aes(mpg, disp))
base
annotation_custom(grob = ggplotGrob(bp), xmin = 22.5, ymin = 250)

uj5u.com熱心網友回復:
也許你可以使用這個patchwork包,有一個
uj5u.com熱心網友回復:
使用viewport您可以通過這種方式完成您的任務。如果要保存在 png 中,只需注釋掉該行#png("my_plot.png")
library(grid)
library(ggplot2)
grid.newpage()
p1 <- ggplot(mtcars) geom_point(aes(mpg, disp))
p2 <- ggplot(mtcars) geom_boxplot(aes(gear, disp, group = gear))
vp <- viewport(x=0.5,y=0.5,width = 1,height = 1)vp_sub <- viewport(x=0.73,y=0.8,width=0.4,height=0.3)
#png("my_plot.png")
print(p1, vp=vp)
print(p2, vp=vp_sub)
dev.off()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427674.html
