r - 使用viridis在ggplot2中的气泡图图例颜色
问题描述
您好,我希望此气泡图图例中的气泡以图表中使用的绿色颜色着色。我想出了如何使用简单的颜色(例如此处的橙色)来自定义颜色,但我不确定如何使用 viridis 颜色或更一般地使用任何调色板来覆盖 aes。
指南(size=guide_legend(override.aes = list(color= "orange")))
# 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()
解决方案
您可以使用该viridis
函数从viridis
调色板传递颜色(在本例中为 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()
推荐阅读
- sql-server - Windows cmd 从 SQL Server 存储过程回显到文件
- ios - Swift - 通过引用获取约束或循环通过属性的约束?
- php - 如何在 .php 之后获取值
- postgresql - 分离 pgAdmin 和 cmd psql 数据库
- python-3.6 - python中类的行为
- java - SWT.graphics.color 的 Alpha 值不起作用
- scala - 光滑的导入钻石运算符 <>
- apache-camel - 如何在 Camel JMS 选择器选项中使用加号?
- r - 具有多列和权重列的桑基图 - 使用 NetworkD3 包
- image - macos Mojave beta - 图像缩略图问题