下面是使用ggplot2
和dplyr
的解決方案。我不知道你的實際數據看起來像是否能夠持續擴展,所以我在代碼中即興創作。
library(ggplot2)
library(dplyr)
#loading map data
france <- map_data("france")
#creating dummy data for the continuous values that weren't provided in example above
dummy_data <- france %>% distinct(region)
#adding sample values
dummy_data <- dummy_data %>% mutate(Score = sample(x = c(1:100),
size = nrow(dummy_data),
replace = TRUE))
#merging sample values with the map's dataframe
france <- france %>% left_join(dummy_data)
#plotting the map
plot <- ggplot() + geom_map(data = france,
map = france,
aes(x = long, y = lat,map_id = region,
group = group,
fill = Score),
color = "black") +
scale_fill_gradient(low='lightgreen',high='darkgreen')+
theme(plot.subtitle = element_text(vjust = 1),
plot.caption = element_text(vjust = 1),
axis.line = element_line(colour = NA),
axis.ticks = element_line(colour = NA),
axis.text = element_text(size = 2, colour = NA),
panel.background = element_rect(fill = NA),
plot.background = element_rect(colour = NA)) +labs(x = NULL, y = NULL)
print(plot)
您能否提供更多的數據?爲了分配填充顏色,我們需要知道比例尺基於哪個數據屬性。 – kostr
另外 - 這裏有一個很好的資源來繪製R包: http://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html – kostr
謝謝你的鏈接。有沒有辦法做到這一點,沒有ggplot,只是與地圖?繪製的數據只是100分制的分數。每個地區(N = 95)都會得到一個分數,最常見的是從20到60。 – Max