インセット自体の幅と高さを変更せずに、inset
使用を柔軟に配置するにはどうすればよいですか?ggpmisc
library(tidyverse)
library(sf)
library(ggpmisc)
#data
nc <- st_read(system.file("gpkg/nc.gpkg", package = "sf"), quiet = TRUE) %>%
st_transform(st_crs(4326)) %>%
st_cast('POLYGON')
#create timeseries data
nc_2 <- rbind(nc %>% mutate(timepoint = 1), nc %>% mutate(timepoint = 2))
#create base plot
nc_2_base <- ggplot(data = nc_2) +
geom_sf(aes(fill = BIR74)) +
coord_sf(xlim = c(-80, -76),
ylim = c(32, 37), expand = FALSE)
#facet plot
nc_2_main <- nc_2_base + facet_wrap(~timepoint, dir = "h", ncol = 2)
#extract number of timepoints
nmax_rep_nc <- length(unique(nc_2$timepoint))
#create insets
insets_nc <- lapply(seq_len(nmax_rep_nc), function(i) {
nc_2_base + ggforce::facet_wrap_paginate(~ timepoint, nrow = 1, ncol = 1, page = i) +
coord_sf(xlim = c(-79.5, -78.5), ylim = c(34.5, 35.5)) +
theme(strip.background = element_blank(),
strip.text = element_blank(),
axis.title = element_blank(),
plot.background = element_blank(),
legend.position = "none")
})
インセットを配置するには、目的の位置tibble
を示すを作成する必要があります。ここでは、それらを左下隅に配置したいので、および( 、ここのビネットから取得できます) を指定し、インセットのサイズをメイン プロットの 50% にします ( ):x
y
x = 0.0
y = 0
x
y
0 - 1
vp.width = 0.5, vp.height = 0.5
insets_nc_tibble <- tibble(x = rep(0.0, nmax_rep_nc),
y = rep(0.0, nmax_rep_nc),
plot = insets_nc,
timepoint = unique(nc_2$timepoint))
#add inset to plot:
nc_2_main +
geom_rect(xmin = -79.5, xmax = -78.5, ymin = 34.5, ymax = 35.5,
fill = NA, colour = "red", size = 1.5) +
geom_plot_npc(data = insets_nc_tibble,
aes(npcx = x, npcy = y, label = plot,
vp.width = 0.5, vp.height = 0.5))
これにより、次のプロットが生成されます。
(0, 0)
しかし、私が望んでいたように、インセットが左下隅に正しくありません。はめ込みをこのサイズのままにして、角に直接移動するにはどうすればよいですか?
インセットのサイズを小さくすると効果があるようですが、これは完全に試行錯誤であり、インセットのサイズを小さくしたくありません。
#reduce size
nc_2_main +
geom_rect(xmin = -79.5, xmax = -78.5, ymin = 34.5, ymax = 35.5,
fill = NA, colour = "red", size = 1.5) +
geom_plot_npc(data = insets_nc_tibble,
aes(npcx = x, npcy = y, label = plot,
vp.width = 0.5, vp.height = 0.25))
これは、より良い配置ですが、私が望む正しいサイズではないこのプロットを生成します:
文字列でコーナーを指定することもできますが、これは役に立ちません:
#insets_nc_tibble <- tibble(x = rep("left", nmax_rep_nc),
# y = rep("bottom", nmax_rep_nc),
# plot = insets_nc,
# timepoint = unique(nc_2$timepoint))
この質問は、私の以前の回答やその他の回答へのフォローアップです。
サイズの変更、位置の変更方法がわかりません。指定x, y = 0, 0
は、インセットの左下隅を設定する必要があることを意味すると思いまし0, 0
たが、ここではそうではないようですか?
何か案は?
ありがとう