如何解决如何为每个方面构建一个具有各自比例的热图,而不是 r 中所有的一个通用比例?
我正在尝试创建一个 heatmap
,它应该根据 % vaccinated
每个月(对于每一行)分配颜色
例如一月份所有州之间的颜色比较,然后
例如 3 月份所有州之间的颜色比较 .. .
然后四月...君等
问题:基本上我希望每个月都有自己的高低比例,我正试图用 facet
做到这一点,但它正在分配所有方面/月的一个常见的低-高量表。
library(tidyverse)
library(lubridate)
library(scales)
file_url1 <- url("https://raw.githubusercontent.com/johnsNow09/covid19-df_stack-code/main/df_vaccination.csv")
df_vaccination <- read.csv(url(file_url1))
df_vaccination <- df_vaccination %>%
mutate(Updated.On = as.Date(Updated.On))
代码:我试过了
df_vaccination %>%
filter(State != "India") %>%
# summarise each month,state's vaccination
mutate(month_abbr = month(Updated.On,label = TRUE,abbr = TRUE),State = fct_reorder(State,Population,max)) %>%
group_by(month_abbr,State) %>%
summarise(monthly_ind_vaccinated = sum(Total.Individuals.Vaccinated_Dailycalc,na.rm = TRUE),Population = first(Population),.groups = "drop") %>%
# get % Vaccination to State population for each month
group_by(State) %>%
mutate(prc_vaccinated_per_pop = monthly_ind_vaccinated / Population) %>%
na.omit() %>%
ungroup() %>%
filter(State %in% c("Delhi","Maharashtra")) %>%
# group_by(month_abbr) %>%
ggplot(aes(x = State,y = month_abbr,fill = prc_vaccinated_per_pop)) +
geom_tile() +
scale_fill_gradient2(low = "white",high = "darkblue",labels = percent) +
facet_wrap(~as.factor(month_abbr),scales = "free_y",nrow = 6) +
theme(axis.text.x = element_text(angle = 90,vjust = -.02),strip.text = element_blank()) +
labs(title = "States with highest % Vaccination each month ?",caption = "created by ViSa",fill = "% Vaccinated each month",x = "",y = "")
输出:
我认为由于颜色值基于 fill
,因此不会让不同的比例应用于不同的方面。
有没有类似于 (scales = free_fill
) 而不是 (scales = free_y
) 的东西?
数据输出:
# A tibble: 12 x 5
# Groups: month_abbr [6]
month_abbr State monthly_ind_vaccina~ Population prc_vaccinated_per_~
<ord> <fct> <int> <dbl> <dbl>
1 Jan Delhi 43948 18710922 0.00235
2 Jan Maharash~ 228424 123144223 0.00185
3 Feb Delhi 322859 18710922 0.0173
4 Feb Maharash~ 794370 123144223 0.00645
5 Mar Delhi 666628 18710922 0.0356
6 Mar Maharash~ 4590035 123144223 0.0373
7 Apr Delhi 1547324 18710922 0.0827
8 Apr Maharash~ 7942882 123144223 0.0645
9 May Delhi 1613335 18710922 0.0862
10 May Maharash~ 4455440 123144223 0.0362
11 Jun Delhi 250366 18710922 0.0134
12 Jun Maharash~ 1777873 123144223 0.0144
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。