Petr
Petr

Reputation: 1847

Changing maps colours in ggplot

So I am trying to change colours on my maps ggplot plot

the code is down below:

I am familiar with a scale_fill_viridis_d() function and also argument options in that function

However is there a way how to make those maps in "nicer" colours, I know that you don't know what "nicer" means but somewhat similar to example I attached?

Also I can write colours list "manually" but what if we have 100 or more countries?

library(rgeos)
library(rgdal)
library(dplyr)
require(maps)
require(viridis)
library(ggplot2)
library(spdep)

some.eu.countries <- c(
  "Portugal", "Spain", "France", "Switzerland", "Germany",
  "Austria", "Belgium", "UK", "Netherlands",
  "Denmark")
# Retrievethe map data

map_data("europe")
some.eu.maps <- map_data("world", region = some.eu.countries) 

g = ggplot(some.eu.maps, aes(x = long, y = lat, group = group)) +
  geom_polygon(fill="lightgray", colour = "white") 
g

# Region base centroid
region.lab.data <- some.eu.maps %>%
  group_by(region) %>%
  summarise(long = mean(long), lat = mean(lat)) 

# Now plotting countries
g = ggplot(some.eu.maps, aes(x = long, y = lat)) +
  geom_polygon(aes( group = group, fill = region), colour = "black", size = 1.2) + 
  scale_fill_viridis_d()
print(g)

Example: Image

Upvotes: 0

Views: 6973

Answers (1)

mnist
mnist

Reputation: 6954

Here you have two option:

library(RColorBrewer)
library(ggplot2)
library(dplyr)

# get number of groups
groups <- some.eu.maps %>% 
  pull(region) %>% 
  unique() %>% 
  length()

# continuous color scale from red over green to orange
colors1 <- colorRampPalette(c("red", "green", "orange"))(groups) 
# discrete colour scale with red, grren, and orange
colors2 <- rep(c("red", "green", "orange"), length.out = groups)

g <- ggplot(some.eu.maps, aes(x = long, y = lat)) +
  geom_polygon(aes( group = group, fill = region), 
               colour = "black", size = 1.2) 

g + 
  scale_fill_manual(values = colors1)
g + 
  scale_fill_manual(values = colors2)

Upvotes: 1

Related Questions