library(sf)
## Linking to GEOS 3.5.1, GDAL 2.2.1, proj.4 4.9.3
library(raster)
## Loading required package: sp
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.1 (2017-06-30)
## system x86_64, linux-gnu
## ui X11
## language (EN)
## collate en_AU.UTF-8
## 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.1 2017-07-07 local
## colorspace 1.3-2 2016-12-14 CRAN (R 3.4.0)
## compiler 3.4.1 2017-07-07 local
## datasets * 3.4.1 2017-07-07 local
## DBI 0.7 2017-06-18 CRAN (R 3.4.0)
## devtools 1.13.2 2017-06-02 CRAN (R 3.4.0)
## digest 0.6.12 2017-01-27 CRAN (R 3.4.0)
## evaluate 0.10.1 2017-06-24 CRAN (R 3.4.0)
## ggplot2 * 2.2.1.9000 2017-07-24 Github (tidyverse/ggplot2@331977e)
## graphics * 3.4.1 2017-07-07 local
## grDevices * 3.4.1 2017-07-07 local
## grid 3.4.1 2017-07-07 local
## gtable 0.2.0 2016-02-26 CRAN (R 3.4.0)
## htmltools 0.3.6 2017-04-28 cran (@0.3.6)
## knitr 1.16.5 2017-07-20 Github (yihui/knitr@546e059)
## 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.0)
## magrittr 1.5 2014-11-22 CRAN (R 3.4.0)
## memoise 1.1.0 2017-06-20 Github (hadley/memoise@d63ae9c)
## methods * 3.4.1 2017-07-07 local
## munsell 0.4.3 2016-02-13 CRAN (R 3.4.0)
## plyr 1.8.4 2016-06-08 CRAN (R 3.4.0)
## raster * 2.5-8 2016-06-02 CRAN (R 3.4.0)
## 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.0)
## rprojroot 1.2 2017-01-16 CRAN (R 3.4.0)
## scales 0.4.1.9002 2017-07-23 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.0)
## stats * 3.4.1 2017-07-07 local
## stringi 1.1.5 2017-04-07 CRAN (R 3.4.0)
## stringr 1.2.0 2017-02-18 CRAN (R 3.4.0)
## tibble 1.3.3 2017-05-28 CRAN (R 3.4.0)
## tools 3.4.1 2017-07-07 local
## udunits2 0.13 2016-11-17 CRAN (R 3.4.0)
## units 0.4-5 2017-06-15 CRAN (R 3.4.0)
## utils * 3.4.1 2017-07-07 local
## withr 1.0.2 2016-06-20 CRAN (R 3.4.0)