【问题标题】:R: plotting maps as inset in a barplotR:将地图绘制为条形图中的插图
【发布时间】:2018-03-02 11:51:36
【问题描述】:

样本数据:

dat1 <- data.frame(month = 1:12, rain = sample(100:200,12, replace = T), temp = sample(20:30, 12,, replace = T))


par(mfrow=c(1,2))

with(dat1, barplot(rain), ylim = c(0,500))
par(new = TRUE)
with(dat1, plot(temp, type = "b", lwd = 2, col = "red",axes = F, bty = "n", xlab = "", ylab = "", ylim = c(12,30)))
axis(side = 4, las = 2)

该地块属于 Id = 12 的法国地区

library(raster)
dat <- getData('GADM', country='FRA', level=1)
plot(dat, col = ifelse(dat$ID_1 == 12, "red","grey"))

有什么办法可以把这两个数字合二为一,这样法国地图就出来了 作为插图?这与将条形图放在地图内的问题相反 How to plot barchart onto ggplot2 map

我试过了:

 dat1 <- data.frame(month = 1:12, rain = sample(100:200,12, replace = T), temp = sample(20:30, 12,, replace = T))

 layout(matrix(c(1,1,2,1), nrow = 2, ncol = 2, byrow = TRUE))
 with(dat1, barplot(rain), ylim = c(0,500))
 par(new = TRUE)
 with(dat1, plot(temp, type = "b", lwd = 2, col = "red",axes = F, bty = "n", xlab = "", ylab = "", ylim = c(12,30)))
      axis(side = 4, las = 2)

 library(raster)
 dat <- getData('GADM', country='FRA', level=1)
 plot(dat, col = ifelse(dat$ID_1 == 12, "red","grey"))

但这与我的条形图重叠。我怎样才能像右上角或左上角的小插图一样显示它。

【问题讨论】:

    标签: r ggplot2 raster shapefile gridextra


    【解决方案1】:

    我看到了 2 个选项。

    1/ 使用透明背景:

    ...
    par(bg=NA)
    plot(dat, col = ifelse(dat$ID_1 == 12, "red","grey"))
    

    2/增加布局矩阵:

    dat1 <- data.frame(month = 1:12, rain = sample(100:200,12, replace = T), temp = sample(20:30, 12,, replace = T))
    layout(matrix(c(2,1,1,1,1,1,1,1,1), nrow = 3, ncol = 3, byrow = TRUE))
    
    with(dat1, barplot(rain), ylim = c(0,500))
    par(new = TRUE)
    with(dat1, plot(temp, type = "b", lwd = 2, col = "red",axes = F, bty = "n", xlab = "", ylab = "", ylim = c(12,30)))
      axis(side = 4, las = 2)
    
    dat <- getData('GADM', country='FRA', level=1)
    par(bg=NA)
    plot(dat, col = ifelse(dat$ID_1 == 12, "red","grey"))
    

    【讨论】:

      猜你喜欢
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      • 2020-04-11
      • 1970-01-01
      • 2022-11-10
      相关资源
      最近更新 更多