Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

r - ploting ggmap with geom Points (lat_long) annotated 1 to19. Points data is in CSVfile

I am plotting with ggmap and ggplot2 using geom_point. I want to add annotation text (i.e., 1 to 19) close to the points as well.

Here is my code:

setwd("../Documents/MAPS")
library(ggplot2)
library(mapproj)
library(maps)
library(maptools)
library(rgdal)
library(ggmap)
library(sp)

bdl_sites <- get_map(location  =c(lon = 34.832, lat = 0.852), colour = "colour",
                      source = "google", maptype = "terrain", zoom = 9)
save(bdl_sites, file = "bdl_sites.rda")
load(file = "bdl_sites.rda")
BDL_Org_Data.csv <- read.csv("BDL_Org_Data.csv")
BDL_Org_DataF.csv <- fortify(BDL_Org_Data.csv, region = "ORGANIZATION_ID")

ggmap(bdl_sites) +  
geom_point(data = BDL_Org_DataF.csv, aes(x = long, y = lat),
           colour = "red", size = 2, alpha = .5) +
annotate("text", x=BDL_Org_DataF.csv$long, y=BDL_Org_DataF.csv$lat,
         label = BDL_Org_DataF.csv$ORGANIZATION_ID, size = 2, position = "right") + 
labs(title = "MAP FOR BDL PROJECT SITES") +
labs(x = "Longitude", y = "Latitude")

Help show in legend Point numbers and names!

Thanks BattleHamster for improving my question. It is my first time to post on this site but I have learnt a lot through this site. I actually wish to display geom_Points in the ggmap annotated 1,2,3,up to 19. Then the legend to show "1" and its corresponding name "Kitale" like below:

Legend: Point Names
1=Kitale
2=Chereng'any
3=Kaplamai
4=Ndalu
5=Tongaren
.
.
.
19=Kiminini

The Points data is in a CSV file in the format below
ORGANIZATION_ID     lat         long        Name_of_Organization
1                   0.988597    35.124259   Kitale
2                   0.981345    35.219947   Chereng'any 
3                   1.019304    35.040037   Kaplamai
4                   0.840672    34.994145   Ndalu 
5                   0.78183     34.965753   Tongaren
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is one approach. I created new lat for text annotation using transform in base R. I used geom_text to add the labels you wanted. I used scale_colour_discrete to change the name of the legend.

library(ggmap)
library(ggplot2)

### Get a map
map <- get_map(location=c(lon=34.832, lat=0.852), color="color",
               source="google", maptype="terrain", zoom=9)

### Create new lat for annotation position             
mydf2 <- transform(mydf,lat2 = lat + 0.05)


ggmap(map) +
geom_point(data = mydf2, aes(x = long, y = lat, color = Name_of_Organization)) +
geom_text(data = mydf2, aes(x = long, y = lat2, label = ORGANIZATION_ID), size = 3) +
scale_colour_discrete(name  = "Name of Organization")

enter image description here

DATA

mydf <- structure(list(ORGANIZATION_ID = 1:5, lat = c(0.988597, 0.981345, 
1.019304, 0.840672, 0.78183), long = c(35.124259, 35.219947, 
35.040037, 34.994145, 34.965753), Name_of_Organization = structure(c(3L, 
1L, 2L, 4L, 5L), .Label = c("2 = Chereng'any", "3 = Kaplamai", "1 = Kitale", 
"4 = Ndalu", "5 = Tongaren"), class = "factor")), .Names = c("ORGANIZATION_ID", 
"lat", "long", "Name_of_Organization"), class = "data.frame", row.names = c(NA, 
-5L))

#  ORGANIZATION_ID      lat     long Name_of_Organization
#1               1 0.988597 35.12426           1 = Kitale
#2               2 0.981345 35.21995      2 = Chereng'any
#3               3 1.019304 35.04004         3 = Kaplamai
#4               4 0.840672 34.99415            4 = Ndalu
#5               5 0.781830 34.96575         5 = Tongaren

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...