我正在嘗試使用特定坐標將多個圖(不同大小)繪制到網格上。為此,我正在使用該treemap包創建一個樹狀圖來獲取坐標,然后使用這些坐標,我嘗試放置我的個人圖。希望我下面的例子能更好地解釋我的問題。
首先,我創建了一些條形圖,然后創建了一個樹狀圖,如下所示:
library(ggplot2)
library(treemap)
#create data
df <- data.frame(var1 = c("A", "B", "C"),
var2 = runif(3),
var3 = runif(3),
var4 = runif(3),
size = c(10, 5, 3),
labels = c(1:3))
# Basic barplot
p1 <- ggplot(data=df, aes(x=var1, y=var2))
geom_bar(stat="identity")
p2 <- ggplot(data=df, aes(x=var1, y=var3))
geom_bar(stat="identity")
p3 <- ggplot(data=df, aes(x=var1, y=var4))
geom_bar(stat="identity")
#create treemap
tm <- treemap(df,
index = "labels",
vSize = "size",
palette = "Set2")
如果我們看一看樹形圖中的物件tm,我們可以看到,它提供的樹形圖的坐標如下所示,其中x0,y0,w,和h為坐標。
> tm$tm
labels vSize vColor stdErr vColorValue level x0 y0 w h color
1 1 10 1 10 NA 1 0.0000000 0.000 0.5555556 1.000 #66C2A5
2 2 5 1 5 NA 1 0.5555556 0.375 0.4444444 0.625 #FC8D62
3 3 3 1 3 NA 1 0.5555556 0.000 0.4444444 0.375 #8DA0CB

那么,什么林要做的是把我的三個barplots p1,p2和p3,并將它們放到樹形圖,這樣p1會在位置1樹形圖....p2會在適當的位置2等..
為清楚起見,我想要的結果如下所示:

關于我如何做到這一點的任何建議?我嘗試使用該patchwork軟體包,但遇到了重疊的問題......但我對使用任何軟體包的建議持開放態度(例如gridExtra或只是ggplot)
EDIT
to clarify things a little, as per the answer given by Peter below in relation to the patchwork package, I cant manually input the coordinates as the plot layout (I was originally using the area function in patchwork and inputting the coordinates of the treemap into the area function). But, as mentioned in the comments below, if I have numerous barplots, and the size and shape of the treemap changes, then I cant manually input the layout values. I am trying to find a way to automate the process
uj5u.com熱心網友回復:
這需要一些算術,并不完美,但應該是使用patchwork更復雜布局選項的開始。有幾個技巧可以解決:
treemap根據視口縮放其尺寸,這意味著它計算的位置取決于 RStudio 繪圖窗格中的查看器。相反,這不是絕對必要的,但您可能應該決定縱橫比以確保框的位置是您想要的。- 您需要將小數位和大小轉換為整數,而不是簡單地乘以某個數字和四舍五入,而是通過找到它們的等價分數的分母并乘以它。例如,第一個框的寬度為 0.55555,即 5/9;要獲得整數,我們需要知道這個盒子應該是 5 個單位寬,總共 9 個單位。取最大寬高是任意的;關鍵是要得到一個不等于 1.0 的值。
- 從不同的維度加減 1 并不是最好的,但它是一種足夠簡單的方法來獲得例如一個從 1 到 5 的盒子,以及從 6 到 9 的下一個盒子。
- 繪圖的順序不正確,但大小是......還沒有弄清楚原因,但為了嘗試診斷這一點,我為每個繪圖添加了標簽。
library(ggplot2)
library(treemap)
library(dplyr)
asp <- 1.25
tm <- treemap(df, index = "labels", vSize = "size", palette = "Set2", sortID = "labels",
aspRatio = asp)
denoms <- tm$tm %>%
tidyr::pivot_longer(w:h, names_to = "direction") %>%
filter(value < 1) %>%
group_by(direction) %>%
slice_max(value, n = 1) %>%
mutate(denom = MASS::fractions(value) %>%
stringr::str_extract("\\d $") %>%
as.numeric()) %>%
select(direction, denom) %>%
tibble::deframe()
denoms
#> h w
#> 8 9
areas <- tm$tm %>%
mutate(across(c(x0, w), ~. * denoms[["w"]]),
across(c(y0, h), ~. * denoms[["h"]]),
top = y0 1,
left = x0 1,
bottom = top h - 1,
right = left w - 1) %>%
select(top:right) %>%
purrr::pmap(function(top, left, bottom, right) patchwork::area(top, left, bottom, right)) %>%
purrr::reduce(c)
patchwork::wrap_plots(p1, p2, p3, design = areas)

uj5u.com熱心網友回復:
patchwork 似乎作業正常:
library(patchwork)
library(ggplot2)
design <- "
11122
11122
11133
"
p1 p2 p3 plot_layout(design = design)

由reprex 包(v2.0.1)于 2021 年 11 月 28 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/369345.html
上一篇:我可以強制Rdata.table%like%使用“fixed=TRUE”嗎?
下一篇:UseMethod("select")中的錯誤:沒有適用于'select'的方法應用于類"c('integer','nu
