Ggplot2 (version 3) incompatibility with ggmap for geom_density_2d

ggplot2 version 3 seems to have an incompatibility with ggmap when using the geom_density2d() function to add a layer. The following code returns an error (though worked with ggplot2 version 2):

# Load libraries
library(ggplot2) # version 3
library(ggmap) # needs to be *dev* version, that's the issue
# Create a data frame
df <- data.frame(
  long = rnorm(50, -122.32, .2), 
  lat = rnorm(50, 47.6, .2) 
)

# Use qmplot to create a base layer of map tiles
base_plot <- qmplot(
  data = df, 
  x = long, # data feature for longitude
  y = lat, # data feature for latitude
  geom = "blank", # don't display data points (yet)
  maptype = "toner-background", # map tiles to query
  darken = .7, # darken the map tiles
  legend = "topleft" # location of legend on page
)

# Show the map in RStudio
base_plot

# Use ggplot to create a 2d density map (without tiles -- works fine)
ggplot(df, aes(x = long, y = lat)) + 
  geom_density2d() + 
  stat_density_2d(
    aes(x = long, y = lat, fill = stat(level)), # in v2, fill = ..level..
    # Use the computed density to set the fill
    alpha = .3,
    geom="polygon" # Set the alpha (transparency)
  )

# Add 2d density plot on map tiles -- returns an error
base_plot + 
  geom_density2d() + 
  stat_density_2d(
    aes(x = long, y = lat, fill = stat(level)), # in v2, fill = ..level..
    # Use the computed density to set the fill
    alpha = .3,
    geom="polygon" # Set the alpha (transparency)
  )

# Error in width_cm(guide$barwidth %||% theme$legend.key.width) : 
 # Unknown input

Any guidance on how to use geom_density2d() to add a layer to a qmplot() map would be appreciated!

(Map below of the image created with ggplot2 version 2)

The issue was with ggmap, and is solved by using the development version of ggmap (i.e., devtools::install_github("dkahle/ggmap").

2 Likes

qmplot() is from ggmap, correct? I think this might be an issue with the new version of ggplot2 and ggmap.

ggmap

Version: 2.6.1

Newly broken

  • checking examples ... ERROR
    ... Warning:panel.marginis deprecated. Please usepanel.spacing` property instead
> # legend example
> df$class <- factor(sample(0:1, .5*n^2,  replace = TRUE))
> p <- qplot(x, y, data = df, geom = "tile", fill = class)
> p
> p + theme_nothing()
> Warning: `panel.margin` is deprecated. Please use `panel.spacing` property instead
> p + theme_nothing(legend = TRUE)
Warning: `panel.margin` is deprecated. Please use `panel.spacing` property instead
Theme element panel.border missing
Theme element axis.line.x.bottom missing
Theme element axis.ticks.x.bottom missing
Theme element axis.line.y.left missing
Theme element axis.ticks.y.left missing
Error in UseMethod("element_grob") : 
  no applicable method for 'element_grob' applied to an object of class "NULL"
Calls: <Anonymous> ... guide_gengrob.legend -> ggname -> grobName -> element_grob
Execution halted
```
1 Like

Thanks much, @mara! Someone on SO noted that the issue was just needing to use the development version of ggmap. I can take this post down if it isn't helpful to keep around.

Glad you got there! This is why full reprexes are so helpful for troubleshooting — we have the CRAN packages that have errors, etc. from revdepchecks, so it's a super helpful way to see where the errors are coming from.

1 Like

Leave it up — I'm sure you won't be the only one to run into this problem! :slightly_smiling_face:

1 Like

Thanks for keeping this post up folks, I ran into a similar issue re-running code with ggmap and the new ggplot2 v3.0 -- this post saved me from a Friday afternoon :rabbit2: hole .

2 Likes