r - 如何为每个方面构建一个具有各自比例的热图,而不是在 r 中为所有人构建一个通用比例?
问题描述
我正在尝试创建一个heatmap
应该根据每个月(每行)分配颜色的% vaccinated
例如 1 月份所有州之间的颜色比较,然后
例如,三月份所有州之间的颜色比较......
然后是 Apr ... Jun 等
问题:基本上我希望每个月都有自己的高低比例,我正在尝试这样做,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
解决方案
推荐阅读
- php - 如何正确拥有具有多个过滤器的搜索页面?
- c - snprintf() 只读取到换行符?
- c# - 在 C# 中为属性定义 if 语句
- openssl - 使用 openssl (virtualbox) 使用文件中的公钥加密消息
- javascript - 如何使用javascript交换图片
- ios - 当图像滚动然后导航栏更新
- javascript - 如果数组第一个元素中的字符串包含数组第二个元素中字符串的所有字母,则返回 true
- android - 铃声意图:检测取消按钮或无选择
- php - 如何在 laravel 中使用 eloquent 和 db 做到这一点
- data-structures - 反向单链表java?