本節(jié)根據(jù)2020-2021年美國疾病死亡率數(shù)據(jù)來繪制玫瑰圖锅很,后臺(tái)回復(fù)關(guān)鍵詞2021-4-5獲取數(shù)據(jù)及代碼辰狡,喜歡的小伙伴歡迎關(guān)注我的公眾號(hào)R語言數(shù)據(jù)分析指南
加載R包
library(tidyverse)
加載數(shù)據(jù)
deaths <- read.delim("deaths.xls",header = T,sep="\t")
數(shù)據(jù)過濾
dat <- deaths %>%
filter(state == "United States" & year == "2020") %>%
select(state,year, month, all_cause,
heart_disease,starts_with("covid")) %>%
mutate(covid = covid_other + covid_only,
all_other = all_cause - covid - heart_disease) %>%
select(-c(all_cause, covid_other,covid_only)) %>%
pivot_longer(cols = c("heart_disease","covid", "all_other"),
names_to = "cause",
values_to = "number") %>%
group_by(month) %>%
mutate(label_y = sum(number) + 40000,
month = factor(month.abb[month], levels = month.abb))
- filter() 按行進(jìn)行數(shù)據(jù)篩選 & 且
- select() 取需要的列
- starts_with() 取以covid開頭的列
- mutate() 添加新列
- select(-c(**)) 刪除列
- group_by(month) 以month對數(shù)據(jù)進(jìn)行分組
- pivot_longer() 寬表轉(zhuǎn)長表
- pivot_longer()函數(shù)有三個(gè)主要的參數(shù):
- cols撼唾,表示哪些列需要轉(zhuǎn)換
- names_to,表示cols選取的這些列的名字撬码,構(gòu)成了新的一列
- values_to淮悼,表示cols選取的這些列的值减宣,構(gòu)成了新的一列
- coord_polar() 轉(zhuǎn)換為極坐標(biāo)
- month.abb() 將月份轉(zhuǎn)換為縮寫
my_months <- sample(1:12)
month.abb[my_months]
> my_months
[1] 7 3 11 1 8 10 2 4 6 9 5 12
> month.abb[my_months]
[1] "Jul" "Mar" "Nov" "Jan" "Aug" "Oct" "Feb" "Apr" "Jun" "Sep" "May"
[12] "Dec"
數(shù)據(jù)可視化
ggplot(dat, aes(x = month, y = number, fill = cause)) +
geom_col(color = "#5B5A5A",
width = 1) +
geom_text(aes(label = month, y = label_y),
family = "Deckhouse Regular") +
scale_fill_manual(name = NULL,
values = c("#87c0e6","#ffa0aa","#808080"),
labels = c("All other deaths","COVID-19","Heart disease")) +
theme_void() +
theme(legend.position = c(.5,.1),
legend.direction = "horizontal") +
coord_polar()