R 缩放\填充\步骤中的单个选项可更改图例中的颜色渲染
我使用ggplot的scale\u fill\u stepsn生成一个具有阶梯比例的地图。当我使用选项n时,会在图例中正确渲染指定的颜色。n、 打断基于指定的打断数计算打断。但是,当我使用该选项手动指定与n.breaks中使用的打断数相同的打断时,图例中的颜色渲染会发生变化,并且不会正确渲染 这是没有道理的。第二个示例中的图例颜色是否与第一个示例中的图例颜色相同R 缩放\填充\步骤中的单个选项可更改图例中的颜色渲染,r,ggplot2,R,Ggplot2,我使用ggplot的scale\u fill\u stepsn生成一个具有阶梯比例的地图。当我使用选项n时,会在图例中正确渲染指定的颜色。n、 打断基于指定的打断数计算打断。但是,当我使用该选项手动指定与n.breaks中使用的打断数相同的打断时,图例中的颜色渲染会发生变化,并且不会正确渲染 这是没有道理的。第二个示例中的图例颜色是否与第一个示例中的图例颜色相同 library(urbnmapr) library(ggplot2) library(dplyr) library(ggthemes)
library(urbnmapr)
library(ggplot2)
library(dplyr)
library(ggthemes)
# Set colors
red <- c(0.67, 0.75, 0.84, 0.92, 1, 1, 0.8, 0.53, 0, 0, 0, 0)
green <- c(0.25, 0.4, 0.56, 0.71, 0.86, 1, 1, 0.95, 0.9, 0.75, 0.6, 0.48)
blue <- c(0.11, 0.18, 0.25, 0.33, 0.4, 0.45, 0.4, 0.27, 0, 0, 0, 0)
# Obtain county polygon data
states_sf <- get_urbn_map(map = "states", sf = TRUE)
counties_sf <- get_urbn_map(map = "counties", sf = TRUE)
# Assign random values of data to each count
counties_sf$value = runif(length(counties_sf$county_fips), min=-3.0, max=3.0)
# Remove AK and HI - lower 48 only
states_sf <- states_sf[!(states_sf$state_abbv %in% c("HI","AK")),]
counties_sf <- counties_sf[!(counties_sf$state_abbv %in% c("HI","AK")),]
# Plot county-level data with a discrete legend
data_levels <- c(-3,-1.5, -0.8, -0.5, -0.25,-0.1,0.1,0.25,0.5,.8,1.5,3)
level_colors <- rgb(red, green, blue)
length(data_levels)
length(level_colors)
# First version -
counties_sf %>%
ggplot() +
# Overlay State Outlines
# Plot county data and fill with value
geom_sf(mapping = aes(fill = value), color = NA) +
geom_sf(data = states_sf, fill = NA, color = "black", size = 0.25) +
# Remove grid lines from plot
coord_sf(datum = NA) +
#
# THE FIRST OPTION of scale_fill_stepsn IS WHERE THEY ARE DIFFERENT
#
scale_fill_stepsn(n.breaks=12, colors=level_colors, limits=c(-3,3),
labels=scales::label_number(accuracy=0.1)) +
labs(title='This Data is Completely Random',
fill ='The Legend') +
theme_map() +
theme(legend.position = "bottom",
legend.key.width=unit(1.5,"cm"),
legend.box.background = element_rect(color="black", size=2),
legend.title = element_text(face = "bold"),
legend.spacing = unit(0.25,"cm"),
legend.justification = "center",
plot.title=element_text(hjust=0.5)) +
guides(fill = guide_colorsteps(even.step=TRUE,
title.position="top",
title.hjust = 0.5,
frame.colour = 'black',
barwidth=unit(250,'points'),
axis.linewidth=unit(3,'points')))
此版本生成以下图像。请注意,图例中的颜色现在不同了。
第一个选项将-3到3之间的12个间隔均匀隔开,然后与您的颜色完全一致。而第二个选项设置间距不均的值,精确的颜色在某些中断之间。但是(隐藏的)渐变仍然是均匀分布的。要将渐变作为打断隔开,需要设置比例的
值
参数。下面是一个简化的例子
库(ggplot2)
非常好。非常感谢。
#
# Second version -
counties_sf %>%
ggplot() +
# Overlay State Outlines
# Plot county data and fill with value
geom_sf(mapping = aes(fill = value), color = NA) +
geom_sf(data = states_sf, fill = NA, color = "black", size = 0.25) +
# Remove grid lines from plot
coord_sf(datum = NA) +
#
# THE FIRST OPTION of scale_fill_stepsn IS WHERE THEY ARE DIFFERENT
# replaced n.breaks with breaks option
#
scale_fill_stepsn(breaks=data_levels, colors=level_colors, limits=c(-3,3),
labels=scales::label_number(accuracy=0.1)) +
labs(title='This Data is Completely Random',
fill ='The Legend') +
theme_map() +
theme(legend.position = "bottom",
legend.key.width=unit(1.5,"cm"),
legend.box.background = element_rect(color="black", size=2),
legend.title = element_text(face = "bold"),
legend.spacing = unit(0.25,"cm"),
legend.justification = "center",
plot.title=element_text(hjust=0.5)) +
guides(fill = guide_colorsteps(even.step=TRUE,
title.position="top",
title.hjust = 0.5,
frame.colour = 'black',
barwidth=unit(250,'points'),
axis.linewidth=unit(3,'points')))