我正在根据某个研究主题的出版物数量绘制世界各国的热图。地图几乎可以工作,但颜色条不会表现...我希望 0 值是灰色的,后续的 bin 遵循绿色渐变。我使用的调色板已在其他 ggplot 应用程序中使用,但我无法使其与scale_fill_stepsn 或任何相关函数一起使用。 为什么这不会绘制一个灰色箱和其余箱的绿色渐变?
使用数据:
# Load the necessary libraries
library(ggplot2)
library(sf)
library(dplyr)
library(rnaturalearth)
library(rnaturalearthdata)
# Load world map data (countries and continents)
world <- ne_countries(scale = "medium", returnclass = "sf")
加上添加的“出版物”栏,其中包含每个国家/地区的出版物数量。
代码:
# Define the color palette for the bins (light grey to dark green)
# Create color palette
green_palette <- colorRampPalette(c("#ADBC9F", "#12372A"))(6)
custom_palette <- c("#B4B4B8", green_palette)
first_color <- "#B4B4B8" # Gray color for 0 values
# Create a world map with countries color-coded by number of publications using binned colors
map_countries <- ggplot(world_data) +
geom_sf(aes(fill = Publications), color = "white") + # Color by publications
scale_fill_stepsn(
breaks = c(0, 1, 5, 10, 25, 50, 75, Inf), # Define bin breaks
colors = c(first_color, green_palette), # First color is gray, rest are green gradient
na.value = first_color # Handle NA values with the same gray color
) +
# Adjust the legend appearance
guides(
fill = guide_coloursteps(
even.steps = TRUE,
show.limits = TRUE,
barwidth = unit(15, 'cm'), # Adjust the width of the colorbar
title = "Publications" # Optional: Add title to colorbar
)
) +
theme_minimal() +
theme(
legend.position = "bottom",
legend.key.height = unit(1, "cm"),
legend.title = element_text(face = "bold"),
legend.spacing = unit(0.25, "cm"),
legend.justification = "center",
plot.title = element_text(hjust = 0.5)
) +
# Customize the legend and colorbar appearance
#theme_minimal() +
labs(
title = "World Map: Publications by Country (Binned Colorbar)",
subtitle = "Total number of publications per country"
) +
# Change projection
coord_sf(crs = "+proj=robin") # Use the Robinson projection
# Print country map
print(map_countries)
正在制作的情节:
谢谢你。
如果您想要分档秤,其中
颜色不应该沿着渐变均匀分布
然后你必须将向量传递给
values=
参数,其中
向量给出了颜色向量中每种颜色的位置(介于 0 和 1 之间)。
但是,为了获得所需的结果,您可以简单地手动将映射到
fill
的变量装箱并使用 scale_fill_manual
:
library(ggplot2)
library(rnaturalearth)
library(rnaturalearthdata)
#>
#> Attaching package: 'rnaturalearthdata'
#> The following object is masked from 'package:rnaturalearth':
#>
#> countries110
world <- ne_countries(scale = "medium", returnclass = "sf")
green_palette <- colorRampPalette(c("#ADBC9F", "#12372A"))(6)
custom_palette <- c("#B4B4B8", green_palette)
first_color <- "#B4B4B8" # Gray color for 0 values
set.seed(123)
world_data <- world
world_data$Publications <- sample(200, nrow(world), replace = TRUE)
breaks <- c(0, 1, 5, 10, 25, 50, 75, Inf)
world_data$pub_cut <- cut(
world_data$Publications,
breaks = breaks,
right = TRUE,
include.lowest = TRUE
)
ggplot(world_data) +
geom_sf(
aes(fill = pub_cut),
color = "white",
show.legend = TRUE
) +
scale_fill_manual(
values = c(first_color, green_palette),
labels = c(
0, "1 - 5", "6 - 10", "11 - 25", "26 - 50", "51 - 75",
"76+"
),
drop = FALSE
) +
guides(
fill = guide_legend(
title = "Publications",
theme = theme(
legend.text.position = "bottom",
legend.key.width = unit(1.5, "cm"),
legend.key.height = unit(1, "cm"),
legend.key.spacing.x = unit(0, "pt")
),
override.aes = list(linewidth = 0, color = NA),
nrow = 1
)
) +
theme_minimal() +
theme(
legend.position = "bottom",
legend.title = element_text(face = "bold"),
legend.justification = "center",
plot.title = element_text(hjust = 0.5)
) +
labs(
title = "World Map: Publications by Country (Binned Colorbar)",
subtitle = "Total number of publications per country"
) +
coord_sf(crs = "+proj=robin") # Use the Robinson projection