Добавить участки на карте в R - PullRequest
0 голосов
/ 31 мая 2018

Я хотел бы добавить графики на карте мира в нескольких местах.

С помощью этого кода я могу добавлять точки на карту:

# Worldmap with two sites
library(rworldmap)
plot(worldmap[which(worldmap$REGION != "Antarctica"), ])
points(c(3, 80), c(10, 40), cex = 3, pch = 16)

enter image description here

Но вместо этих двух пунктов я хотел бы добавить два следующих графика:

# Plots associated to these two sites
x_list <- list()
x_list[[1]] <- rnorm(12, 10, 1)
x_list[[2]] <- rnorm(12, 15, 1)

y_list <- list()
y_list[[1]] <- rnorm(12, 10, 1)
y_list[[2]] <- rnorm(12, 15, 1)

plot(x_list[[1]], y_list[[1]], pch = 16)
plot(x_list[[2]], y_list[[2]], pch = 16)

Результат должен возвращать что-то вроде этого: enter image description here

Как мне этого добиться (с синтаксисом base или с ggplot2)?

1 Ответ

0 голосов
/ 31 мая 2018

Вы можете использовать annotation_custom из ggplot2 для вставки врезанных графиков:

library(ggplot2)
library(dplyr)
worldmap <- map_data("world") %>% 
  filter(region != "Antarctica")
g1 <- ggplotGrob(
    ggplot() +
      geom_point(aes(x = x_list[[1]], y = y_list[[1]])) +
      theme_void() +
      theme(plot.background = element_rect(fill = "white")))
g2 <- ggplotGrob(
    ggplot() +
      geom_point(aes(x = x_list[[2]], y = y_list[[2]])) +
      theme_void() +
      theme(plot.background = element_rect(fill = "white")))
p <- ggplot(worldmap, aes(x=long, y=lat, group=group)) +
  geom_map(map = worldmap,
           aes(map_id = region),
           fill = "white",
           colour = "#7f7f7f",
           size = 0.5) 
p + 
  annotation_custom(grob = g1, xmin = 0, xmax = 40, ymin = 20, ymax = 40) +
  annotation_custom(grob = g1, xmin = 80, xmax = 120, ymin = 0, ymax = 20)

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...