R语言数据处理R for statisticsR语言可视化之美

ggplot2绘制玫瑰图

2021-04-05  本文已影响0人  R语言数据分析指南

本节根据2020-2021年美国疾病死亡率数据来绘制玫瑰图,后台回复关键词2021-4-5获取数据及代码,喜欢的小伙伴欢迎关注我的公众号R语言数据分析指南

加载R包

library(tidyverse)

加载数据

deaths <- read.delim("deaths.xls",header = T,sep="\t")

数据过滤

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() 按行进行数据筛选 & 且
  • select() 取需要的列
  • starts_with() 取以covid开头的列
  • mutate() 添加新列
  • select(-c(**)) 删除列
  • group_by(month) 以month对数据进行分组
  • pivot_longer() 宽表转长表
  • pivot_longer()函数有三个主要的参数:
  • cols,表示哪些列需要转换
  • names_to,表示cols选取的这些列的名字,构成了新的一列
  • values_to,表示cols选取的这些列的值,构成了新的一列
  • coord_polar() 转换为极坐标
  • month.abb() 将月份转换为缩写
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"

数据可视化

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()
上一篇 下一篇

猜你喜欢

热点阅读