简体   繁体   English

使用geom_polygon()保留自定义颜色

[英]Keep customized colors with geom_polygon()

I have plotted a map of France with customized colors with map() : 我用map()用自定义颜色绘制了一张法国map()

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

Results : 结果:

法国与颜色的地图

(don't mind the two errors) (不要介意这两个错误)

and then I want to put this map with a projection of Lambert using ggplot2 package : 然后我想使用ggplot2包将这张地图与Lambert的投影放在一起:

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 : 但是它给了我这个:

法国与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 arg fill or to keep the colors I have put before ? 我知道深色来自geom_polygon()的arg fill ,但是有没有办法告诉函数geom_polygon()不要使用arg fill或保持我之前放置的颜色?

Vector of colours, color : 颜色向量, 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")

First, you should use map_data that create a dataset in the good format for ggplot with maps from library map. 首先,您应该使用map_data以良好的格式为ggplot创建一个数据集,该数据集带有来自库映射的映射。 In which case, you will not have these white dots on your map. 在这种情况下,您的地图上将不会有这些白点。
As you defined the order of your colors based on the order of polygons in the map function, I created a table for left joining with map_data . 当您根据map函数中多边形的顺序定义颜色顺序时,我创建了一个表,用于与map_data左连接。

# Get polygon order from map
map_france <- map(database="france", fill=TRUE)

# Create a dataframe with department and corresponding colors
names_col <- data.frame(
  region = map_france$names,
  col_dpt = 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")
)

Then you can join this dataframe to data created by map_data and call the colour vector with ggplot. 然后,您可以将此数据map_data连接到由map_data创建的数据,并使用ggplot调用颜色向量。

# Get the map in the correct format for ggplot
# And join the correspondance of colors
carte_france <- map_data('france') %>%
  left_join(names_col)

# Plot
ggplot(carte_france, aes(long, lat, group = group)) +
  geom_polygon(col = 1, fill = carte_france$col_dpt) + 
  coord_map(projection = "lambert", 
            parameters = c(lat0 = 41.366005 , lat1 = 51.097523))

For your information, on the website of the IGN ( http://professionnels.ign.fr/geofla ), you can download map of French departements, with the information on the region. 有关您的信息,请在IGN的网站( http://professionnels.ign.fr/geofla )上下载法国部门的地图以及该地区的信息。 In this case, it would be easier to create a vector of colors based on the region name... 在这种情况下,根据区域名称创建颜色向量会更容易...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM