R語言學(xué)習(xí)-歐洲地圖繪制

一:繪制世界地圖

library(maps)

map("world", fill = TRUE, col = rainbow(200),

? ? ylim = c(-60, 90), mar = c(0, 0, 0, 0))

title("world map")

效果圖:

二:繪制歐洲地圖

library(raster)

library(sf)

library(dplyr)

library(spData)

library(spDataLarge)

library(maps)

library(tmap)

library(eurostat)

#world <- rnaturalearth::countries110

#euro <- world[world$region_un=="Europe"&world$name!='Russia',]

#這兩行代碼可以忽略,是當(dāng)時(shí)測(cè)試用的

sp_data <- eurostat::get_eurostat("tgs00026",

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? time_format = "raw") %>%

? # subset to have only a single row per geo

? dplyr::filter(time == 2010, nchar(geo) == 4) %>%

? # categorise

? dplyr::mutate(income = cut_to_classes(values, n = 5))

geodata <- get_eurostat_geospatial(output_class = "sf",

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? resolution = "60",

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? nuts_level = 2,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? year = 2013)

map_data <- inner_join(geodata, sp_data)

# Fix / remove some broken entries for the demo purpose

geodata <- sf::st_make_valid(geodata)

geodata <- geodata[sf::st_is_valid(geodata),]

# Create and plot the map

map1 <- tmap::tm_shape(geodata) +

? tmap::tm_fill("lightgrey") +

? tmap::tm_shape(map_data) +

? tmap::tm_grid() +

? tmap::tm_polygons("income",

? ? ? ? ? ? ? ? ? ? title = "Disposable household\nincomes in 2010",?

? ? ? ? ? ? ? ? ? ? palette = "Oranges")

print(map1)

效果圖:

三:繪制歐洲背景的英國地圖

#必須的庫

library(tidyverse)

library(sf)

library(cowplot)

#讀取地圖源文件

uk<- st_read("uk.json")

europe <- st_read("europe.json")

#讀取倫敦和麥姆斯伯里的經(jīng)緯度

#uk_cities<- read_csv("UKcities.csv",col_types = cols(Latitude = col_number(), Long = col_number()))

#繪制地圖

ditu <- ggplot()+

? geom_sf(data = europe, colour = "#d9d9d9",fill="#d9d9d9")+

? geom_sf(data = uk, colour = "#d9d9d9",fill="yellow")+

? coord_sf(xlim = c(-10, 20), ylim = c(40, 65), expand = TRUE, clip = "on")+

? geom_point(data = uk_cities , aes(x=Long,y=Latitude),colour="green",size=2,alpha=0.8)+

? geom_text(data = uk_cities,aes(x=Long,y=Latitude,label=Cities),size =1.5,vjust = 0, nudge_y = 0.5)+

? ggtitle("the Life of Thomas Hobbes")+

? theme(panel.grid = element_blank(),

? ? ? ? panel.background = element_rect(fill = "Aliceblue"),

? ? ? ? axis.text = element_blank(),

? ? ? ? axis.ticks = element_blank(),

? ? ? ? axis.title = element_blank(),

? ? ? ? plot.title = element_text(size = 15, hjust = 0.5, vjust=0),

? ? ? ? plot.margin = unit(c(0, 0, 0, 0), "inches"))

#ggdraw() + draw_plot(ditu)

print(ditu)

效果圖:


四:繪制英國郡一級(jí)的地圖

library(maps)

library(mapdata)

library(maptools)

library(rgdal)

library(ggmap)

library(ggplot2)

library(rgeos)

library(broom)

library(plyr)

library(tidyverse)

library(sf)

library(cowplot)

#step2標(biāo)示城市(放棄)

#UKC<- read_csv("UKcities2.csv",col_types = cols(Latitude = col_number(), Long = col_number()))

setwd(".../Desktop")

getwd()

#step1 畫出英國地圖

#Load the shapefile - make sure you change the filepath to where you saved the shapefiles

file.exists('/Users/duchen/Downloads/level/level.shp')

list.files('/Users/duchen/Downloads/level', pattern='\\.shp$')

shapefile <- readOGR(dsn=path.expand("/Users/duchen/Downloads/level"), layer="level")?

#一定要用path.expand,之前直接上就一直報(bào)錯(cuò)

#Reshape for ggplot2 using the Broom package

mapdata <- tidy(shapefile, region="nuts218nm") #This might take a few minutes

head(mapdata)

#Check the shapefile has loaded correctly by plotting an outline map of the UK

gg <- ggplot() + geom_polygon(data = mapdata, aes(x = long, y = lat, group = group), color = "#FFFFFF", size = 0.25)+

#geom_point(data = UKC , aes(x=Long,y=Latitude),colour="red",size=3,alpha=0.8)+

#(放棄了)

# ?geom_text(data = UKC,aes(x=Long,y=Latitude,label=Cities),size =4,vjust = 0, nudge_y = 0.5)

gg <- gg + coord_fixed(1) #This gives the map a 1:1 aspect ratio to prevent the map from appearing squashed

print(gg)

效果圖:


五:繪制新西蘭地圖

library(raster)

library(sf)

library(dplyr)

library(spData)

library(spDataLarge)

library(maps)

library(tmap)

# Add fill layer to nz shape

tm_shape(europe) +tm_fill()

# Add border layer to nz shape

tm_shape(nz) +

? tm_borders()

# Add fill and border layers to nz shape

tm_shape(nz) +

? tm_fill() +

? tm_borders()

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 0228 1.some refernce materialR CookbookR in Actionggplot2...
    TX_ab85閱讀 952評(píng)論 0 0
  • 嘗試用R制作中國地圖的熱圖 library(spatial) library(rgdal) library(ras...
    luo_small閱讀 2,253評(píng)論 0 0
  • http://blog.sina.com.cn/s/blog_6bc5205e0102vma9.html inst...
    付德剛Q閱讀 3,221評(píng)論 0 3
  • 16宿命:用概率思維提高你的勝算 以前的我是風(fēng)險(xiǎn)厭惡者,不喜歡去冒險(xiǎn),但是人生放棄了冒險(xiǎn),也就放棄了無數(shù)的可能。 ...
    yichen大刀閱讀 7,687評(píng)論 0 4
  • 公元:2019年11月28日19時(shí)42分農(nóng)歷:二零一九年 十一月 初三日 戌時(shí)干支:己亥乙亥己巳甲戌當(dāng)月節(jié)氣:立冬...
    石放閱讀 7,412評(píng)論 0 2

友情鏈接更多精彩內(nèi)容