gpt4 book ai didi

r - 如何在r中的国家 map 上绘制位置点?

转载 作者:行者123 更新时间:2023-12-04 10:48:26 24 4
gpt4 key购买 nike

我已经为我的特定问题搜索了很多,但找不到解决方案。虽然,我认为它很容易解决,但我是 r 的新手。

  • 我想做什么:我想绘制一张黑白的印度 map 。我想用点和相应的名称绘制我访问过的所有地方。我设法用下面的代码完成了所有这些。
  • 我的问题:如何以更简单的方式编写代码来绘制访问过的地方?我不想创建像 visit.x 这样的所有变量和 visit.y .此外,我必须为每个地方编写 geom_point 的代码。和 geom_text .当我试图一次绘制多个地方时,例如
    visited <- c("New Delhi", "Rishikesh")

  • 然后我收到错误消息“美学必须是长度为 1 或与数据相同”。
  • 我的问题:如何将所有访问过的地方存储在一个变量中并在一次运行中绘制它?像这样我只需要一行 geom_point而不是我想绘制的每个地方。
    #Load required packages
    library(maps)
    library(ggmap)
    library(ggplot2)

    #Dataframe with country relevant information
    map <- fortify(map(fill = T, plot = F, region = "India"))

    #Places I want to mark on the map
    visited <- c("New Delhi")
    visited2 <- c("Rishikesh")
    visited3 <- c("Agra")

    #Extracting long / lat of the places
    visit.x <- geocode(visited)$lon
    visit.y <- geocode(visited)$lat
    visit.x2 <- geocode(visited2)$lon
    visit.y2 <- geocode(visited2)$lat
    visit.x3 <- geocode(visited3)$lon
    visit.y3 <- geocode(visited3)$lat

    #Defining font
    font = c("Courier")
    font.size = 3

    #Specifing the look of the map with ggplot2
    map_india <- ggplot(data = map, aes(x = long, y = lat, group = group)) +
    geom_polygon(fill = "white") +
    geom_path(colour = "black") +
    theme(panel.background = element_rect(fill = "#000000"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.ticks = element_blank(),
    axis.text.y = element_blank(),
    axis.text.x = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank())

    #Plotting the places I want on the map with labels
    map_india <- map_india +
    geom_point(aes(x = visit.x, y = visit.y)) +
    geom_text(data = NULL, x = visit.x - 1, y = visit.y + 0.2, label = "New Delhi", size = font.size, family = font) +
    geom_point(aes(x = visit.x2, y = visit.y2)) +
    geom_text(data = NULL, x = visit.x2 - 1, y = visit.y2 + 0.2, label = "Rishikesh", size = font.size, family = font) +
    geom_point(aes(x = visit.x3, y = visit.y3)) +
    geom_text(data = NULL, x = visit.x3, y = visit.y3 + 0.5, label = "Agra", size = font.size, family = font) +
    coord_fixed(0.8)

    #Creating pdf
    pdf("India.pdf", height = 11.69, width = 16.53)
    print(map_india)
    dev.off()
  • 最佳答案

    正如@hrbrmstr 在上面的评论中所建议的那样,ggplot2 旨在与 data.frames 一起使用,因此最好将您的数据始终保持在一个中。这样做实际上也大大简化了代码:

    library(tidyverse)    # for ggplot2 and `%>%`
    library(ggmap)
    library(ggrepel) # for geom_text_repel, though adjust overlaps manually if you prefer

    cities <- data_frame(city = c("New Delhi", "Rishikesh", "Agra")) %>% # start data.frame
    mutate_geocode(city) # use ggmap function to add lon/lat columns

    cities
    #> # A tibble: 3 × 3
    #> city lon lat
    #> <chr> <dbl> <dbl>
    #> 1 New Delhi 77.20902 28.61394
    #> 2 Rishikesh 78.26761 30.08693
    #> 3 Agra 78.00807 27.17667

    box <- geocode('India', output = 'more') # get lon/lat for bounding box

    box
    #> lon lat type loctype address north south east
    #> 1 78.96288 20.59368 country approximate india 35.5087 6.753516 97.39556
    #> west country
    #> 1 68.16289 India

    get_stamenmap(bbox = c(left = box$west, # get background tiles, set bounding box
    bottom = box$south,
    right = box$east,
    top = box$north),
    maptype = 'toner-background', # set map style
    zoom = 5) %>%
    ggmap(extent = 'device') + # note switch from %>% to + as moves to ggplot
    geom_point(aes(x = lon, y = lat), data = cities) + # add points
    geom_text_repel(aes(x = lon, y = lat, label = city), data = cities) # add labels

    india map with points and labels

    随心所欲地调整。

    关于r - 如何在r中的国家 map 上绘制位置点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41092026/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com