地图上的条形图 [英] Barplots on a Map

查看:23
本文介绍了地图上的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在地图上绘制垂直条形图.我在网上浏览了示例,但不知何故无法.

I am trying to plot vertical bar plots over a map. I went through examples online but somehow not being able to.

我的数据目前采用以下格式:

My data is currently in this format:

University| Count | Category | lat | long

这是我尝试执行的代码:

Here is the code that I am trying execute:

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"))))

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

When I run the above code, I get the following error:

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

推荐答案

您还应该使用 mapproj 包.使用以下代码:

You should also use the mapproj package. With the following code:

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

As noted in the comments of the question: this solution works in R 2.15.3 but for some reason not in R 3.0.2

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

UPDATE 16 januari 2014: when you update the ggsubplot package to the latest version, this solution now also works in R 3.0.2

2014 年 10 月 2 日更新:在包作者 (Garret Grolemund) 关于 问题(我的文本格式):

UPDATE 2 oktober 2014: Below the answer of the package author (Garret Grolemund) about the issue mentioned by @jazzuro (text formatting mine):

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

Unfortunately, ggsubplot is not very stable. ggplot2 was not designed to be extensible or recursive, so the api between ggsubplot and ggplot2 is very jury rigged. I think entropy will assert itself as R continues to update.

未来的发展计划是将ggsubplot作为一个内置在 Hadley 的新包 ggvis 的一部分.这将更多比 ggsubplot + ggplot2 配对更易于维护.

The future plan for development is to implement ggsubplot as a built in part of Hadley's new package ggvis. This will be much more maintainable than the ggsubplot + ggplot2 pairing.

我将有几个月无法调试 ggsubplot,但我很乐意在 github 上接受拉取请求.

I won't be available to debug ggsubplot for several months, but I would be happy to accept pull requests on github.

<小时>

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


UPDATE 23 december 2016: The ggsubplot-package is no longer actively maintained and is archived on CRAN:

包ggsubplot"已从 CRAN 存储库中删除.

Package ‘ggsubplot’ was removed from the CRAN repository.

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

Formerly available versions can be obtained from the archive.

应维护者的要求于 2016-01-11 归档.

Archived on 2016-01-11 as requested by the maintainer .

这篇关于地图上的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆