shapefile 的 ggplot 自定义类别图例
ggplot custom category legend for shapefile
library(raster)
library(ggplot2)
library(sf)
fr.shp.admin <- getData('GADM', country='FRA', level=0)
fr.shp <- getData('GADM', country='FRA', level=1)
fr.shp.df <- data.frame(fr.shp)
set.seed(123)
dat <- data.frame(
NAME_1 = rep(fr.shp.df$NAME_1, 2),
season = rep(c('a','b'), each = 13),
value = c(sample(0:6, size = 8, replace = T), 0.1, 0.6, 100, 500,1250,
sample(0:6, size = 8, replace = T), 0.3, 0.7,50, 400,1700))
fr.shp.sf <- st_as_sf(fr.shp)
fr.shp.admin.sf <- st_as_sf(fr.shp.admin)
temp.shp <- merge(fr.shp.sf, dat, by = 'NAME_1')
ggplot() +
geom_sf(data = temp.shp, aes(fill = value), colour = NA) +
geom_sf(data = fr.shp.admin.sf, fill = NA, col = 'black') +
facet_wrap(~season)
我想绘制这张地图,而不是连续的图例,我有类别,我可以为这些类别定义颜色。例如
(0-1] # => 0 & < 1
(1-2] # >= 1 & < 2
(2-3] # >= 2 & < 3
(3-4] # >= 3 & < 4
(4-5] # >= 4 & < 5
> 5 # >= 5
我想这样做是为了给每个类别涂上不同的颜色。我怎样才能在 ggplot
中做到这一点
我建议结合使用 base::cut()
将您的连续变量转换为因子,并使用 ggplot2::scale_fill_manual()
为分类命名颜色。
考虑一下您的代码的这个细微变化:
library(raster)
library(ggplot2)
library(sf)
fr.shp.admin <- getData('GADM', country='FRA', level=0)
fr.shp <- getData('GADM', country='FRA', level=1)
fr.shp.df <- data.frame(fr.shp)
set.seed(123)
dat <- data.frame(
NAME_1 = rep(fr.shp.df$NAME_1, 2),
season = rep(c('a','b'), each = 13),
value = c(sample(0:6, size = 8, replace = T), 0.1, 0.6, 100, 500, 1250,
sample(0:6, size = 8, replace = T), 0.3, 0.7,50, 400, 1700))
fr.shp.sf <- st_as_sf(fr.shp)
fr.shp.admin.sf <- st_as_sf(fr.shp.admin)
temp.shp <- merge(fr.shp.sf, dat, by = 'NAME_1')
temp.shp$value2 = cut(temp.shp$value,
breaks = c(-Inf, 0, 1000, +Inf),
labels = c("low", "medium", "high"))
ggplot() +
geom_sf(data = temp.shp, aes(fill = value2), colour = NA) +
geom_sf(data = fr.shp.admin.sf, fill = NA, col = 'black') +
scale_fill_manual(values = c("low" = "cornflowerblue",
"medium" = "goldenrod2",
"high" = "red")) +
facet_wrap(~season)
library(raster)
library(ggplot2)
library(sf)
fr.shp.admin <- getData('GADM', country='FRA', level=0)
fr.shp <- getData('GADM', country='FRA', level=1)
fr.shp.df <- data.frame(fr.shp)
set.seed(123)
dat <- data.frame(
NAME_1 = rep(fr.shp.df$NAME_1, 2),
season = rep(c('a','b'), each = 13),
value = c(sample(0:6, size = 8, replace = T), 0.1, 0.6, 100, 500,1250,
sample(0:6, size = 8, replace = T), 0.3, 0.7,50, 400,1700))
fr.shp.sf <- st_as_sf(fr.shp)
fr.shp.admin.sf <- st_as_sf(fr.shp.admin)
temp.shp <- merge(fr.shp.sf, dat, by = 'NAME_1')
ggplot() +
geom_sf(data = temp.shp, aes(fill = value), colour = NA) +
geom_sf(data = fr.shp.admin.sf, fill = NA, col = 'black') +
facet_wrap(~season)
我想绘制这张地图,而不是连续的图例,我有类别,我可以为这些类别定义颜色。例如
(0-1] # => 0 & < 1
(1-2] # >= 1 & < 2
(2-3] # >= 2 & < 3
(3-4] # >= 3 & < 4
(4-5] # >= 4 & < 5
> 5 # >= 5
我想这样做是为了给每个类别涂上不同的颜色。我怎样才能在 ggplot
中做到这一点我建议结合使用 base::cut()
将您的连续变量转换为因子,并使用 ggplot2::scale_fill_manual()
为分类命名颜色。
考虑一下您的代码的这个细微变化:
library(raster)
library(ggplot2)
library(sf)
fr.shp.admin <- getData('GADM', country='FRA', level=0)
fr.shp <- getData('GADM', country='FRA', level=1)
fr.shp.df <- data.frame(fr.shp)
set.seed(123)
dat <- data.frame(
NAME_1 = rep(fr.shp.df$NAME_1, 2),
season = rep(c('a','b'), each = 13),
value = c(sample(0:6, size = 8, replace = T), 0.1, 0.6, 100, 500, 1250,
sample(0:6, size = 8, replace = T), 0.3, 0.7,50, 400, 1700))
fr.shp.sf <- st_as_sf(fr.shp)
fr.shp.admin.sf <- st_as_sf(fr.shp.admin)
temp.shp <- merge(fr.shp.sf, dat, by = 'NAME_1')
temp.shp$value2 = cut(temp.shp$value,
breaks = c(-Inf, 0, 1000, +Inf),
labels = c("low", "medium", "high"))
ggplot() +
geom_sf(data = temp.shp, aes(fill = value2), colour = NA) +
geom_sf(data = fr.shp.admin.sf, fill = NA, col = 'black') +
scale_fill_manual(values = c("low" = "cornflowerblue",
"medium" = "goldenrod2",
"high" = "red")) +
facet_wrap(~season)