【问题标题】:Changing maps colours in ggplot在ggplot中更改地图颜色
【发布时间】:2019-10-27 08:32:13
【问题描述】:

所以我正在尝试更改地图上的颜色 ggplot plot

代码如下:

我熟悉 scale_fill_viridis_d() 函数以及该函数中的参数 options

但是,有没有办法让这些地图以“更好”的颜色制作,我知道您不知道“更好”的含义,但与我附上的示例有些相似?

我也可以“手动”编写颜色列表,但如果我们有 100 个或更多国家/地区怎么办?

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)

示例:

【问题讨论】:

    标签: r ggplot2 maps


    【解决方案1】:

    这里有两种选择:

    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)
    

    【讨论】:

    猜你喜欢
    • 2019-04-17
    • 2021-07-16
    • 1970-01-01
    • 2013-02-14
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多