我正在尝试在 map 上绘制垂直条形图。我在网上浏览了示例,但以某种方式无法完成。
我的数据当前采用以下格式:
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 "%:::%"
最佳答案
您还应该使用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(文本格式我的名字)提到的issue的答案下面:
Unfortunately,
ggsubplot
is not very stable.ggplot2
was not designed to be extensible or recursive, so the api betweenggsubplot
andggplot2
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 theggsubplot
+ggplot2
pairing.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,它是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 .
关于r - map 上的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20465070/