Bubble map legend color in ggplot2 using viridis

aanand

Hello I would like the bubbles in the legend of this bubble map to be colored in the viridis colors used in the chart. I figured out how to customize the colors using a simple color (e.g. orange here) but I'm I'm not sure how to override aes with the viridis colors, or more generally any color palette.

guides(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() 
Greg

You can use the viridis function to pass colors (in this case 3 colors) from the viridis palette to override.aes like you where doing with orange:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Map shape, size and color to the same legend in ggplot2

Customize the legend of bubble map by using leaflet

Creating a Map with Custom Legend Using ggplot2 in R

Custom legend for bubble plot using ggplot

discretizing viridis ggplot color scale

ggplot2: how to assign a graduated color legend and add shaded relief background to a map

Using ggplot2 and viridis, fill histogram based on other variable

How to change colour for barplot in ggplot2 using viridis package?

How to add a custom edge color to a bubble plot using ggplot2

Color shape legend in black ggplot2

Combining linetype and color in ggplot2 legend

ggplot2 unable to color legend icons

ggplot2 - R - How to fix size of bubble in legend

ggplot2 legend entries change from posixct to integer when using scale_color_gradientn

ggplot2: Getting a color legend to appear using stat_function() in a for loop

Mixed fill color in ggplot2 legend using geom_smooth() in R

Changing the legend title breaks the legend color in ggplot2

Legend in ggplot2 - using factor labels

Changing legend using ggplot2

viridis ggplot2 color package won't install , even with dependencies = TRUE

Fixing legends and color scheme of ggplot2 bubble grid plot

Adding color and bubble size legend in R plotly

size legend for plotly bubble map/chart

Where does the term viridis (for color map) in matplotlib come from?

ggplot2: Shape, Color and Linestyle into one legend

ggplot2 - adding lines of same color but different type to legend

R ggraph/ggplot2 color legend not displayed properly

merge color and shape in legend for two variables in ggplot2

Reverse color palette with legend limits ggplot2