library(sf)
## Warning: package 'sf' was built under R version 3.4.1
## Linking to GEOS 3.6.1, GDAL 2.2.0, proj.4 4.9.3
library(raster)
## Warning: package 'raster' was built under R version 3.4.1
## Loading required package: sp
## Warning: package 'sp' was built under R version 3.4.1
library(ggplot2)

## we have to set a CRS, it really doesn't matter which
## everything we are using is Cartesian in "degrees longitude,latitude"
r <- raster(matrix(1:12, 3), crs = "+proj=longlat +ellps=WGS84")
names(r) <- "value"

## in this case we aren't transforming the raster to a new projection
## https://github.com/ropensci/plotdap/blob/master/R/plotdap.R#L310

## plotdap does this internally
## https://github.com/ropensci/plotdap/blob/master/R/plotdap.R#L319
p_sp <- rasterToPolygons(r)
p_sf <- st_as_sf(p_sp)

## fill mapping is crafted by plotdap as "value"
## https://github.com/ropensci/plotdap/blob/master/R/plotdap.R#L321
## https://github.com/ropensci/plotdap/blob/master/R/plotdap.R#L328
ggplot(p_sf, aes(fill = value)) + geom_sf(colour=NA)

ggplot(p_sf, aes(fill = value)) + geom_sf()

r <- raster(volcano, crs = "+proj=longlat +ellps=WGS84")
names(r) <- "value"
p_sp <- rasterToPolygons(r)
p_sf <- st_as_sf(p_sp)
ggplot(p_sf, aes(fill = value)) + geom_sf(colour=NA)

ggplot(p_sf, aes(fill = value)) + geom_sf()

devtools::session_info()
## Session info -------------------------------------------------------------
##  setting  value                                      
##  version  R version 3.4.0 Patched (2017-04-28 r72639)
##  system   x86_64, mingw32                            
##  ui       RTerm                                      
##  language (EN)                                       
##  collate  English_Australia.1252                     
##  tz       Australia/Hobart                           
##  date     2017-07-27
## Packages -----------------------------------------------------------------
##  package    * version    date       source                            
##  backports    1.1.0      2017-05-22 CRAN (R 3.4.0)                    
##  base       * 3.4.0      2017-05-02 local                             
##  colorspace   1.3-2      2016-12-14 CRAN (R 3.4.1)                    
##  compiler     3.4.0      2017-05-02 local                             
##  datasets   * 3.4.0      2017-05-02 local                             
##  DBI          0.7        2017-06-18 CRAN (R 3.4.1)                    
##  devtools     1.13.2     2017-06-02 CRAN (R 3.4.1)                    
##  digest       0.6.12     2017-01-27 CRAN (R 3.4.1)                    
##  evaluate     0.10.1     2017-06-24 CRAN (R 3.4.1)                    
##  ggplot2    * 2.2.1.9000 2017-07-27 Github (tidyverse/ggplot2@b2b58f1)
##  graphics   * 3.4.0      2017-05-02 local                             
##  grDevices  * 3.4.0      2017-05-02 local                             
##  grid         3.4.0      2017-05-02 local                             
##  gtable       0.2.0      2016-02-26 CRAN (R 3.4.1)                    
##  htmltools    0.3.6      2017-04-28 CRAN (R 3.4.1)                    
##  knitr        1.16       2017-05-18 CRAN (R 3.4.1)                    
##  labeling     0.3        2014-08-23 CRAN (R 3.4.0)                    
##  lattice      0.20-35    2017-03-25 CRAN (R 3.4.0)                    
##  lazyeval     0.2.0      2016-06-12 CRAN (R 3.4.1)                    
##  magrittr     1.5        2014-11-22 CRAN (R 3.4.1)                    
##  memoise      1.1.0      2017-04-21 CRAN (R 3.4.1)                    
##  methods    * 3.4.0      2017-05-02 local                             
##  munsell      0.4.3      2016-02-13 CRAN (R 3.4.1)                    
##  plyr         1.8.4      2016-06-08 CRAN (R 3.4.1)                    
##  raster     * 2.5-8      2016-06-02 CRAN (R 3.4.1)                    
##  Rcpp         0.12.12    2017-07-15 CRAN (R 3.4.1)                    
##  rgdal        1.2-8      2017-07-01 CRAN (R 3.4.1)                    
##  rlang        0.1.1      2017-05-18 CRAN (R 3.4.1)                    
##  rmarkdown    1.6        2017-06-15 CRAN (R 3.4.1)                    
##  rprojroot    1.2        2017-01-16 CRAN (R 3.4.1)                    
##  scales       0.4.1.9002 2017-07-27 Github (hadley/scales@6db7b6f)    
##  sf         * 0.5-2      2017-07-12 CRAN (R 3.4.1)                    
##  sp         * 1.2-5      2017-06-29 CRAN (R 3.4.1)                    
##  stats      * 3.4.0      2017-05-02 local                             
##  stringi      1.1.5      2017-04-07 CRAN (R 3.4.0)                    
##  stringr      1.2.0      2017-02-18 CRAN (R 3.4.1)                    
##  tibble       1.3.3      2017-05-28 CRAN (R 3.4.1)                    
##  tools        3.4.0      2017-05-02 local                             
##  udunits2     0.13       2016-11-17 CRAN (R 3.4.0)                    
##  units        0.4-5      2017-06-15 CRAN (R 3.4.1)                    
##  utils      * 3.4.0      2017-05-02 local                             
##  withr        1.0.2      2016-06-20 CRAN (R 3.4.0)