昨天在微博中刷到了下面的圖片巷懈,當(dāng)時覺得就很驚艷,很好奇怎么制作的战虏。
而后面又看到1篇微信公共號推文萍丐,才知道這種圖形叫“風(fēng)玫瑰圖”轩端,并介紹了如何使用ggplot2繪制新冠疫情的風(fēng)玫瑰圖。下面碉纺,參考上面提到的推文中的代碼船万,我用自己的思路繪制了一幅新冠疫情的風(fēng)玫瑰圖。
#1. 數(shù)據(jù)提取骨田。這里使用了nCov2019包中的新冠疫情數(shù)據(jù)耿导。
library(nCov2019)
data <- load_nCov2019()
mydata <- data['global']
#2. 數(shù)據(jù)整理
library(dplyr)
data_rose <- mydata %>% filter(time == time(data)) %>%? ? ? ? ? ? ?arrange(desc(cum_confirm)) %>% slice(1:30)? #提取數(shù)據(jù),提取前30位國家
data_rose$country <- factor(data_rose$country, levels = data_rose$country) #按照感染人數(shù)排序國家
data_rose$angle = 1:30 * 360/30 #計(jì)算角度态贤,后面會用到
#3. 繪圖
library(ggplot2)
ggplot(data_rose, aes(country, cum_confirm, fill = cum_confirm)) +
? geom_col(width = 1, color = 'white') +? ?#繪制圖形基本結(jié)構(gòu)
? geom_col(aes(y = I(6)), width = 1, alpha = 0.1, fill = 'white') +
? geom_col(aes(y = I(4)), width = 1, alpha = 0.3, fill = 'white') +
? geom_col(aes(y = I(2)), width = 1, color = 'white', fill = 'white') +? #繪制空心白和暈輪
? scale_y_log10() + #縱坐標(biāo)取對數(shù)以壓縮縱坐標(biāo)
? scale_fill_gradientn(colors = c("darkgreen", "green", "orange", "firebrick","red"), trans = 'log') + #顏色填充舱呻,注意顏色按對數(shù)映射
? geom_text(aes(label = paste(paste(country, cum_confirm, sep = '\n'), '例', sep = '\n'),
? ? ? ? ? ? ? ? y = cum_confirm * 0.8, angle = angle),
? ? ? ? ? ? ? ?data = subset(data_rose, cum_confirm > 700),
? ? ? ? ? ? ? ?color = "white", fontface="bold", vjust=1, size = 2) + #添加文字
? geom_text(aes(label = paste0(cum_confirm, '例',country),
? ? ? ? ? ? ? ? y = cum_confirm * 4, angle = angle+90),
? ? ? ? ? ? data = subset(data_rose, cum_confirm < 700),
? ? ? ? ? ? fontface="bold", vjust = 0, size = 2) + #添加文字
? coord_polar(direction=-1) + #極坐標(biāo)轉(zhuǎn)換,逆時針排序
? theme_void() +
? theme(legend.position="none")
最終出圖如下:
最后附上我的代碼:
鏈接:https://pan.baidu.com/s/1EuGth8NbDJKy2w1OB1mUBA
提取碼:x1wo