iT邦幫忙

0

R語言-視覺化- 世界地圖( map in r.studio )

廢話不多說,直接附上code
影片含有程式碼詳細解說,若有誤再煩請告知,謝謝/images/emoticon/emoticon25.gif

資料下載處

library(ggplot2)
library(dplyr)
library(maps) #世界各地地圖
library(sf) 
world <- st_as_sf(map("world", plot = FALSE, fill = TRUE))
world %>% 
  ggplot(aes(geometry = geom)) + 
  geom_sf(fill = 'brown', color = 'black')+
  #coord_sf(xlim = c(-130,-90), ylim = c(30,40))+
  #geom_text annotate
  theme_void() #去除格線

#地圖實作--美國各州總統選舉
usa <- st_as_sf(map("state", plot = FALSE, fill = TRUE))
usa %>% 
  ggplot(aes(geometry = geom)) + 
  geom_sf(fill = 'brown', color = 'black')

library(data.table)
election <- fread(file="D:\\美國歷年總統選舉.csv",header=TRUE,stringsAsFactors = FALSE)
final <- left_join(election,usa,by="ID")#共和黨0 民主黨1
final %>% 
  subset(year=2016) %>% 
  ggplot(aes(geometry = geom)) + 
  geom_sf(aes(fill = win))

尚未有邦友留言

立即登入留言