6

我正在尝试在地图上绘制垂直条形图。我在网上浏览了一些例子,但不知何故无法做到。

我的数据目前是这种格式:

University| Count | Category | lat | long

这是我尝试执行的代码:

library(ggplot2)
library(ggmap)
library(ggsubplot)

df1 <- data.frame(
  University = c(rep("University1", 4), rep("University2", 4), rep("University3", 4), 
    rep("University4", 4)),
  Count = sample(1:10, 16, replace = T),
  Category = rep(c("A", "B", "C", "D")),
  lat = c(rep(10.902469, 4), rep(17.921959, 4), rep(18.606910, 4), rep(13.202366, 4)),
  long = c(rep(76.90020, 4), rep(83.42510, 4), rep(73.87501, 4), rep(77.62340, 4))
)

india <- get_map("India", zoom = 5)
p <- ggmap(india)
p + geom_subplot(data = df1, mapping=aes(x = long, y = lat, group = University,
subplot= geom_bar(aes(x = Category, y = Count, color = Category, stat = "identity"))))

当我运行上面的代码时,我收到以下错误:

Error in get(x, envir = this, inherits = inh)(this, ...) : 
   could not find function "%:::%"
4

2 回答 2

11

您还应该使用该mapproj软件包。使用以下代码:

ggmap(india) +
  geom_subplot(data = df1, aes(x = long, y = lat, group = University,
                           subplot = geom_bar(aes(x = Category, y = Count,
                                                  fill = Category, stat = "identity"))))

我得到以下结果:

在此处输入图像描述

如问题评论中所述:此解决方案适用于 R 2.15.3,但由于某种原因不适用于 R 3.0.2


2014 年 1 月 16 日更新:当您将 ggsubplot 包更新到最新版本时,此解决方案现在也适用于 R 3.0.2


2014 年 10 月 2 日更新:在包作者(Garret Grolemund)关于@jazzuro(文本格式我的)提到的问题的回答下方:

不幸的是,ggsubplot不是很稳定。ggplot2并非设计为可扩展或递归的,因此 和 之间的 apiggsubplot 非常ggplot2受陪审团操纵。我认为熵会随着 R 的不断更新而自我肯定。

未来的开发计划是将 ggsubplot 实现为 Hadley 新包的内置部分ggvis。这将比ggsubplot+ggplot2配对更易于维护。

几个月后我将无法调试 ggsubplot,但我很乐意接受 github 上的拉取请求。


2016 年 12 月 23 日更新:ggsubplot-package 不再积极维护,并存档在 CRAN 上

包 'ggsubplot' 已从 CRAN 存储库中删除。

以前可用的版本可以从存档中获得。

应维护者的要求于 2016-01-11 存档。

于 2013-12-09T10:56:53.537 回答
0

事实上,我们实际上可以使用geom_errorbar()在地图中添加一个条形图。只要我们设置的x和ygeom_errorbar()就是经纬度;ymin 和 ymax 是您的数据的统计数据。

library(tidyverse)

plant_data <- read.csv("DV.csv")
colnames(plant_data) <- c("name", "lat", "lon", "p", "t", "hmi")
head(plant_data)
# name      lat      lon    p    t         hmi
# 1 Dodonaea viscosa subsp. viscosa -17.3000 145.9667 4084 24.1 8.905719509
# 2 Dodonaea viscosa subsp. viscosa -18.0833 146.0000 2562 24.0 13.18851823
# 3 Dodonaea viscosa subsp. viscosa -18.0833 146.0000 2562 24.0 13.18851823
# 4 Dodonaea viscosa subsp. viscosa -18.0833 146.0000 2562 24.0 13.18851823
# 5 Dodonaea viscosa subsp. viscosa -18.0833 146.0000 2562 24.0 13.18851823
# 6 Dodonaea viscosa subsp. viscosa -18.0833 146.0000 2562 24.0 13.18851823

情节部分

aus <- map_data('world', region = "(Australia)|(Indonesia)|(Papua New Guinea)")
ggplot() +
  geom_polygon(data = aus, aes(x = long, y = lat, group = group), 
               fill = "gray80") + 
  geom_point(data = plant_data, aes(x = lon, y = lat), color = '#00CC00') +
  coord_map(xlim = c(110, 160), ylim = c(-45, -5)) +
  ggtitle("植物分布") + 
  theme(plot.title = element_text(hjust = 0.5)) + 
  geom_errorbar(data = plant_data[c(50, 99), ], 
                aes(x = lon,ymin=lat-3,ymax=lat+1), 
                color='blue', size = 3, width=0)

这部分非常聪明:

geom_errorbar(data = plant_data[c(50, 99), ], 
                aes(x = lon,ymin=lat-3,ymax=lat+1), 
                color='blue', size = 3, width=0)

太棒了!!

在此处输入图像描述

于 2020-03-01T15:14:37.850 回答