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.
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 }
0 commit comments