Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

r - Barplots on a Map

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 "%:::%"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

enter image description here

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 .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...