garrettgman / ggsubplot Goto Github PK
View Code? Open in Web Editor NEWEmbed subplots in ggplot2 graphics in R
Embed subplots in ggplot2 graphics in R
Hi there, I'm having a problem with ggsubplot where the ref boxes seem to obscure/block out the glyphs. Not sure what the issue is, but help would be much appreciated! Thanks!
head(test)
City variable value Longitude Latitude
1 Arlington Full1 1.01126868 -97.08282 32.76843
2 Augusta Full1 0.79747398 -82.03618 33.54812
3 Austin Full1 2.70904885 -97.71979 30.24379
4 Bald Head Island Full1 -0.43922099 -77.99813 33.87832
5 Brownsville Full1 0.02593881 -97.48888 25.89420
6 Cedar Creek Full1 1.71790076 -94.69412 34.78443
var = ggplot(test)+ geom_subplot(ref=ref_box(fill="white",alpha=0.2), aes(Longitude, Latitude, group=City, subplot = geom_bar(aes(x = value, y = variable, fill=variable), stat="identity")))
Using v0.3.1
Basic subplot attempt:
ggplot(nasa) + geom_subplot(aes(long,lat,group=id,subplot=geom_point(aes(surftemp,temperature),size=1/4) ) )+ coord_map()
Results in
Error in get(x, envir = this, inherits = inh)(this, ...) :
could not find function "%:::%"
Works in v0.2.0
Using R 3.01 and 3.0.2
I am trying to run the example from the geom_subplot
help page
ggplot(casualties, aes(lon, lat), size = 0) +
geom_subplot2d(aes(lon, lat,
subplot = geom_bar(aes(victim, ..count.., fill = victim))),
breaks=list(c(60,65,70, 75), c(28, 33, 38)), ref = NULL, width = rel(0.8))
And I get the following error:
Error in layout_base(data, vars, drop = drop) :
At least one layer must contain all variables used for facetting
This same error occurs with with every ggsubplot example I've tried to run so far. I've tried with both the CRAN and github version of ggsubplot.
> R.version
_
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 1.0
year 2014
month 04
day 10
svn rev 65387
language R
version.string R version 3.1.0 (2014-04-10)
nickname Spring Dance
> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggsubplot_0.3.1 ggplot2_0.9.3.1 devtools_1.5
loaded via a namespace (and not attached):
[1] colorspace_1.2-4 digest_0.6.4 evaluate_0.5.5 grid_3.1.0 gtable_0.1.2 httr_0.3 labeling_0.2 MASS_7.3-32
[9] memoise_0.2.1 munsell_0.4.2 parallel_3.1.0 plyr_1.8.1 proto_0.3-10 Rcpp_0.11.1 RCurl_1.95-4.1 reshape2_1.4
[17] scales_0.2.4 stringr_0.6.2 tools_3.1.0 whisker_0.3-2
This is related to this question on Stackoverflow.
I can easily produce a plotting command that displays stacked bar graphs on a map, derived from the examples shown here:
library(ggplot2)
library(ggsubplot)
library(ggmap)
qmap('afghanistan', zoom = 6, maptype = 'toner', extent = 'panel') +
geom_subplot2d(
aes(lon, lat,
subplot = geom_bar(aes(x=factor(1), y=..count.., fill = victim))
), bins = c(15,12), ref = NULL, width = rel(0.8),
data = casualties
)
Is there a way to show the stacked bar plots as pie charts? Usually, I would add + coord_polar(theta="y")
, but adding it to the subplot produces a non-numeric argument to binary operator
error and adding it to the whole plot transforms the map into polar coordinates:
# this example produces a "non-numeric argument to binary operator" error
qmap('afghanistan', zoom = 6, maptype = 'toner', extent = 'panel') +
geom_subplot2d(
aes(lon, lat,
subplot = geom_bar(aes(x=factor(1), y=..count.., fill = victim)) +
coord_polar(theta="y")
), bins = c(15,12), ref = NULL, width = rel(0.8),
data = casualties
)
# this example transforms the whole thing into polar coordinates
qmap('afghanistan', zoom = 6, maptype = 'toner', extent = 'panel') +
geom_subplot2d(
aes(lon, lat,
subplot = geom_bar(aes(x=factor(1), y=..count.., fill = victim))
), bins = c(15,12), ref = NULL, width = rel(0.8),
data = casualties
) + coord_polar(theta="y")
Been looking for this functionality, thanks!
I'm not positive this is the sort of issue to post here, but I would appreciate any help. When I run the example "ggplot(casualties) + geom_coxcomb(aes(angle = month, fill = month))" I get the following: -could not find function "zero_wrap"-.
Here's my session info:
R version 2.15.1 (2012-06-22)
Platform: x86_64-pc-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RColorBrewer_1.0-5 ggsubplot_0.2.0 ggplot2_0.9.2.1 MASS_7.3-22 plyr_1.7.1 mapproj_1.1-8.3 maps_2.2-6
loaded via a namespace (and not attached):
[1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2 grid_2.15.1 gtable_0.1.1 labeling_0.1 memoise_0.1 munsell_0.4
[9] proto_0.3-9.2 reshape2_1.2.1 scales_0.2.2 stringr_0.6.1 tools_2.15.1
Thank you for this great package
During installation of the package I encountered following error:
installing source package 'ggsubplot' ...
R
data
moving datasets to lazyload DB
preparing package for lazy loading
Error in eval(expr, envir, enclos) : could not find function "eval"
Error : unable to load R code in package 'ggsubplot'
ERROR: lazy loading failed for package 'ggsubplot'
removing '(...)/Documents/R/win-library/3.2/ggsubplot'
Recently I found similar topic on github concerning different package:
jrnold/ggthemes#36
It is possible to fix this issue for ggsubplot?
I noticed that this package has been archived on CRAN. Is there any hope/intention on bringing it back?
I guess this may be more of a request than an issue.
I would like to integrate ggsubplot
functionality within my package pitchRx
. Here is a minimal example of something I would like to achieve.
library(ggsubplot)
library(pitchRx)
data(pitches)
pitches$px <- as.numeric(pitches$px)
pitches$pz <- as.numeric(pitches$pz)
#works
ggplot(data=pitches)+geom_subplot2d(aes(x=px, y=pz,
subplot = geom_bar(aes(x=des, fill = des))))
#doesn't work
ggplot(data=pitches)+geom_subplot2d(aes(x=px, y=pz,
subplot = geom_bar(aes(x=des, fill = des))))+facet_wrap(~stand)
Is there a theoretical reason why facetting isn't supported in ggsubplot
? Or is it just a difficult problem? By the way, this package is quite awesome and I think it has a lot of potential!
Based on my own tests and this StackOverflow answer, ggsubplot
does not work with R v3.2.0
and/or ggplot2 v1.0.1
. It would be great if ggsubplot
could be removed from CRAN, or the depends
and imports
directives were updated to reflect the fact that older installations of R and/or ggplot2 are needed for the package to work correctly.
The ref_*
help files need an example of how to use them.
I saw some compatibility issues were closed, so I tried to run the Afghanistan casualties example code. I'm getting an error:
Error in get(x, envir = this, inherits = inh)(this, ...) :
could not find function "aesply"
Here's the code I'm trying to run:
library(devtools)
dev_mode(on=T)
install_github("garrettgman/ggsubplot")
library(ggplot2)
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")
Amazing work!
The examples use map_afghanistan and map_americas, which I can not find or reproduce.
I am guessing something like this is needed:
americas <- data.frame(map("americas", plot=FALSE)[c("x","y")])
map_americas <- qplot(x, y, data=americas, geom="path"))
Here's an example
n <- 10
df <- data.frame(y = rnorm(4*n),
group = rep(letters[1:4], each = n),
long = rep(c(-120, -80, -110, -70), each = n),
lat = rep(c(45, 30), each = 2*n))
ggplot(df, aes(long, lat)) +
geom_subplot(aes(long, lat, group = group,
subplot = geom_histogram(aes(y, ..count..))))
# now try to add reference boxes
ggplot(df, aes(long, lat)) +
geom_subplot(aes(long, lat, group = group,
subplot = geom_histogram(aes(y, ..count..))), ref = ref_box())
# scale limits are way too big!
Here's the complete code for the figures in the Revolutions blog post. More code and examples can also be had from the updated package vignette:
https://github.com/garrettgman/ggsubplot/blob/master/inst/doc/manual.pdf
library(maps)
library(ggplot2)
library(RColorBrewer)
library(ggsubplot)
## You will also need to have the mapproj package installed for coord_map()
#############################
### Map layers ###
#############################
# 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")
states <- map_data("state")
states$group <- max(world$group) + states$group
both <- rbind(world, states)
americas <- getbox(both, xlim = c(-115, -55), ylim = c(-21.1, 36.6))
nasa$fahrenheit <- nasa$surftemp * 9 / 5 - 459.67
# building layers
map_americas <- list(
geom_polygon(aes(long, lat, group = group), data = americas, fill = "grey70",
colour = "grey60", 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)))
afghanistan <- getbox(world, c(60,75), c(28, 39))
map_afghan <- list(
geom_polygon(aes(long, lat, group = group), data = afghanistan,
fill = "grey80", colour = "white", 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)))
###################################################
### Figure 1: Afghanistan casualties ###
###################################################
ggplot(casualties) +
map_afghan +
annotate("text", label = "Kabul", x = 69.1667, y = 34.453,
color = "white", size = 3.5) +
annotate("text", label = "Kandahar", x = 65.7053, y = 31.4978,
color = "white", size = 3.4) +
geom_subplot2d(aes(lon, lat,
subplot = geom_bar(aes(victim, ..count.., fill = victim),
color = rev(brewer.pal(5,"Blues"))[1], size = 1/4)),
bins = c(15,12), ref = NULL, width = rel(0.8), height = rel(1)) +
ggtitle("Afghan War Diary - Casualty counts by type and location") +
coord_map() +
scale_fill_manual(values = rev(brewer.pal(5,"Blues"))[c(1,4,2,3)])
###################################################
### Figure 2: Overplotted diamonds ###
###################################################
ggplot(diamonds) +
geom_subplot2d(aes(log10(carat), log10(price),
subplot = geom_bar(aes(color, fill = color), position = "dodge")),
bins = c(10, 14), y_scale = free, height.adjust = 0.8,
width.adjust = 0.8, ref = ref_box(aes(color = length(color)))) +
scale_color_gradient("Total\ncount", low = "grey70", high = "black") +
ggtitle("Diamonds, carat vs. price")
###################################################
### Figure 3: Glyphmap ###
###################################################
ggplot(nasa) +
map_americas +
geom_subplot(aes(long, lat, group = id,
subplot = geom_star(aes(r = surftemp, angle = date,
fill = mean(surftemp)), r.zero = FALSE))) +
scale_fill_gradient("Average\nTemperature (F) ",
breaks = c(55,60,65,70,75,80), guide = guide_colorbar()) +
ggtitle("Surface temperature fluctuations 1995 - 2001") +
coord_map()
###################################################
### Figure 3: Interactions ###
###################################################
ggplot(nasa) +
geom_subplot(aes(min(surftemp), max(surftemp), group = id,
subplot = geom_star(aes(r = surftemp, angle = date,
fill = mean(surftemp)), r.zero = FALSE)), width = 2, height = 2) +
scale_fill_gradient("Average\nTemperature (F) ",
breaks = c(55,60,65,70,75,80), guide = guide_colorbar()) +
scale_x_continuous("Minimum temperature") +
scale_y_continuous("Maximum temperature") +
ggtitle("Temperature extremes and seasonality\nby region 1995 - 2001") +
coord_fixed()
Garrett
There are several errors while using subplot2d:
ggplot(casualties, aes(lon, lat), size = 0) +geom_subplot2d(aes(lon, lat, subplot = geom_bar(aes(victim, ..count.., fill = victim))), ref = NULL, width = rel(0.8))
Using binwidth 1.36
Error in UseMethod("fullseq") :
no applicable method for 'fullseq' applied to an object of class "c('double', 'numeric')"ggplot(casualties, aes(lon, lat), size = 0) +geom_subplot2d(aes(lon, lat, subplot = geom_bar(aes(victim, ..count.., fill = victim))), binwidth=0.1, ref = NULL, width = rel(0.8))
Error in UseMethod("fullseq") :
no applicable method for 'fullseq' applied to an object of class "c('double', 'numeric')"ggplot(casualties, aes(lon, lat), size = 0) +geom_subplot2d(aes(lon, lat, subplot = geom_bar(aes(victim, ..count.., fill = victim))), bins=c(15,12), ref = NULL, width = rel(0.8))
Using binwidth 0.905
Error in UseMethod("fullseq") :
no applicable method for 'fullseq' applied to an object of class "c('double', 'numeric')"ggplot(casualties, aes(lon, lat), size = 0) +geom_subplot2d(aes(lon, lat, subplot = geom_bar(aes(victim, ..count.., fill = victim))), breaks=c(-0.5,0,0.5), ref = NULL, width = rel(0.8))
Error: Zero breaks in scale for x/xmin/xmax/xend/xintercept
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
Hi,
using ggsubplot 0.3.2 with ggplot2 1.0.0 on R 3.1.2 does not work.
E.g. if I use your code to fix the missing map_americas and then run the geom_subplot example I just get
Error in layout_base(data, vars, drop = drop) :
At least one layer must contain all variables used for facetting
It's the same error for my own attempts to create a map.
Thx
Stefan
library(ggplot2)
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")
states <- map_data("state")
states$group <- max(world$group) + states$group
both <- rbind(world, states)
americas <- getbox(both, xlim = c(-115, -55), ylim = c(-21.1, 36.6))
# building americas layer
map_americas <- list(
geom_polygon(aes(long, lat, group = group), data = americas, fill = "grey70",
colour = "grey60", 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)))
ggplot(nasa) +
map_americas +
geom_subplot(aes(long, lat, group = id,
subplot = geom_point(aes(surftemp, temperature), size = 1/4))) +
coord_map()
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.