22315

plot ggmap image over raster

Question:

I'm trying to plot a map from ggmap in front of a raster. This map has a lot of white area, so I wanted to make the white area transparent so I can plot the remaining elements on top of a colorful raster, using ggplot. Here is the map:

Fulanus_bbox <- c(left = 17.250000, bottom = -3.916667, right = 36.916667, top = 8.416667) Fulanus_big <- ggmap::get_map(location = Fulanus_bbox, source = "stamen", maptype = "toner-lite") map <- ggmap::ggmap(Fulanus_big) map

All I could think of were this two solutions, which I didn't expect to work and they didn't. I have tried replacing the hex codes on the map:

map_colors <- map$layers[[2]]$geom_params$raster map_colors[map_colors == '#FFFFFF'] <- '#00FFFFFF' map$layers[[2]]$geom_params$raster <- map_colors map

I have also tried transforming the colors into a raster to see if the pattern was maintained:

maprow <- nrow(map_colors) mapcol <- ncol(map_colors) map_factors <- as.factor(map_colors) map_numeric <- as.numeric(map_factors) map_matrix <- matrix(map_numeric, nrow = maprow, ncol = mapcol) map_raster <- raster::raster(map_matrix) map_raster <- raster::setExtent(map_raster, Fulanus_bbox) raster::crs(map_raster) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 " raster::plot(map_raster)

Is that possible to do? If it is, how?

Answer1:

<blockquote>

This map has a lot of white area, so I wanted to make the white area transparent [...]

</blockquote>

This seems to work:

map <- ggmap::ggmap(Fulanus_big) r <- map$layers[[2]]$geom_params$raster r[r=="#FFFFFF"] <- NA map$layers[[2]]$geom_params$raster <- r map

<a href="https://i.stack.imgur.com/LYR02.gif" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/LYR02.gif" data-original="https://i.stack.imgur.com/LYR02.gif" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Recommend

  • Add legend to ggmap
  • How to create unique labels for multiple rows in geom_text?
  • “Time Limit Exceeded” on LeetCode's three-sum probl*m?
  • Collapsible Sankey Diagram - D3
  • Detecting application hangs with ActiveX controls in .Net
  • IllegalArgumentException: The servlets named [HelloWorlds] and [Hello] are both mapped to the url-pa
  • Java: java.util.ConcurrentModificationException
  • How to format code on aptana 3?
  • Validation fired but Red Border does not appear with User Control in Silverlight 4
  • Gstreamer1.0 : link a decodebin to videoconvert
  • MySQL: Difference between `… ADD INDEX(a); … ADD INDEX(b);` and `… ADD INDEX(a,b);`?
  • css background images not always displayed
  • Backward compatibility of Python 3.5 for external modules
  • Draw half infinite lines?
  • How to use tag-it
  • Changing references to deprecated methods C++
  • jquery code not working without breakpoint
  • Motorola barcode scanner SDK events C#
  • Enumerating Controls on a Form
  • Is there some graphical way to create my own configuration file on SonarLint?
  • Row to Column conversion in Talend
  • order post according to custom array position
  • Intel-64 and ia32 atomic operations acquire-release semantics and GCC 5+
  • Zoom in and out of jPanel
  • Firefox Extension - Monitor refresh and change of tab
  • Unable to get column index with table.getColumn method using custom table Model
  • Saving Changes After In-App Purchase Has Been Purchased
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • Oledb connection string for excel files
  • Spark fat jar to run multiple versions on YARN
  • Avoid links criss cross / overlap in d3.js using force layout
  • CSS Linear-gradient formatting issue accross different browsers
  • Master page gives error
  • Nant, Vault & Windows Integrated Authentication
  • What is the “return” in scheme?
  • Counter field in MS Access, how to generate?
  • Javascript + PHP Encryption with pidCrypt
  • Websockets service method fails during R startup
  • To display the title for the current loaction in map in iphone
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?