geom_point 图例颜色具有反转的形状叠加层而不是实际形状

geom_point legend colors have inverted shape overlays instead of actual shape

您可以在下图中看到 'colour' 图例未正确运行。我期待一个彩色圆圈(例如颜色一应该是绿​​色圆圈),但是背景是绿色的,你可以看到黑色覆盖的似乎是某种类型的边界框。

这是我的代码:

library(ggmap)

c_map <- c(left = -86.817417, bottom = 36.133247, right = -86.793141, top = 36.153650)
campus <- get_stamenmap(c_map, zoom = 15, maptype = "toner-lines")

some_data <- read.csv('https://gist.githubusercontent.com/pconwell/085c1413e418adaa7c1e203c9680a0f8/raw/c7a3e5f7aa900de6bc2bcccd5dc5d9b8f7e31b81/some_data.csv')

ggmap(campus, darken = c(0.33, "white"), extent = "device") + 

  stat_density2d(data = some_data,
                 aes(x = longitude,
                     y = latitude,
                     fill = stat(nlevel),
                     alpha = stat(nlevel)),
                 size = 2,
                 bins = 12,
                 geom = "polygon"
  ) +

  scale_fill_gradientn(guide = "colorbar",
                       colours = c("#adddd1", "#3e98af", "#375980"),
                       breaks=c(0.2, 0.9), 
                       labels=c("Least","Most"), 
                       name = "Heat Map"
  ) +

  geom_point(data = some_data,
             aes(x = longitude,
                 y = latitude,
                 color = color,
                 shape = shape),
             alpha = .75,
             size = 4

  )  +

  scale_alpha_continuous(range = c(0.33, 0.66), 
                         guide = FALSE
  )

如何让图例正确显示颜色?图标在地图中显示正确,但在图例中显示不正确。

背景和边界框来自多边形颜色的图例。这是一个更简单的可重现示例:

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.3

set.seed(42)
df <- as.data.frame(MASS::mvrnorm(100, c(0, 0), matrix(c(1, .6), 2, 2)))

ggplot(df, aes(V1, V2)) +
  stat_density_2d(geom = "polygon") +
  geom_point(aes(colour = cut_number(V1, 5)))

您可以通过显式设置多边形 geom 的颜色来解决此问题:

ggplot(df, aes(V1, V2)) +
  stat_density_2d(geom = "polygon", colour = NA) +
  geom_point(aes(colour = cut_number(V1, 5)))

但是,即使关联的图层没有任何映射到颜色的内容,多边形图例也会出现,这让我感到有些惊讶。也许这是一个错误?

更新: 我无法用其他 geom 或统计数据重现此行为,所以我进一步研究了密度 2d:奇怪的是,它似乎是出现多边形是因为 StatDensity2d 具有默认的颜色审美值:

StatDensity2d$default_aes
#> Aesthetic mapping: 
#> * `colour` -> "#3366FF"
#> * `size`   -> 0.5
StatDensity2d$default_aes <- aes()

ggplot(df, aes(V1, V2)) +
  stat_density_2d(geom = "polygon") +
  geom_point(aes(colour = cut_number(V1, 5)))

reprex package (v0.3.0)

于 2019-07-04 创建

问题的简化版本(删除了 ggmap 和 Google API 键的要求)表明它主要是关于多余的参数。稍后将代码逐层分开,因为这些层构建了图例中包含的内容。 stat_density2d 中的 size 参数对设置为多边形的 geom 没有任何作用;它只对轮廓有意义,例如:

library(ggplot2)

some_data <- read.csv('https://gist.githubusercontent.com/pconwell/085c1413e418adaa7c1e203c9680a0f8/raw/c7a3e5f7aa900de6bc2bcccd5dc5d9b8f7e31b81/some_data.csv')

ggplot(some_data, aes(x = longitude, y = latitude)) +
  geom_point(aes(color = color, shape = shape), alpha = 0.75, size = 4) +
  stat_density2d(aes(alpha = stat(nlevel)),
                 size = 2) + 
  guides(alpha = "none",
         color = guide_legend(override.aes = list(fill = NA)))

您可以在密度和图例中看到 size = 2 的效果,它在颜色图例中显示为粗线。

因为这对于多边形来说不是必需的,所以删除大小参数或将其设置为 0。您还有一个来自密度层的填充元素,使图例上的那些变暗区域。通过在图例中将填充显式设置为 NA 来删除它。

ggplot(some_data, aes(x = longitude, y = latitude)) +
  geom_point(aes(color = color, shape = shape), alpha = 0.75, size = 4) +
  stat_density2d(aes(fill = stat(nlevel), alpha = stat(nlevel)),
                 size = 0,
                 geom = "polygon") + 
  guides(alpha = "none",
         color = guide_legend(override.aes = list(fill = NA)))