Orpheus
Orpheus

Reputation: 329

How to plot longitude-latitude points on a map with different color using ggplot2 package?

I have a dataframe contains variable named ID, longitude(LON),latitude(LAT) which you can download from here. I have plotted some longitude-latitude on a country map with same color using ggplot2 package with following code:

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

And I got the following plot enter image description here

Now I want some of those point will be plotted on map with different color according to their ID. For example, I want these ID (111141,111142,111241,111281,111301,131141,131144,131161) will be with blue color and rest of ID will remain with red color. How can I do this?

Upvotes: 3

Views: 5976

Answers (1)

hrbrmstr
hrbrmstr

Reputation: 78792

Perhaps:

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

enter image description here

Upvotes: 5

Related Questions