一:繪制世界地圖
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)
效果圖:
三:繪制歐洲背景的英國(guó)地圖
#必須的庫(kù)
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)
效果圖:
四:繪制英國(guó)郡一級(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 畫出英國(guó)地圖
#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()