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

问题描述 投票:0回答:1

enter image description here

这个瓦片图显示了德克萨斯州十个人口最多的县的冠状病毒感染率的强度。红色最浅的瓷砖在该日有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()函数有关,但我试过的最多变换只是让深红色应用于数据集中的更多值,这与我想要的正好相反。

谢谢!

r dataset data-visualization gradient legend-properties
1个回答
0
投票
scale_fill_gradientn(limits = c(-3,3)

在这个例子中,'-3'指的是下限,'3'指的是上限。

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

ggplot 比例尺颜色渐变到数据范围之外的范围。

© www.soinside.com 2019 - 2024. All rights reserved.