ggplot2(版本3)与geg_density_2d的ggmap不兼容

时间:2021-10-13 07:00:15

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):

当使用geom_density2d()函数添加图层时,ggplot2版本3似乎与ggmap不兼容。以下代码返回错误(尽管使用ggplot2版本2):

# 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!

有关如何使用geom_density2d()向qmplot()地图添加图层的任何指导将不胜感激!

(Map below of the image created with ggplot2 version 2) ggplot2(版本3)与geg_density_2d的ggmap不兼容

(使用ggplot2版本2创建的图像下方的地图)

1 个解决方案

#1


0  

Answered in comments via @Tung: the issue is with ggmap, and the solution is to use the development version of ggmap (devtools::install_github("dkahle/ggmap")

通过@Tung在评论中回答:问题在于ggmap,解决方案是使用ggmap的开发版本(devtools :: install_github(“dkahle / ggmap”))

#1


0  

Answered in comments via @Tung: the issue is with ggmap, and the solution is to use the development version of ggmap (devtools::install_github("dkahle/ggmap")

通过@Tung在评论中回答:问题在于ggmap,解决方案是使用ggmap的开发版本(devtools :: install_github(“dkahle / ggmap”))