我已经下载了印度的gadm地图(SpatialPolygonsDataFrame).当我使用spplot时,它会绘制完整的印度地图以及州界.
A.是否有可能只绘制一个国家(比如阿萨姆邦)而不是其他国家的边界?
B.我可以在各自的界限内写下国家的名字吗?
C.或者假设我只想为一个特定国家而不是其他国家着色
将不胜感激.谢谢.
(示例代码如下所示.但是它绘制了完整的国家和所有国家.如果我只想在国内绘制一个国家,该怎么办)
library(sp)
library(RColorBrewer)
load("IND_adm1.RData")
abc <- read.csv("crime.csv", header = TRUE)
temp <- abc[,c(1,4)]
State = temp[1:38,1]
Offence = temp[1:38,2]
afg <- data.frame(State, Offence)
gadm$Offence <- afg[1:35,]
col <- brewer.pal(n = 9, name = "OrRd")
spplot(gadm, "Offence", col.regions = col, at = c(0,50,100,200, 500,1000, 1500,2000,2500,4000), main = "Offence")
Run Code Online (Sandbox Code Playgroud)
让我将@hrbrmstr的重要教训应用到这个案例中.你可以从他身上学到很多东西.链接在这里.
library(raster)
library(rgdal)
library(rgeos)
library(ggplot2)
library(dplyr)
### Q1: Assam only
### Get data
india <- getData("GADM", country = "India", level = 1)
### Choose Assam
assam <- subset(india, NAME_1 == "Assam")
### SPDF to DF
map <- fortify(assam)
### Draw a map
ggplot() +
geom_map(data = map, map = map, aes(x = long, y = lat, map_id = id, group = group))
Run Code Online (Sandbox Code Playgroud)

### Q2: State with their names
map <- fortify(india)
map$id <- as.integer(map$id)
dat <- data.frame(id = 1:(length(india@data$NAME_1)), state = india@data$NAME_1)
map_df <- inner_join(map, dat, by = "id")
centers <- data.frame(gCentroid(india, byid = TRUE))
centers$state <- dat$state
### This is hrbrmstr's own function
theme_map <- function (base_size = 12, base_family = "") {
theme_gray(base_size = base_size, base_family = base_family) %+replace%
theme(
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.ticks.length=unit(0.3, "lines"),
axis.ticks.margin=unit(0.5, "lines"),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.background=element_rect(fill="white", colour=NA),
legend.key=element_rect(colour="white"),
legend.key.size=unit(1.5, "lines"),
legend.position="right",
legend.text=element_text(size=rel(1.2)),
legend.title=element_text(size=rel(1.4), face="bold", hjust=0),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.margin=unit(0, "lines"),
plot.background=element_blank(),
plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
plot.title=element_text(size=rel(1.8), face="bold", hjust=0.5),
strip.background=element_rect(fill="grey90", colour="grey50"),
strip.text.x=element_text(size=rel(0.8)),
strip.text.y=element_text(size=rel(0.8), angle=-90)
)
}
ggplot() +
geom_map(data = map_df, map = map_df,
aes(map_id = id, x = long, y = lat, group = group),
color = "#ffffff", fill = "#bbbbbb", size = 0.25) +
geom_text(data = centers, aes(label = state, x = x, y = y), size = 2) +
coord_map() +
labs(x = "", y = "", title = "India State") +
theme_map()
Run Code Online (Sandbox Code Playgroud)

### Q3: Add color to one state
### Create a column for color. Assam will have a different color.
map_df2 <- transform(map_df, hue = ifelse(state == "Assam", "a", NA))
ggplot() +
geom_map(data = map_df2, map = map_df2,
aes(map_id = id, x = long, y = lat, group = group, fill = hue),
color = "#ffffff", size = 0.25) +
geom_text(data = centers, aes(label = state, x = x, y = y), size = 2) +
coord_map() +
labs(x = "", y = "", title = "India State") +
theme_map() +
theme(legend.position = "none")
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
2725 次 |
| 最近记录: |