我试图在地图上绘制垂直条形图.我在线浏览了一些例子,但不知怎的.
我的数据目前采用以下格式:
University| Count | Category | lat | long
Run Code Online (Sandbox Code Playgroud)
这是我正在尝试执行的代码:
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"))))
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,我收到以下错误:
Error in get(x, envir = this, inherits = inh)(this, ...) :
could not find function "%:::%"
Run Code Online (Sandbox Code Playgroud)
Jaa*_*aap 10
您还应该使用该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"))))
Run Code Online (Sandbox Code Playgroud)
我得到了以下结果:

如问题评论中所述:此解决方案适用于R 2.15.3但由于某些原因不在R 3.0.2中
更新16 januari 2014:当您将ggsubplot软件包更新到最新版本时,此解决方案现在也适用于R 3.0.2
更新2014年8月2日:在包装作者(Garret Grolemund)的回答下,关于@jazzuro提到的问题(文本格式化我的):
不幸的是,
ggsubplot不是很稳定.ggplot2不是设计成可扩展的或递归的,所以api之间ggsubplot和ggplot2apr非常被操纵.我认为随着R继续更新,熵将自我断言.未来的开发计划是将ggsubplot作为Hadley新软件包的内置部分来实现
ggvis.这比ggsubplot+ggplot2配对更容易维护.我将无法在几个月内调试ggsubplot,但我很乐意接受github上的pull请求.
更新2016年12月23日:ggsubplot-package不再主动维护并在CRAN上存档:
包'ggsubplot'已从CRAN存储库中删除.
可以从存档中获取以前可用的版本.
根据维护者的要求存档于2016-01-11.