作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经为我的特定问题搜索了很多,但找不到解决方案。虽然,我认为它很容易解决,但我是 r 的新手。
visit.x
这样的所有变量和 visit.y
.此外,我必须为每个地方编写 geom_point
的代码。和 geom_text
.当我试图一次绘制多个地方时,例如visited <- c("New Delhi", "Rishikesh")
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
关于r - 如何在r中的国家 map 上绘制位置点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41092026/
我是一名优秀的程序员,十分优秀!