Я хочу нанести один столбец для каждой страны на карту.
Мой код:
library(tidyverse)
library(rworldmap)
# Get map data
worldMap <- map_data("world")
# Select only some countries and add values
europe <- data.frame("country"=c("Austria", "Belgium", "Germany", "Spain", "Finland", "France",
"Greece", "Ireland", "Italy", "Netherlands", "Portugal",
"Bulgaria","Croatia","Cyprus", "Czech Republic","Denmark","Estonia", "Hungary",
"Latvia", "Lithuania","Luxembourg","Malta", "Poland", "Romania","Slovakia",
"Slovenia","Sweden","UK", "Switzerland",
"Ukraine", "Turkey", "Macedonia", "Norway", "Slovakia", "Serbia", "Montenegro",
"Moldova", "Kosovo", "Georgia", "Bosnia and Herzegovina", "Belarus",
"Armenia", "Albania", "Russia"),
"Growth"=c(1.0, 0.5, 0.7, 5.2, 5.9, 2.1,
1.4, 0.7, 5.9, 1.5, 2.2, rep(NA, 33)))
# Merge data and keep only Europe map data
worldMap$value <- europe$Growth[match(worldMap$region,europe$country)]
worldMap <- worldMap %>%
filter(region %in% europe$country)
# Plot it
P <- ggplot()+
geom_polygon(data = worldMap, aes(x=long, y = lat, group = group, fill=value),
colour = "white", size = 0.1)+
coord_map(xlim = c(-13, 35), ylim = c(32, 71))
Мне известно об этом решении , но я не могу его воспроизвести:
# Adding Centroids
centres <- worldMap %>%
group_by(region) %>%
summarize(long=mean(long, na.rm = T),
lat=mean(lat, na.rm = T))
centres$value <- europe$Growth[match(centres$region,europe$country)]
# Trying to add the barplots
europe$id <- (rep(1:length(europe$country)))
bar.testplot_list <-
lapply(1:length(europe$country), function(i) {
gt_plot <- ggplotGrob(
ggplot(europe[europe$id == i,])+
geom_bar(aes(factor(id),Growth,group=country), fill = rainbow(length(europe$country))[i],
position='dodge',stat='identity', color = "black") +
labs(x = NULL, y = NULL) +
theme(legend.position = "none", rect = element_blank(),
line = element_blank(), text = element_blank())
)
panel_coords <- gt_plot$layout[gt_plot$layout$name == "panel",]
gt_plot[panel_coords$t:panel_coords$b, panel_coords$l:panel_coords$r]
})
bar_annotation_list <- lapply(1:length(europe$country), function(i)
annotation_custom(bar.testplot_list[[i]],
xmin = centres$long[centres$region == as.character(europe$country[i])] - 5e3,
xmax = centres$long[centres$region == as.character(europe$country[i])] + 5e3,
ymin = centres$lat[centres$region == as.character(europe$country[i])] - 5e3,
ymax = centres$lat[centres$region == as.character(europe$country[i])] + 5e3) )
result_plot <- Reduce(`+`, bar_annotation_list, P)
result_plot
Я также вижу, что он не работает с coord_map
, но результат также не работает, когда я не включаю coord_map(xlim = c(-13, 35), ylim = c(32, 71))
.
-> Может кто-нибудь объяснить, как я могу добавить переменную роста в виде столбца для каждой соответствующей страны?