我有一个数据帧包含名为ID,经度(LON),纬度(LAT)的变量,您可以download from here。我使用ggplot2
包使用以下代码在相同颜色的国家/地区地图上绘制了一些经度 - 纬度:
library(ggplot2)
library(raster)
read.csv("station.csv")
skorea<- getData("GADM", country= "KOR", level=1)
plot(skorea)
skorea<- fortify(skorea)
ggplot()+
geom_map(data= skorea, map= skorea, aes(x=long,y=lat,map_id=id,group=group),
fill=NA, colour="black") +
geom_point(data=station, aes(x=LON, y=LAT),
colour= "red", alpha=1,na.rm=T) +
scale_size(range=c(2,7))+
labs(title= "coordinate data of seoul",
x="Longitude", y= "Latitude")+
theme(title= element_text(hjust = 0.5,vjust = 1,face= c("bold")))
现在我希望其中一些点会根据其ID在不同颜色的地图上绘制。例如,我希望这些ID(111141,111142,111241,111281,111301,131141,131144,131161)将为蓝色,其余ID将保持红色。我怎么能这样做?
答案 0 :(得分:5)
也许:
library(raster)
library(rgeos)
library(ggplot2)
library(ggthemes)
skorea <- gSimplify(getData("GADM", country= "KOR", level=1), tol=0.001, TRUE)
skorea <- fortify(skorea)
st <- read.csv("station.csv")
st$color <- "red"
st[st$ID %in% c(111141, 111142, 111241, 111281, 111301, 131141, 131144, 131161),]$color <- "blue"
gg <- ggplot()
gg <- gg + geom_map(data=skorea, map=skorea,
aes(x=long, y=lat, map_id=id, group=group),
fill=NA, color="black")
gg <- gg + geom_point(data=st, aes(x=LON, y=LAT, color=color),
alpha=1, na.rm=TRUE)
gg <- gg + scale_size(range=c(2,7))
gg <- gg + scale_color_identity()
gg <- gg + labs(title= "coordinate data of seoul",
x="Longitude", y= "Latitude")
gg <- gg + coord_map()
gg <- gg + theme_map()
gg <- gg + theme(title= element_text(hjust = 0.5, vjust=1, face="bold"))
gg