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

时间:2017-04-06 11:37:41

标签: r ggplot2 maps polygon

我已使用map()

绘制了法国地图,其中包含自定义颜色
map(database="france", fill=TRUE, col=color)

结果:

map of france with the colors

(不要介意这两个错误)

然后我想使用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))

然而它给了我这个:

map of france with geom_polygon()

我知道深色来自fill的arg geom_polygon(),但有一种方法可以告诉函数geom_polygon()不要使用arg fill或者保持我以前的颜色?

颜色矢量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")

1 个答案:

答案 0 :(得分:1)

首先,您应该使用map_data以ggplot的良好格式创建数据集,其中包含来自库地图的地图。在这种情况下,您的地图上不会有这些白点 当您根据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")
)

然后,您可以将此数据框连接到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))

有关您的信息,请在IGN网站(http://professionnels.ign.fr/geofla)上下载法国部门地图,并提供有关该地区的信息。在这种情况下,根据区域名称...

创建颜色矢量会更容易