如何在R语言中绘制国家地图上的位置点?

4

我已经搜索了很多关于我的具体问题,但是找不到解决方案。虽然我认为这很容易解决,但我对r语言还很陌生。

  • What i want to do: I would like to plot a map of India held in black and white. And I want to plot all the places I visited with dots and the corresponding name. I managed to do all that with the code below.
  • My problem: How can I write the code in a simpler way for plotting the visited places? I don't want to create all the variables like visit.x and visit.y. Further, I would have to write for every place the code for geom_point and geom_text. When I tried to plot more than one place at once with e.g.

    visited <- c("New Delhi", "Rishikesh")
    

然后我收到了错误信息“美学必须是长度为1或与数据相同的长度”。

  • My question: How can I just store all the visited places in one variable and plot it in one run? Like that I just need one line for geom_point and not for every place I want to plot.

    #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()
    

你可以将数据框传递给ggplot2几何对象以获得所需的行为。你只需要构建一个带有列的数据框,并将其传递给data(而不是现在的NULL),然后使用列名作为xylabel。最好通过运行ggplot2示例并更好地学习它。虽然最终对于图表来说这是一种逻辑和直接的方法,但需要进行一些试验和错误才能到达那里。请记住ggplot2几何对象:heart:数据框。 - hrbrmstr
1个回答

4

正如@hrbrmstr在上面的评论中建议的那样,ggplot2被设计为与数据框一起使用,因此最好将数据保持在一个数据框中。这样做实际上也大大简化了代码:

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

带点和标签的印度地图

根据需要进行调整。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接