如何在r?

时间:2016-12-11 22:50:07

标签: r plot ggplot2 geospatial

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

  • 我想做什么:我想画一张黑白相间的印度地图。我想用点和相应的名字绘制我访问过的所有地方。我设法用下面的代码完成所有这些。
  • 我的问题:如何以更简单的方式编写代码来绘制访问过的地点?我不想创建所有变量,例如visit.xvisit.y。此外,我必须为每个地方写geom_pointgeom_text的代码。当我试图用例如一次多个地方作图时。

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

然后我收到错误信息&#34;美学必须是长度1或与数据相同&#34;。

  • 我的问题:如何将所有访问过的地点存储在一个变量中并将其绘制在一次运行中?就像我只需要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()
    

1 个答案:

答案 0 :(得分:4)

正如@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

随意调整。