所以我试图更改地图ggplot
上的颜色
代码在下面:
我熟悉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)
示例:
答案 0 :(得分: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)