在地图上的地图 [英] Barplots on a Map
问题描述
我试图在地图上绘制垂直条形图。我通过网上的例子,但不知何故不能。
我的数据目前采用以下格式:
大学| Count |类别| 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) b $ b Count =样本(1:10,16,replace = T),
Category = rep(c(A,B,C,D)),
(rep(76.90020,4)),rep((10.902469,4),rep(17.921959,4),rep(18.606910,4),rep(13.202366,4)),
long = c (印度,zoom = 5)
p($)
印度< - get_map(India,zoom = 5) < - ggmap(印度)
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))))
当我运行上面的代码,我得到以下错误:
get(x,envir = t他的,inherits = inh)(this,...):
找不到函数%:::%
$ b $你还应该使用 mapproj
包。 使用以下代码:
ggmap(印度)+
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中
UPDATE 16 2014年1月1日: ggsubplot包到最新版本,现在这个解决方案也可以在R 3.0.2中使用。
UPDATE 2 oktober 2014:Below软件包作者(Garret Grolemund)关于 iss ue @jazzuro(text text mine)提到:
不幸的是, ggsubplot
不是很稳定。 ggplot2
不是
设计为可扩展或递归的,所以 ggsubplot
和 ggplot2
非常受陪审团的限制。我认为当R持续更新时,熵会自行增加
。
未来的开发计划是将ggsubplot作为一个构建的
部分实现哈德利的新包 ggvis
。这将比 ggsubplot
+ ggplot2
配对维护更多
。
我几个月都无法调试ggsubplot,但是我
会很乐意接受github上的pull request。
更新2016年12月23日: ggsubplot
-package不再被主动维护, 在CRAN上存档:
软件包'ggsubplot'已从CRAN存储库中删除。
以前可用的版本可以从存档中获得。
根据维护者
的要求存档于2016年1月11日。
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 "%:::%"
解决方案 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"))))
I got the following result:
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
UPDATE 16 januari 2014: when you update the ggsubplot package to the latest version, this solution now also works in R 3.0.2
UPDATE 2 oktober 2014: Below the answer of the package author (Garret Grolemund) about the issue mentioned by @jazzuro (text formatting mine):
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.
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.
I won't be available to debug ggsubplot for several months, but I
would be happy to accept pull requests on github.
UPDATE 23 december 2016: The ggsubplot
-package is no longer actively maintained and is archived on CRAN:
Package ‘ggsubplot’ was removed from the CRAN repository.
Formerly available versions can be obtained from the archive.
Archived on 2016-01-11 as requested by the maintainer
.
这篇关于在地图上的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!