我正在使用 usmaps 包进行绘图。现在,我正在使用连续比例填充,但我想使用 RColorBrewer 的离散托盘。在我当前的代码(如下)中,变量是在列表中定义的,然后循环。那么如何在不将每个变量单独转换为因子的情况下将变量列表定义为因子呢?
library(usmap)
library(ggplot2)
library(tidyverse)
library(haven)
library(RColorBrewer)
library(stringr)
library(extrafont)
library(dplyr)
library(purrr)
var_list <- ("var1", "var2", "var3", "var4", "var5")
var_labs <- ("This is var1", "This is var2", "This is var3", "This is var4", "This is var5")
for (k in seq_along(var_list)) {
for(i in 0:10) {
print(plot_usmap(data = filter(database, year == 1800 + i*10), values = var_list[k]) +
scale_fill_continuous(
low = "white", high = "blue", na.value="light gray",
label = scales::comma,
name = "Legend") +
theme(legend.position = "right",
plot.title=element_text(size=18),
legend.title = element_blank())+
labs(title = str_c(var_labs[k], ", ", 1800 + i*10)))
}
}
我尝试的是如下所示。我希望能够将中断/离散单元定义为非连续的。我得到“错误:提供给离散刻度的连续值”
scale_fill_brewer(
type = 'quant',
palette = 'Purples',
breaks = c(100, 200, 300, 400, 500, 600, 1000, 2000, 8000),
drop=FALSE) +
编辑:我也尝试过类似的东西:
database$var1_cut <- cut(database$var_1,
breaks = c(0, 100, 200, 300, 400, 500, 600, 1000, 8000),
right = FALSE)
database$var1_cutf <- as.factor(database$var1_cut)
var1_discrete <- c("var1_cutf"
var1_discretelab <- c("Var1 on Discrete scale")
但它仍然给了我“错误:离散值应用于连续规模。