Set opacity of background map with ggmap

With ggmapand the ggplotfollowing code ... (irreproducible, but IMHO you do not need to understand the problem).

map <- get_googlemap(center = c(lon = 10.64, lat = 50.56), maptype = "terrain", source = "google", zoom = 6, language = "de-DE", color = "bw")

ggmap(map) + 
  geom_point(data = frage_3_daten, aes(x = lng_google, y = lat_google, colour = pronunciation_id), alpha = 0.2) + 
  scale_colour_hue(name = "Aussprache", labels = c("Krampus", "Grittibänz")) +
  ggtitle("Gebäck in Form einer menschlichen Gestalt") +
  xlab("Länge") + ylab("Breite") +
  theme_srf()

I can create this beautiful map of points over German-speaking Europe.

enter image description here

Now . My only (and, I hope, simple) question: how to reduce the transparency of the background layer so that the dots become more important?

I was able to perform the following "hacking" by setting darken: ggmap(map, darken = c(0.6, "white")).

enter image description here

This almost solves my problem, but maybe there is a (hidden) option to globally reduce the opacity of the first layer of the map (or, what's more, any layer in the plot).

+4
source share
1 answer

darken, Google, ggmap.

ggmap , , . ( , ( EPSG: 4326) ggmap, .)

adjustcolor() R, ( , - , col2rgb()), , , -. - , 1 0 .

...

meuse, sp.

data(meuse)

dataframe sp, ( ), /.

coordinates(meuse) = ~x+y
proj4string(meuse) <- "+init=epsg:28992 +proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +towgs84=565.4171,50.3319,465.5524,-0.398957388243134,0.343987817378283,-1.87740163998045,4.0725 +units=m +no_defs"
meuse <- spTransform(meuse, CRS("+init=epsg:4326"))

Google, . , , , .

meuse_basemap <- get_map(location = colMeans(coordinates(meuse)), 
                         maptype = "terrain", 
                         source = "google", 
                         zoom = 13, 
                         language = "de-DE", 
                         color = "bw")

, ggmap, . , ggmap. ggmap ggmap ( , ggmap ggmap()).

meuse_basemap_attributes <- attributes(meuse_basemap)

, Google, , .

meuse_basemap_transparent <- matrix(adjustcolor(meuse_basemap, 
                                                alpha.f = 0.5), 
                                    nrow = nrow(meuse_basemap))

Assign the saved attributes to the modified matrix in order to return it to the useful one ggmap.

attributes(meuse_basemap_transparent) <- meuse_basemap_attributes

Here's the original plot:

ggmap(meuse_basemap) +
  geom_point(data = as.data.frame(meuse), 
             aes(x = x, y = y, color = dist), 
             cex = 2)

original ggmap

But more transparent!

ggmap(meuse_basemap_transparent) +
  geom_point(data = as.data.frame(meuse), 
             aes(x = x, y = y, color = dist), 
             cex = 2)

more transparent ggmap

+1
source

All Articles