如何在 R 中手动调整平铺图的梯度比例?

How can you manually adjust gradient scale for tile plot in R?

此磁贴图显示了德克萨斯州人口最多的十个县的冠状病毒感染率强度。当天,最浅红色的方块所在县的确诊冠状病毒病例占该县人口的 0%。最深红色的地块在该特定日期占该县 0.25% 的确诊冠状病毒病例。

是否可以调整比例,将最深的红色设置为 50%?源数据集中没有这个值,只是觉得如果我们能将最高强度的颜色设置为数据集中最大值以外的值可能更符合数据可视化设计原则。

例如,将最深的红色设置为西班牙流感期间的感染率峰值可能会很有趣。

谢谢!

library("tidyverse")
library("ggthemes")
library("RColorBrewer")

# may16 is a modified dataset from the Texas Department of State Health Services: (1) it's been transposed from wide to long, (2) it's been mutated to include a new "Rate" column, (3) "Date" column has been transformed from type <chr> to <date>, and (4) it's .csv instead of .xlsx.
# may16 modified dataset includes 18,035 rows and 5 columns: (1) "County" <chr>, (2) "Population" <int>, (3) "Date" <date>, (4) "Cases" <int>, and (5) "Rate" <dbl>
# top10 is a list of the names of the ten most populous counties in Texas.

may16 %>% filter(County %in% top10)
%>% ggplot(aes(Date, County, fill=Rate))
+ geom_tile(color="grey50")
+ theme_minimal()
+ theme(panel.grid=element_blank())
+ scale_fill_gradientn(colors=RColorBrewer::brewer.pal(9, "Reds"))
+ geom_vline(xintercept=as.Date("2020-05-01"))
+ labs(title = "Coronavirus Infection Rates", subtitle = "for 10 most populous counties", caption = "Source: Texas Department of State Health Services 2020", x="", y="")
+ geom_text(aes(as.Date("2020-04-25"),"Travis",label="Texas re-opens »"))

它可能与 scale_fill_gradientn() 函数有关,但最多我尝试过的转换只是将较深的红色应用于数据集中的更多值,这是一种与我要找的相反。

谢谢!

scale_fill_gradientn(limits = c(-3,3)

本例中'-3'表示下限,'3'表示上限

这将覆盖基于源数据集的默认下限和上限。

ggplot scale color gradient to range outside of data range