可从CRAN获得的ggsubplot版本与R的最新版本(例如3.1.1)不兼容,并且运行ggsubplot示例会返回以下错误:
Error in layout_base(data, vars, drop = drop) :
At least one layer must contain all variables used for facetting
人们在StackOverflow问题中已经注意到了这个问题:
Plotting bar chart on map using ggplot2
和GitHub问题:
https://github.com/garrettgman/ggsubplot/issues/10
但是,GitHub问题已于本月初(2015-07-09)结束,这使我对ggsubplot将与R 3.2.1+兼容的情况感到乐观。
CRAN hosts version 0.3.2 of ggsubplot (released 2013-12-14),所以我使用
devtools
包从GitHub安装ggsubplot。我试图运行阿富汗人员伤亡示例代码,但现在遇到了另一个错误:
Error in get(x, envir = this, inherits = inh)(this, ...) :
could not find function "aesply"
我可以做些什么来做这项工作吗?
另外,如何确认我正在使用哪个版本的ggsubplot?
我尝试的代码如下:
## install.packages("devtools")
library(devtools)
dev_mode(on=T)
install_github("garrettgman/ggsubplot")
library(ggplot2)
## library(ggsubplot) - I just loaded this, right?
library(maps)
library(plyr)
# getbox by Heike Hoffman,
# https://github.com/ggobi/paper-climate/blob/master/code/maps.r
getbox <- function (map, xlim, ylim) {
# identify all regions involved
small <- subset(map, (long > xlim[1]) & (long < xlim[2]) & (lat > ylim[1]) & (lat < ylim[2]))
regions <- unique(small$region)
small <- subset(map, region %in% regions)
# now shrink all nodes back to the bounding box
small$long <- pmax(small$long, xlim[1])
small$long <- pmin(small$long, xlim[2])
small$lat <- pmax(small$lat, ylim[1])
small$lat <- pmin(small$lat, ylim[2])
# Remove slivvers
small <- ddply(small, "group", function(df) {
if (diff(range(df$long)) < 1e-6) return(NULL)
if (diff(range(df$lat)) < 1e-6) return(NULL)
df
})
small
}
## map layer
## adapted from map_nasa:
# https://github.com/ggobi/paper-climate/blob/master/code/maps.r
# assembling data
world <- map_data("world")
# building afghanistan layer
afghanistan <- getbox(world, c(60,75), c(28, 39))
map_afghanistan <- list(
geom_polygon(aes(long, lat, group = group), data = afghanistan,
fill = "white", colour = "black", inherit.aes = FALSE,
show_guide = FALSE),
scale_x_continuous("", breaks = NULL, expand = c(0.02, 0)),
scale_y_continuous("", breaks = NULL, expand = c(0.02, 0)))
## 2d bin with bar chart subplots displaying data in each region
ggplot(casualties) +
map_afghanistan +
geom_subplot2d(aes(lon, lat,
subplot = geom_bar(aes(victim, ..count.., fill = victim))),
bins = c(15,12), ref = NULL, width = rel(0.8)) +
coord_map()+
theme(legend.position = "right")
最佳答案
似乎不再维护ggsubplot
软件包。
Message on CRAN (as of 2016-12-23):
在GitHub(https://github.com/garrettgman/ggsubplot)上,最后一次提交可以追溯到2015-07-09。
在R 3.3.2下从GitHub使用devtools::install_github('garrettgman/ggsubplot')
进行安装失败,并显示一条错误消息,提示缺少eval
函数。
关于r - ggsubplot适用于R 3.2.1+吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31493592/