如何在 R 中的地图上显示一些点
How show some points on a map in R
我在 R 中使用这段代码来显示地图。
library(maps)
library(raster)
library(rgeos)
library(ggplot2)
library(dplyr)
# Iran map
iran <- getData("GADM", country = "Iran", level = 1)
mymap <- fortify(iran)
mymap$id <- as.integer(mymap$id)
dat <- data.frame(id = rownames(iran@data),
state = iran@data$NAME_1,
pr = c(530,-42,1673,75,206,544,1490,118,75,
40,105,191,111,810, 609,425,418,550, 40, 425, -54,-50,
16, 18, 133,425, -30, 241,63, 191,100)) %>%
mutate(color_province = case_when(pr <= 50 ~ 'green',
pr > 150 ~ 'red',
TRUE ~ 'yellow'))
dat$id <- as.integer(dat$id)
mydf <- inner_join(mymap, dat, by = "id")
centers <- data.frame(gCentroid(iran, byid = TRUE))
centers$state <- dat$state
ggplot() +
geom_map(data = mydf,
map = mymap,
aes(map_id = id, group = group,
x = long, y = lat,
fill = as.factor(color_province))) +
coord_map() +
labs(x = "", y = "") +
scale_fill_manual(values = c("green", "red", "yellow"),
name = "areas")
现在我想添加一些不同大小的点(城市)来显示人口,但是我无法添加点。
# Cities
Khorasan <- c(60,35)
Kerman <- c(54,30)
Sistan <- c(61,27)
Cities <- rbind(Khorasan, Kerman, Sistan) %>%
as.data.frame()
colnames(Cities) <- c("ras","bal")
# Show the cities on the map
points(x=Cities$ras, y=Cities$bal, col="slateblue", cex=3, pch=20)
你能帮我吗,我怎样才能在地图上添加一些点并且这些点的大小显示它们的pupolation?
我会提出一种使用 geom_sf
的简单方法来处理坐标数据。有关更多功能,请参阅 package sf。来自 rnaturalearth
.
的国家/地区数据
library(rnaturalearth)
library(ggplot2)
part <- ne_countries(country="Iran", returnclass="sf")
Vahdat_Park <- c(51.484655, 35.653493)
Khorasan <- c(60,35)
Kerman <- c(54,30)
Sistan <- c(61,27)
sites <- data.frame(Vahdat_Park, Khorasan, Kerman, Sistan)
sites <- cbind( longitude=stack(sites[1,]), latitude=stack(sites[2,]))
sites
longitude.values longitude.ind latitude.values latitude.ind
1 51.48465 Vahdat_Park 35.65349 Vahdat_Park
2 60.00000 Khorasan 35.00000 Khorasan
3 54.00000 Kerman 30.00000 Kerman
4 61.00000 Sistan 27.00000 Sistan
ggplot(part) +
geom_sf() +
geom_point(data=sites,
aes(x = longitude.values, y = latitude.values, col=latitude.ind))
# adding a connection from Vahdat_Park to Khorasan
line <- data.frame(x=c(51.48465, 60.00000 ), y=c(35.65349, 35.00000))
ggplot(part) +
geom_sf() +
geom_point(data=sites,
aes(x = longitude.values, y = latitude.values, col=latitude.ind)) +
geom_line(data=line, aes(x=x, y=y ) )
我在 R 中使用这段代码来显示地图。
library(maps)
library(raster)
library(rgeos)
library(ggplot2)
library(dplyr)
# Iran map
iran <- getData("GADM", country = "Iran", level = 1)
mymap <- fortify(iran)
mymap$id <- as.integer(mymap$id)
dat <- data.frame(id = rownames(iran@data),
state = iran@data$NAME_1,
pr = c(530,-42,1673,75,206,544,1490,118,75,
40,105,191,111,810, 609,425,418,550, 40, 425, -54,-50,
16, 18, 133,425, -30, 241,63, 191,100)) %>%
mutate(color_province = case_when(pr <= 50 ~ 'green',
pr > 150 ~ 'red',
TRUE ~ 'yellow'))
dat$id <- as.integer(dat$id)
mydf <- inner_join(mymap, dat, by = "id")
centers <- data.frame(gCentroid(iran, byid = TRUE))
centers$state <- dat$state
ggplot() +
geom_map(data = mydf,
map = mymap,
aes(map_id = id, group = group,
x = long, y = lat,
fill = as.factor(color_province))) +
coord_map() +
labs(x = "", y = "") +
scale_fill_manual(values = c("green", "red", "yellow"),
name = "areas")
现在我想添加一些不同大小的点(城市)来显示人口,但是我无法添加点。
# Cities
Khorasan <- c(60,35)
Kerman <- c(54,30)
Sistan <- c(61,27)
Cities <- rbind(Khorasan, Kerman, Sistan) %>%
as.data.frame()
colnames(Cities) <- c("ras","bal")
# Show the cities on the map
points(x=Cities$ras, y=Cities$bal, col="slateblue", cex=3, pch=20)
你能帮我吗,我怎样才能在地图上添加一些点并且这些点的大小显示它们的pupolation?
我会提出一种使用 geom_sf
的简单方法来处理坐标数据。有关更多功能,请参阅 package sf。来自 rnaturalearth
.
library(rnaturalearth)
library(ggplot2)
part <- ne_countries(country="Iran", returnclass="sf")
Vahdat_Park <- c(51.484655, 35.653493)
Khorasan <- c(60,35)
Kerman <- c(54,30)
Sistan <- c(61,27)
sites <- data.frame(Vahdat_Park, Khorasan, Kerman, Sistan)
sites <- cbind( longitude=stack(sites[1,]), latitude=stack(sites[2,]))
sites
longitude.values longitude.ind latitude.values latitude.ind
1 51.48465 Vahdat_Park 35.65349 Vahdat_Park
2 60.00000 Khorasan 35.00000 Khorasan
3 54.00000 Kerman 30.00000 Kerman
4 61.00000 Sistan 27.00000 Sistan
ggplot(part) +
geom_sf() +
geom_point(data=sites,
aes(x = longitude.values, y = latitude.values, col=latitude.ind))
# adding a connection from Vahdat_Park to Khorasan
line <- data.frame(x=c(51.48465, 60.00000 ), y=c(35.65349, 35.00000))
ggplot(part) +
geom_sf() +
geom_point(data=sites,
aes(x = longitude.values, y = latitude.values, col=latitude.ind)) +
geom_line(data=line, aes(x=x, y=y ) )