Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bubble map legend color in ggplot2 using viridis

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() 
like image 428
aanand Avatar asked Mar 13 '20 19:03

aanand


People also ask

What colors are in viridis?

The Viridis palette was implemented using blues and yellow sequences (and avoiding reds), in order to increase the readability for the visualizations.

Is viridis Colour blind friendly?

viridis , and its companion package viridisLite provide a series of color maps that are designed to improve graph readability for readers with common forms of color blindness and/or color vision deficiency.

Which variable will be added or updated to set the color of the map to viridis?

The viridis() function produces the viridis color scale. You can choose the other color scale options using the option parameter or the convenience functions magma() , plasma() , inferno() , cividis() , mako() , rocket () , and turbo()`.

How do I name a circle in a bubble chart in R?

To add labels on each bubble in a bubble plot in the R Language, we use the geom_text() function of the ggplot2 package. The geom_text() function adds textual annotation overlap on top of the ggplot plot. Parameter: x and y: determines the position of the label.


1 Answers

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

like image 159
Greg Avatar answered Oct 17 '22 20:10

Greg