3

I have plotted a map of France with customized colors with map():

map(database="france", fill=TRUE, col=color)

Results:

map of france with the colors

(don't mind the two errors)

and then I want to put this map with a projection of Lambert using ggplot2 package :

head(fortify(carte_france))
ggplot(map('france'), aes(long, lat, group = group)) +
  geom_polygon(col = 1) + 
  coord_map(projection = "lambert",
            parameters = c(lat0 = 41.366005 , lat1 = 51.097523))

However it gives me this :

map of france with geom_polygon()

I know that the dark colours comes from the arg fill from geom_polygon(), but is there a way to tell the function geom_polygon() to not use the argument fill or to keep the colors I have put before?

Vector of colours:

color <- c("yellowgreen", "yellowgreen", "yellowgreen", "yellowgreen", 
"gold1", "sienna3", "yellowgreen", "yellowgreen", "sienna3", 
"gold1", "gold1", "gold1", "sienna3", "sienna3", "gold1", "dodgerblue2", 
"dodgerblue2", "dodgerblue2", "gold1", "dodgerblue2", "sienna3", 
"dodgerblue2", "burlywood2", "dodgerblue2", "palevioletred4", 
"dodgerblue2", "palevioletred4", "dodgerblue2", "palevioletred4", 
"palevioletred4", "gold1", "palevioletred4", "gold1", "darkorchid4", 
"gold1", "darkorchid4", "palevioletred4", "green4", "cyan4", 
"gold1", "palevioletred4", "burlywood2", "green4", "green4", 
"cyan4", "darkorchid4", "green4", "burlywood2", "palevioletred4", 
"burlywood2", "green4", "green4", "palevioletred4", "palevioletred4", 
"palevioletred4", "green4", "burlywood2", "olivedrab2", "green4", 
"olivedrab2", "darkorchid4", "darkorchid4", "cyan4", "darkorchid4", 
"cyan4", "cyan4", "olivedrab2", "olivedrab2", "olivedrab2", "cyan4", 
"cyan4", "olivedrab2", "cyan4", "olivedrab2", "olivedrab2", "cyan4", 
"cyan4", "olivedrab2", "olivedrab2", "olivedrab2", "cyan4", "cyan4", 
"cyan4", "cyan4", "orange2", "orangered1", "orangered1", "orangered1", 
"olivedrab2", "orange2", "olivedrab2", "orangered1", "orange2", 
"orangered1", "orange2", "orange2", "orangered1", "orangered1", 
"orangered1", "orange2", "orangered1", "orange2", "orangered1", 
"olivedrab2", "orangered1", "orangered1", "orangered1", "orangered1", 
"orange2", "orange2", "orange2", "chartreuse3", "orangered1", 
"chartreuse3")

2 Answers 2

4

The trick to get the same colors in the ggplot is that you have to make a new vector repeating your color vector to reach the same length as the number of rows of the mapFrance object.

Try the reproducible and commented step-by-step code:

# Load libraries
library("maps")
library("ggplot2")

# Colors
color <- c("yellowgreen", "yellowgreen", "yellowgreen", "yellowgreen", 
           "gold1", "sienna3", "yellowgreen", "yellowgreen", "sienna3", 
           "gold1", "gold1", "gold1", "sienna3", "sienna3", "gold1", "dodgerblue2", 
           "dodgerblue2", "dodgerblue2", "gold1", "dodgerblue2", "sienna3", 
           "dodgerblue2", "burlywood2", "dodgerblue2", "palevioletred4", 
           "dodgerblue2", "palevioletred4", "dodgerblue2", "palevioletred4", 
           "palevioletred4", "gold1", "palevioletred4", "gold1", "darkorchid4", 
           "gold1", "darkorchid4", "palevioletred4", "green4", "cyan4", 
           "gold1", "palevioletred4", "burlywood2", "green4", "green4", 
           "cyan4", "darkorchid4", "green4", "burlywood2", "palevioletred4", 
           "burlywood2", "green4", "green4", "palevioletred4", "palevioletred4", 
           "palevioletred4", "green4", "burlywood2", "olivedrab2", "green4", 
           "olivedrab2", "darkorchid4", "darkorchid4", "cyan4", "darkorchid4", 
           "cyan4", "cyan4", "olivedrab2", "olivedrab2", "olivedrab2", "cyan4", 
           "cyan4", "olivedrab2", "cyan4", "olivedrab2", "olivedrab2", "cyan4", 
           "cyan4", "olivedrab2", "olivedrab2", "olivedrab2", "cyan4", "cyan4", 
           "cyan4", "cyan4", "orange2", "orangered1", "orangered1", "orangered1", 
           "olivedrab2", "orange2", "olivedrab2", "orangered1", "orange2", 
           "orangered1", "orange2", "orange2", "orangered1", "orangered1", 
           "orangered1", "orange2", "orangered1", "orange2", "orangered1", 
           "olivedrab2", "orangered1", "orangered1", "orangered1", "orangered1", 
           "orange2", "orange2", "orange2", "chartreuse3", "orangered1", 
           "chartreuse3")

# Make map
mapFrance <- map(database = "france", fill = TRUE, col = color)
  • simple plot map

firstMap

# Make gg object
gg <- ggplot(data = mapFrance)

# Make auxilliary data.frame
values <- data.frame("group" = factor(1:length(mapFrance$names)), "color" = color)

# Add color data to gg object by merging
gg$data <- merge(gg$data, values, by = c("group"))

# Plot gg object
gg + 
  geom_polygon(aes(x = long, y = lat, group = group),  fill = gg$data$color, colour = 1) + 
  coord_map(projection = "lambert", parameters = c(lat0 = 41.366005 , lat1 = 51.097523))
  • ggplot2 map

ggplotMap

3

The easiest way is to use scale_fill_identity in ggplot2:

library("ggplot2")
library("maps")
library("dplyr")

# create data frame and merge predefined colors in vector `color`
map_fr <- map_data("france") %>%
          left_join(data.frame(group = seq_along(color), color = color),
                    by = "group")

p <- ggplot(data = map_fr) +
     geom_polygon(aes(x = long, y = lat, group = group, fill = color),
                  colour = "black") +
     coord_map(projection = "lambert",
               parameters = c(lat0 = 41.366005 , lat1 = 51.097523)) +
     scale_fill_identity()

print(p)

plot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.