【问题标题】:Mapping just one State of India and writing its name inside the state boundary仅绘制一个印度州并将其名称写在州界内
【发布时间】:2015-02-04 13:40:30
【问题描述】:

我已经下载了印度的 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")

【问题讨论】:

  • 您已经尝试过的一些代码会有所帮助:)
  • 谢谢。我添加了一个示例代码。但正如你所看到的,它是非常初步的,并没有帮助。

标签: r ggplot2 gis


【解决方案1】:

让我将@hrbrmstr 的重要教训应用到这个案例中。你可以从他身上学到很多东西。链接是here

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))

### 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()

### 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")

【讨论】:

  • 非常感谢这么多麻烦。我需要一些时间才能理解这一切,但我会逐行阅读。
  • @user3282777 很高兴。花点时间习惯这种任务。您可能想检查当您通过线路时数据的外观。这样,您将看到正在发生的事情。
猜你喜欢
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-28
  • 1970-01-01
相关资源
最近更新 更多