只映射一个印度国家并在国界内写下它的名字 [英] Mapping just one State of India and writing its name inside the state boundary

查看:488
本文介绍了只映射一个印度国家并在国界内写下它的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为印度下载了gadm地图(SpatialPolygonsDataFrame)。当我使用spplot时,它会绘制印度的完整地图以及国界。



是否有可能画出一个国家的边界​​(比如阿萨姆邦)而不是其他国家?

B。我可以在各自的边界内写下国家的名字吗?

C。或者假设我只想给一个特定的状态上色,而不是其他颜色。


感谢您的帮助。 (示例代码如下,但它绘制了整个国家和所有国家的情况,如果我只想在国内绘制一个国家,情况会怎样呢?)

p>

 库(sp)
库(RColorBrewer)

load(IND_adm1.RData)
abc < - read.csv(crime.csv,header = TRUE)
temp < - abc [,c(1,4)]
State = temp [1: 38,1]
Offense = temp [1:38,2]
afg < - data.frame(State,Offense)
gadm $ Offense< - afg [1:35, ]
col < - brewer.pal(n = 9,name =OrRd)
spplot(gadm,Offense,col.regions = col,at = c(0,50,100,200,500 ,1000,1500,2000,2500,4000),main =Offense)


解决方案

让我将@hrbrmstr的伟大教训应用于这种情况。你可以从他那里学到很多东西。链接此处

 库(栅格)
库(rgdal)
库(rgeos)
库(ggplot2)
库(dplyr)

### Q1:只有阿萨姆

###获取数据
印度< - getData( GADM,country =India,level = 1)

###选择Assam
assam< - 子集(印度,NAME_1 ==Assam)

### SPDF到DF
map< - fortify(assam)

###绘制地图

ggplot()+
geom_map(data = map,map = map,aes(x = long,y = lat,map_id = id,group = group))

  ### Q2:以其名称命名

map< - fortify(印度)
map $ id< - as。整数(地图$ id)

dat < - data.frame(id = 1:(length(india @ data $ NAME_1)),state = india @ data $ NAME_1)
map_df < - in ner_join(map,dat,by =id)

中心< - data.frame(gCentroid(印度,byid = TRUE))
中心$ state < - dat $ state


###这是hrbrmstr自己的函数
theme_map< - function(base_size = 12,base_family =){
theme_gray(base_size = base_size, base_family = base_family)%+替换%
主题(
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = (),
axis.ticks = element_blank(),
axis.ticks.length =单位(0.3,lines),
axis.ticks.margin =单位(0.5, (),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.background = element_rect(fill =white,color = NA),
legend.key = element_rect(color =white),
legend.key.size =单位(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 =单位(0,lines),
plot .background = element_blank(),
plot.margin =单位(c(1,1,0.5,0.5),lines),
plot.title = element_text(size = rel(1.8),
strip.background = element_rect(fill =grey90,color =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:将颜色添加到一个状态

###为颜色创建一列。阿萨姆将有不同的颜色。
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)


I have downloaded gadm map for India (SpatialPolygonsDataFrame). When I use spplot it draws the complete map of India alongwith State boundaries.

A. Is it possible to just draw the boundary of one State (say, Assam) and not of others?

B. Can I write the names of States inside their respective boundaries?

C. Or suppose I want to color only one particular State and not others

Will be grateful for help. Thanks.

(A sample code is as below. But it plots full country and all States. What if I want only one State to be plotted inside the country)

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

解决方案

Let me apply the great lesson of @hrbrmstr to this case. You can learn a lot from him. The link is 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")

这篇关于只映射一个印度国家并在国界内写下它的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆