簡體   English   中英

R中圖的偏移量大小和覆蓋

[英]Offset size and overlay of plots in R

我有兩幅圖,我想以特定的方式進行疊加。 我不希望像使用par()那樣並排放置,而是希望其中一個坐在另一個內部,但是要大約四分之一大小。

更多詳細信息:我的地塊之一是地圖,另一個是帶有彩色象限的散點圖。 彩色的象限代表繪制在地圖上的顏色,因此我想在與地圖相同的繪圖中很好地插入它,以便將其用作圖例。

提前致謝

這是一個示例,盡管注釋中的鏈接指向相似的方法。

抓取shapefile:

download.file(file.path('http://www.naturalearthdata.com/http/',
                        'www.naturalearthdata.com/download/50m',
                        'cultural/ne_50m_admin_1_states_provinces_lakes.zip'),
              {f <- tempfile()})
unzip(f, exdir=tempdir())

繪圖:

library(rgdal)
shp <- readOGR(tempdir(), 'ne_50m_admin_1_states_provinces_lakes')
plot(subset(shp, admin=='Australia'), 
     col=sample(c('#7fc97f', '#beaed4', '#fdc086', '#ffff99'), 
                9, repl=TRUE))
opar <- par(plt=c(0.75, 0.95, 0.75, 0.95), new=TRUE)
plot.new()
plot.window(xlim=c(0, 1), ylim=c(0, 1), xaxs='i', yaxs='i')
rect(0, 0, 0.5, 0.5, border=NA, col='#7fc97f')
rect(0.5, 0, 1, 0.5, border=NA, col='#beaed4')
rect(0, 0.5, 0.5, 1, border=NA, col='#fdc086')
rect(0.5, 0.5, 1, 1, border=NA, col='#ffff99')
points(runif(100), runif(100), pch=20, cex=0.8)
box(lwd=2)
par(opar)

有關說明,請參見?par下的plt

帶插圖的地圖

這是我過去的做法

grid.newpage()
vp <- viewport(width = 1, height = 1)
submain <- viewport(width = 0.9, height = 0.9, x = 0.5, y = 1,just=c("center","top"))
print(p, vp = submain)
subvp2 <- viewport(width = 0.2, height = 0.2, x = 0.39, y = 0.35,just=c("right","top"))
print(hi, vp = subvp2)
subvp1 <- viewport(width = 0.28, height = 0.28, x = 0.0, y = 0.1,just=c("left","bottom"))
print(ak, vp = subvp1)

在我的情況下,p,ak和hi是gg對象(使用ggplot創建的地圖),我在主要用途地圖(p)附近插入了每個地圖的一個小版本-通常這樣做是

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM