r - ggplot2 中使用 viridis 的气泡图图例颜色

标签 r dictionary ggplot2 viridis

您好,我希望此气泡图图例中的气泡以图表中使用的绿色颜色着色。我想出了如何使用简单的颜色(例如这里的橙色)自定义颜色,但我不确定如何使用 viridis 颜色或更一般的任何调色板覆盖 aes。

指南(大小=guide_legend(覆盖.aes = 列表(颜色=“橙色”)))

# Libraries
library(ggplot2)
library(dplyr)

# Get the world polygon and extract UK
library(maps)
UK <- map_data("world") %>% filter(region=="UK")

# Get a data frame with longitude, latitude, and size of bubbles (a bubble = a city)
data <- world.cities %>% filter(country.etc=="UK")

# virids package for the color palette
library(viridis)

# Do bubble map and use viridis as color palette 
ggplot() +
  geom_polygon(data = UK, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
  geom_point( data=data, aes(x=long, y=lat, size=pop, color=pop)) +
  scale_size_continuous(range=c(1,12)) +
  scale_color_viridis(trans="log") +
  guides(size=guide_legend(override.aes = list(color= "orange"))) + 
  theme_void() + ylim(50,59) + coord_map() 

最佳答案

您可以使用 viridisviridis 传递颜色(在本例中为 3 种颜色)的函数调色板到 override.aes就像你在用橙色做的那样:

ggplot() +
  geom_polygon(data = UK, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
  geom_point( data=data, aes(x=long, y=lat, size = pop, color = pop)) +
  scale_size_continuous(range=c(1,12)) +
  scale_color_viridis(trans="log") +
  guides(size=guide_legend(override.aes = list(color = viridis(3)))) +
  theme_void() + ylim(50,59) + coord_map() 

enter image description here

关于r - ggplot2 中使用 viridis 的气泡图图例颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60675989/

相关文章:

r - 从箱线图中提取统计信息

r - 使用 grid.arrange 绘制单个 y 轴图例

linux - 每次系统启动时如何在后台自动启动stardict

ios - 根据条件将数组合并到字典中?

r - ggplot2 stat_密度_2d 分组时无法正常工作

r - 如何使用 dplyr 或 ggplot 绘制连续参数的中值?

r - 将 nrow 和 ncol 显式提供给 `as.matrix` 不会创建维度

r - R-标签中的剪切函数,无需科学记数法,可在 ggplot2 中使用

r - ggplot2 以轴为单位指定点大小

Python 2.7 程序(带搁置字典)为特定的键和值组合返回致命的 "dbrunrecoveryerror",但不是其他