ggplot2 (версия 3) несовместимость с ggmap для geom_density_2d - PullRequest
0 голосов
/ 05 июля 2018

ggplot2 версия 3 кажется несовместимой с ggmap при использовании функции geom_density2d() для добавления слоя. Следующий код возвращает ошибку (хотя работал с 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

Буду признателен за любые рекомендации по использованию geom_density2d() для добавления слоя на карту qmplot()!

(Карта ниже изображения, созданного с ggplot2 версия 2) Map created with geom_density

1 Ответ

0 голосов
/ 05 июля 2018

Ответ в комментариях через @Tung: проблема с ggmap, и решение состоит в том, чтобы использовать версию ggmap для разработки (devtools::install_github("dkahle/ggmap")

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...