一:绘制世界地图
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',]
#这两行代码可以忽略,是当时测试用的
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")
#读取伦敦和麦姆斯伯里的经纬度
#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)
效果图:
四:绘制英国郡一级的地图
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标示城市(放弃)
#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,之前直接上就一直报错
#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()