【问题标题】:Offset size and overlay of plots in RR中图的偏移大小和叠加
【发布时间】:2014-02-05 21:56:18
【问题描述】:

我有两个想要以特定方式叠加的图。我希望一个坐在另一个里面,而不是像使用 par() 时那样并排,但大小约为四分之一。

更多细节:我的一个图是地图,另一个是带有彩色象限的散点图。彩色象限代表绘制在地图上的颜色,所以我想将它很好地插入到与地图相同的图中,以便作为图例。

提前致谢

【问题讨论】:

标签: r plot overlay


【解决方案1】:

这是一个示例,尽管 cmets 中的链接指向类似的方法。

获取 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 了解详情。

【讨论】:

    【解决方案2】:

    这就是我过去的做法

    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) 附近插入了每个对象的小版本 - 就像通常所做的那样

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-25
      • 2022-07-14
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 2014-02-28
      • 1970-01-01
      相关资源
      最近更新 更多