R:绘制地图作为条形图中的插图

问题描述 投票:2回答:1

样本数据:

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"))

enter image description here

有什么方法可以将这两个数字合二为一,这样法国的地图就像插图一样?这与这里的问题相反,在这里,条形图放在地图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"))

enter image description here

但这与我的情节重叠。我怎样才能将它显示为顶部或顶部的小插图。

r ggplot2 raster shapefile gridextra
1个回答
2
投票

我看到2个选项。

1 /使用透明背景:

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

enter image description here

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"))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.