在 geom_density2d() 图中更改默认颜色并删除图例

Changing default color and removing legend in geom_density2d() plot

在下面的代码中,如何删除第二个图例(我认为是为了 alpha 级别)并将默认的蓝色更改为红色?

suppressMessages(library(ggmap))
data(crime)
houston.map <- get_map(location = geocode("Houston"),
                       zoom = 14)
ggmap(houston.map, extent = "device", legend = "topleft") +
  stat_density2d(data = crime,
                 aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
                 size = 2,
                 bins = 10,
                 geom = "polygon") +
  labs(fill = "Density")

谢谢

添加 guides(alpha = F) 删除该图例。

suppressMessages(library(ggmap))
data(crime)
houston.map <- get_map(location = geocode("Houston"),
                       zoom = 14)
ggmap(houston.map, extent = "device", legend = "topleft") +
  stat_density2d(data = crime,
                 aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
                 size = 2,
                 bins = 10,
                 geom = "polygon") +
  guides(alpha = F) + 
  labs(fill = "Density") 

您可以使用 guides 图层通过将 alpha 指定为 FALSE 来删除第二个图例,并且您可以使用 scale_fill_gradient 更改色标完成如下:

ggmap(houston.map, extent = "device", legend = "topleft") +
  stat_density2d(data = crime,
                 aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
                 size = 2,
                 bins = 10,
                 geom = "polygon") +
  scale_fill_gradient(low = "#333333", high = "#cc0000") +
  labs(fill = "Density") +
  guides(alpha = F)