r - map 上的条形图

标签 r ggplot2 ggmap

我正在尝试在 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 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.



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/

相关文章:

r - 如何在R中导入数据并创建散点图?

r - 如何将箱线图稍微向左或向右移动其原始位置?

r - ggplot2中的geom_density和base R中的density的区别

r - 重叠标签ggmap

R,类似于ggmap的免费 map

r - 在ggplot2中绘图后如何更改数据子集的颜色/形状/大小

jquery - R Shiny - 在shinyjs::toggleState中使用JQuery选择器禁用动态创建的按钮

r - ggplot - 标记为计算增加 (%)

r - 叠加了geom_map的ggmap

r - 根据列名是否在字符串向量中为多个列分配值