Skip to content

Commit b8077f5

Browse files
authored
Merge pull request #8 from ropensci/gganimate
Gganimate
2 parents 3294a6d + d6175ae commit b8077f5

40 files changed

Lines changed: 219 additions & 2964 deletions

DESCRIPTION

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: plotdap
22
Title: Easily Visualize Data from 'ERDDAP' Servers via the 'rerddap' Package
3-
Version: 0.0.4
4-
Date: 2019-10-14
3+
Version: 0.0.8
4+
Date: 2020-10-23
55
Authors@R: c(
66
person("Carson", "Sievert", role = "aut"),
77
person("Roy", "Mendelssohn", role = c("aut", "ctb", "cre"), email = "roy.mendelssohn@noaa.gov"))
@@ -10,10 +10,12 @@ License: MIT + file LICENSE
1010
URL: https://github.com/ropensci/plotdap
1111
BugReports: https://github.com/ropensci/plotdap/issues
1212
Depends:
13-
R (>= 3.6.0)
13+
R (>= 4.0.0)
1414
Imports:
15+
cmocean,
1516
dplyr,
1617
gganimate,
18+
ggnewscale (>= 0.4.1),
1719
ggplot2 (>= 3.1.0),
1820
lazyeval,
1921
lubridate,
@@ -24,7 +26,8 @@ Imports:
2426
rgeos,
2527
scales,
2628
sf,
27-
tidyr
29+
tidyr,
30+
viridis
2831
Suggests:
2932
Cairo,
3033
knitr,
@@ -33,7 +36,7 @@ Suggests:
3336
rgdal,
3437
rmarkdown,
3538
testthat
36-
RoxygenNote: 6.1.1
39+
RoxygenNote: 7.1.1
3740
LazyData: true
3841
Encoding: UTF-8
3942
VignetteBuilder: knitr

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ importFrom(ggplot2,coord_sf)
1717
importFrom(ggplot2,geom_sf)
1818
importFrom(ggplot2,ggplot)
1919
importFrom(ggplot2,ggtitle)
20+
importFrom(ggplot2,guide_colorbar)
2021
importFrom(ggplot2,guides)
2122
importFrom(ggplot2,labs)
2223
importFrom(ggplot2,scale_colour_gradientn)

NEWS.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
# plotdap 0.0.8
2+
3+
fixed animation bug in 'add_griddap()' not finding the correct number
4+
of frames
5+
6+
# plotdap 0.0.7
7+
8+
* fixed problem with \dontrun example not finding rerddap::griddap()
9+
10+
* fixed problem in vignette to URL link to R docs
11+
12+
# plotdap 0.0.6
13+
* fixed problem when no time coordinate in dataset
14+
* 'add_tabledap()' overlays on gridded maps now work for 'ggplot2'
15+
16+
# plotdap 0.0.5
17+
* fixed problem with animation in 'add_griddap()'
18+
* fixed bad URL for sardine example
19+
* changed to use colors in package 'cmocean' instead of in 'rerddap'
20+
121
# plotdap 0.0.4
222
* fixed changed SODA datasetid in vignette
323
* fixed Namespace and Import problems

R/add_griddap.R

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
#' @param plot a \link{plotdap} object.
66
#' @param grid a \link{griddap} object.
77
#' @param var a formula defining a variable, or function of variables to visualize.
8-
#' @param fill either a character string of length 1 matching a name in \\link[rerddap]{colors}
9-
#' or a vector of color codes. This defines the colorscale used to encode values
8+
#' @param fill either a character string of length 1 matching a name in the
9+
#' package \code{cmocean} or a vector of color codes.
10+
#' This defines the colorscale used to encode values
1011
#' of \code{var}.
1112
#' @param maxpixels integer > 0. Maximum number of cells to use for the plot.
1213
#' If maxpixels < ncell(x), sampleRegular is used before plotting.
@@ -34,12 +35,12 @@
3435
#' # actual datasets in data folder to meet execution timings
3536
#'
3637
#'\donttest{
37-
#' murSST <- griddap(
38-
#' 'jplMURSST41', latitude = c(35, 40), longitude = c(-125, -1205),
38+
#' murSST <- rerddap::griddap(
39+
#' 'jplMURSST41', latitude = c(35, 40), longitude = c(-125, -120.5),
3940
#' time = c('last', 'last'), fields = 'analysed_sst'
4041
#' )
4142
#'
42-
#' QMwind <- griddap(
43+
#' QMwind <- rerddap::griddap(
4344
#' 'erdQMwindmday', time = c('2016-11-16', '2017-01-16'),
4445
#' latitude = c(30, 50), longitude = c(210, 240),
4546
#' fields = 'x_wind'
@@ -133,8 +134,15 @@ add_griddap <- function(plot, grid, var, fill = "viridis",
133134
)
134135
if (inherits(r, "RasterBrick")) {
135136
for (i in seq_len(raster::nlayers(r))) {
136-
r[[i]] <- raster::resample(r[[i]], rnew, method = 'bilinear')
137+
#r[[i]] <- raster::resample(r[[i]], rnew, method = 'bilinear')
138+
junk <- raster::resample(r[[i]], rnew, method = 'bilinear')
139+
if (i == 1) {
140+
temp <- junk
141+
} else {
142+
temp <- raster::addLayer(temp, junk)
143+
}
137144
}
145+
r <- raster::brick(temp)
138146
} else {
139147
r <- raster::resample(r, rnew, method = 'bilinear')
140148
}
@@ -152,18 +160,30 @@ add_griddap <- function(plot, grid, var, fill = "viridis",
152160
}
153161

154162
# color scale
155-
cols <- if (length(fill) == 1) rerddap::colors[[fill]] else fill
163+
# cols <- if (length(fill) == 1) rerddap::colors[[fill]] else fill
164+
if (length(fill) == 1) {
165+
if (fill == 'viridis') {
166+
cols <- viridis::viridis(256)
167+
} else {
168+
cols <- cmocean::cmocean(fill)(256)
169+
170+
}
171+
} else {
172+
cols <- fill
173+
}
156174

157175
if (is_ggplotdap(plot)) {
158176
# TODO: not the most efficient approach, but it will have to do for now
159177
# https://twitter.com/hadleywickham/status/841763265344487424
160178
s <- sf::st_as_sf(raster::rasterToPolygons(r))
161179
vars <- setdiff(names(s), "geometry")
162-
sg <- sf::st_as_sf(tidyr::gather_(s, "variable", "value", vars))
180+
var_name <- lazyeval::f_text(var)
181+
variable_name <- "variable"
182+
sg <- sf::st_as_sf(tidyr::gather(s, {{variable_name}},{{var_name}}, vars))
163183
if (animate) {
164184
try_gganimate()
165185
plot$animate <- TRUE
166-
plot$nper <- length(sg)
186+
plot$nper <- length(s) - 1
167187
plot$ggplot <- plot$ggplot +
168188
gganimate::transition_manual(variable, cumulative = cumulative) +
169189
ggplot2::labs(title = "{current_frame}")
@@ -173,10 +193,10 @@ add_griddap <- function(plot, grid, var, fill = "viridis",
173193
add_ggplot(
174194
plot,
175195
geom_sf(data = sg,
176-
mapping = aes_string(fill = "value", colour = "value"), ...),
177-
scale_fill_gradientn(name = lazyeval::f_text(var), colors = cols),
178-
scale_colour_gradientn(colors = cols),
179-
guides(colour = FALSE)
196+
mapping = ggplot2::aes_string(fill = var_name, colour = var_name), ...),
197+
scale_fill_gradientn(name = var_name, colors = cols),
198+
scale_colour_gradientn(colors = cols)
199+
#ggplot2::guides(colour = "none")
180200
)
181201
)
182202
}

R/add_tabledap.R

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#' @param plot a \link{plotdap} object.
66
#' @param table a \link{tabledap} object.
77
#' @param var a formula defining a variable, or function of variables to visualize.
8-
#' @param color either a character string of length 1 matching a name in \link[rerddap]{colors}
8+
#' @param color either a character string of length 1 matching a name in \code{cmocean}
99
#' or a vector of color codes. This defines the colorscale used to encode values
1010
#' of \code{var}.
1111
#' @param size the size of the symbol.
@@ -29,11 +29,11 @@
2929
#' # code given to extract the data
3030
#'
3131
#'\donttest{
32-
#' sardines <- tabledap(
32+
#' sardines <- rerddap::tabledap(
3333
#' 'FRDCPSTrawlLHHaulCatch',
3434
#' fields = c('latitude', 'longitude', 'time', 'scientific_name', 'subsample_count'),
3535
#' 'time>=2010-01-01', 'time<=2012-01-01',
36-
#' scientific_name="Sardinops sagax"
36+
#' 'scientific_name="Sardinops sagax"'
3737
#' )
3838
#'}
3939
#'
@@ -77,7 +77,17 @@ add_tabledap <- function(plot, table, var, color = c("#132B43", "#56B1F7"),
7777
}
7878

7979
# color scale
80-
cols <- if (length(color) == 1) rerddap::colors[[color]] else color
80+
# cols <- if (length(color) == 1) rerddap::colors[[color]] else color
81+
82+
if (length(color) == 1) {
83+
if (color == 'viridis') {
84+
cols = viridis::viridis(256)
85+
} else{
86+
cols <- cmocean::cmocean(color)(256)
87+
}
88+
} else {
89+
cols <- color
90+
}
8191

8292
if (is_ggplotdap(plot)) {
8393

@@ -89,15 +99,27 @@ add_tabledap <- function(plot, table, var, color = c("#132B43", "#56B1F7"),
8999
gganimate::transition_manual(factor(time), cumulative = cumulative) +
90100
ggplot2::labs(title = "{current_frame}")
91101
}
92-
93-
return(
94-
add_ggplot(
95-
plot,
96-
geom_sf(data = table, mapping = aes_(colour = var),
97-
size = size, pch = shape, ...),
98-
scale_colour_gradientn(name = lazyeval::f_text(var), colours = cols)
102+
if (length(plot$ggplot$layers) == 1) {
103+
return(
104+
add_ggplot(
105+
plot,
106+
geom_sf(data = table, mapping = aes_(colour = var),
107+
size = size, pch = shape, ...),
108+
scale_colour_gradientn(name = lazyeval::f_text(var), colours = cols)
109+
)
99110
)
100-
)
111+
} else {
112+
#plot$ggplot <- plot$ggplot + ggnewscale::new_scale_colour() + ggnewscale::new_scale_fill()
113+
return(
114+
add_ggplot(
115+
plot,
116+
ggnewscale::new_scale_colour(),
117+
geom_sf(data = table, mapping = aes_(colour = var),
118+
size = size, pch = shape, ...),
119+
scale_colour_gradientn(name = lazyeval::f_text(var), colours = cols))
120+
#scale_colour_gradientn(colours = cols)
121+
)
122+
}
101123
}
102124

103125

R/imports.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#' @importFrom scales rescale col_numeric
88
#' @importFrom lazyeval is_formula f_text f_eval
99
#' @importFrom sf st_as_sf st_transform st_crs st_bbox st_graticule
10-
#' @importFrom ggplot2 ggplot geom_sf coord_sf theme_bw scale_colour_gradientn
11-
#' scale_fill_gradientn ggtitle aes_string aes_ guides labs
10+
#' @importFrom ggplot2 aes_ aes_string ggplot geom_sf guides guide_colorbar coord_sf theme_bw scale_colour_gradientn
11+
#' scale_fill_gradientn ggtitle labs
1212
#' @importFrom raster raster nlayers calc ncell nrow ncol crs extent resample
1313
#' projectRaster rasterToPolygons values plot brick setExtent isLonLat
1414
NULL

R/plotdap.R

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ print.ggplotdap <- function(x, ...) {
104104
gg <- x$ggplot
105105
dots <- list(...)
106106
if (isTRUE(dots$landmask)) {
107-
gg$layers <- rev(gg$layers)
107+
gg$layers[1:2] <- rev(gg$layers[1:2])
108108
layer_data <- lapply(gg$layers, function(y) y$layer_data(gg$data))
109109
bbs <- lapply(layer_data[1], sf::st_bbox)
110110
x$ggplot <- gg
@@ -257,7 +257,11 @@ get_raster <- function(grid, var) {
257257
r <- if (length(times) > 1) {
258258
# ensure values appear in the right order...
259259
# TODO: how to detect a south -> north ordering?
260-
d <- dplyr::arrange(grid$data, time, desc(lat), lon)
260+
if ("lat" %in% names(grid$data)) {
261+
d <- dplyr::arrange(grid$data, time, desc(lat), lon)
262+
} else {
263+
d <- dplyr::arrange(grid$data, time, desc(latitude), longitude)
264+
}
261265
b <- raster::brick(
262266
nl = length(times),
263267
nrows = length(lats),
@@ -266,7 +270,11 @@ get_raster <- function(grid, var) {
266270
raster::values(b) <- lazyeval::f_eval(var, d)
267271
raster::setExtent(b, ext)
268272
} else {
269-
d <- dplyr::arrange(grid$data, desc(lat), lon)
273+
if ("lat" %in% names(grid$data)) {
274+
d <- dplyr::arrange(grid$data, desc(lat), lon)
275+
} else {
276+
d <- dplyr::arrange(grid$data, desc(latitude), longitude)
277+
}
270278
raster::raster(
271279
nrows = length(lats),
272280
ncols = length(lons),
@@ -279,7 +287,7 @@ get_raster <- function(grid, var) {
279287
}
280288

281289

282-
utils::globalVariables(c("time", "desc", "lat", "lon"))
290+
utils::globalVariables(c("time", "desc", "lat", "lon", "latitude", "longitude"))
283291

284292
latlon_is_valid <- function(x) {
285293
if (is_raster(x)) {

R/utils.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ try_require <- function(package, fun) {
3232

3333
try_gganimate <- function() {
3434
if (system.file(package = "gganimate") != "") {
35-
if (utils::packageVersion("gganimate") > "0.1") {
35+
if (utils::packageVersion("gganimate") > "1.0.0") {
3636
return(TRUE)
3737
}
3838
}
@@ -87,6 +87,11 @@ format_table <- function(table, .info) {
8787
table
8888
}
8989

90+
# sf is not properly importing rgeos.
91+
# I need to import it, but causes note if not
92+
# referenced. Here is a dummy function.
93+
dummy <- function() rgeos::getScale()
94+
# dummy2 <- function() rgdal::checkCRSArgs("+proj=laea +y_0=0 +lon_0=155 +lat_0=-90 +ellps=WGS84 +no_defs")
9095

9196
# This is likely WRONG, SAD!
9297
# rescale_lims <- function(x, .info = info(attr(x, "datasetid"))) {

cran-comments.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
## This is a minor update
1+
## This is a minor bugfix update
22

3-
- fixed URL change in vignette
4-
- fixed Import and Namespace problems reported on CRAN checks
5-
- On Windows, previous version built with --no-vignettes please continue that
6-
if Vignette build takes too long
73

84

95
## Test environments
10-
* local OS X install, R 3.6.1
11-
* Fedora Linux on RHub)
6+
* local OS X install, R 4.0.3
7+
* local Fedora, R 4.0.2
128
* win-builder (devel and release)
139

1410
## R CMD check results
1511

1612
### OS X
1713

18-
Duration: 3m 53s
14+
Duration: 4m 23.5s
1915

20-
0 errors | 0 warnings | 0 notes
16+
0 errors | 0 warnings | 0 notes
2117

2218
### Winbuilder release
19+
2320
OK
2421

2522
### Winbuilder devel
2623

2724
OK
2825

29-
### RHUb Windows Server 2008 R2 SP1, R-devel, 32/64 bit
3026

31-
OK
32-
### RHub Fedora
33-
OK
27+
### Local Fedora
28+
29+
Duration: 3m 08s
30+
31+
0 errors ✓ | 0 warnings ✓ | 0 notes ✓
32+

0 commit comments

Comments
 (0)