【问题标题】:ploting ggmap with geom Points (lat_long) annotated 1 to19. Points data is in CSVfile用 geom Points (lat long) 绘制 ggmap 注释为 1 到 19。点数据在 CSVfile 中
【发布时间】:2015-02-05 04:09:23
【问题描述】:

我正在使用ggmapggplot2 使用geom_point 进行绘图。我也想在点附近添加注释文本(即 1 到 19)。

这是我的代码:

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

帮助在图例中显示点编号和名称!

感谢 BattleHamster 改进了我的问题。这是我第一次在这个网站上发帖,但我通过这个网站学到了很多东西。我实际上希望在 ggmap 中显示 geom_Points 注释为 1,2,3,最多 19。然后显示“1”的图例及其对应的名称“Kitale”如下所示:

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

【问题讨论】:

    标签: r csv ggplot2 legend ggmap


    【解决方案1】:

    这是一种方法。我在基础 R 中使用transform 为文本注释创建了新的纬度。我使用geom_text 添加了您想要的标签。我用scale_colour_discrete改了图例的名字。

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

    数据

    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
    

    【讨论】:

    • 感谢 Jazzurro 的尝试。但是,它使用颜色键打印了地图。你能知道如何用数字制作相同的图例吗? “ORGANIZATION_ID”如下所示: 图例:点名称 1=Kitale 2=Chereng'any 3=Kaplamai 4=Ndalu 5=Tongaren
    • @WachiyeEmmanuel 我更新了我的建议。如果您想更改图例元素的顺序,您也可以这样做。目前,订单是按字母顺序排列的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2022-01-17
    • 2019-06-29
    • 1970-01-01
    相关资源
    最近更新 更多