南丁格尔玫瑰图 With ggplot2【R语言】
前言
之前在饼图中提到过,要整理一下南丁格尔玫瑰图的画法
南丁格尔玫瑰图又名鸡冠花图、极坐标区域图,是南丁格尔在克里米亚战争期间提交的一份关于士兵死伤的报告时发明的一种图表。南丁格尔玫瑰图是在极坐标下绘制的柱状图,使用圆弧的半径长短表示数据的大小(数量的多少)。
图
需要的工具可视化:ggplot2
处理:stringr、dplyr
library(ggplot2) library(dplyr) library(stringr)
r
运行
123 数据处理来源:https://github.com/rfordatascience/tidytuesday
hike_data <- readr::read_rds("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-11-24/hike_data.rds") hike_data$region <- as.factor(word(hike_data$location, 1, sep = " -- ")) hike_data$length_num <- as.numeric(sapply(strsplit(hike_data$length, " "), "[[", 1)) plot_df <- hike_data %>% group_by(region) %>% summarise( sum_length = sum(length_num), mean_gain = mean(as.numeric(gain)), n = n() ) %>% mutate(mean_gain = round(mean_gain, digits = 0))
r
运行
123456789101112数据长这个样子:

使用:geom_col
p <- ggplot(plot_df) + geom_col( aes( x = reorder(str_wrap(region, 5), sum_length), y = sum_length, fill = n ), position = "dodge2", show.legend = TRUE, alpha = .9 ) p
r
运行
123456789101112
使用:coord_polar()
p <- p + coord_polar() p
r
运行
12
p + geom_hline( aes(yintercept = y), data.frame(y = c(0:3) * 1000), color = "lightgrey" ) + geom_segment( aes( x = reorder(str_wrap(region, 5), sum_length), y = 0, xend = reorder(str_wrap(region, 5), sum_length), yend = 3000 ), linetype = "dashed", color = "gray12" ) + scale_y_continuous( limits = c(-1500, 3500), expand = c(0, 0), breaks = c(0, 1000, 2000, 3000) ) + scale_fill_gradientn( "Amount of Tracks", colours = c( "#6C5B7B","#C06C84","#F67280","#F8B195") ) + guides( fill = guide_colorsteps( barwidth = 15, barheight = .5, title.position = "top", title.hjust = .5 ) ) + theme_minimal()+ theme( axis.title = element_blank(), axis.ticks = element_blank(), axis.text.y = element_blank(), axis.text.x = element_text(color = "gray12", size = 12), legend.position = "bottom" )
r
运行
123456789101112131415161718192021222324252627282930313233343536
最后的图大概就是这个样子,还挺好看
library(ggplot2) library(stringr) hike_data <- readr::read_rds("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-11-24/hike_data.rds") hike_data$region <- as.factor(word(hike_data$location, 1, sep = " -- ")) hike_data$length_num <- as.numeric(sapply(strsplit(hike_data$length, " "), "[[", 1)) plot_df <- hike_data %>% group_by(region) %>% summarise( sum_length = sum(length_num), mean_gain = mean(as.numeric(gain)), n = n() ) %>% mutate(mean_gain = round(mean_gain, digits = 0)) plt <- ggplot(plot_df) + geom_hline( aes(yintercept = y), data.frame(y = c(0:3) * 1000), color = "lightgrey" ) + geom_col( aes( x = reorder(str_wrap(region, 5), sum_length), y = sum_length, fill = n ), position = "dodge2", show.legend = TRUE, alpha = .9 ) + # Lollipop shaft for mean gain per region geom_segment( aes( x = reorder(str_wrap(region, 5), sum_length), y = 0, xend = reorder(str_wrap(region, 5), sum_length), yend = 3000 ), linetype = "dashed", color = "gray12" ) + coord_polar() plt <- plt + scale_y_continuous( limits = c(-1500, 3500), expand = c(0, 0), breaks = c(0, 1000, 2000, 3000) ) + scale_fill_gradientn( "Amount of Tracks", colours = c( "#6C5B7B","#C06C84","#F67280","#F8B195") ) + guides( fill = guide_colorsteps( barwidth = 15, barheight = .5, title.position = "top", title.hjust = .5 ) ) + theme_minimal()+ theme( axis.title = element_blank(), axis.ticks = element_blank(), axis.text.y = element_blank(), axis.text.x = element_text(color = "gray12", size = 12), legend.position = "bottom" )
r
运行
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869相关知识
南丁格尔玫瑰图 With ggplot2【R语言】
R语言ggplot2做玫瑰图的简单小例子
R语言ggplot2做玫瑰图(花瓣图)的简单小例子
使用ggplot2绘制风向风速玫瑰图
统计之美 | 南丁格尔的玫瑰
r语言数据分析案例
如何制作南丁格尔玫瑰图?多种方法对比制作!
掌握R语言:交互式绘图的实战技巧
用R语言绘制玫瑰花
「图表制作」 南丁格尔玫瑰图
网址: 南丁格尔玫瑰图 With ggplot2【R语言】 https://www.huajiangbk.com/newsview2501611.html
| 上一篇: 浪漫的纸玫瑰花折法步骤图解 纸玫 |
下一篇: 520块钱怎么叠玫瑰花 |
推荐分享
- 1君子兰什么品种最名贵 十大名 4012
- 2世界上最名贵的10种兰花图片 3364
- 3花圈挽联怎么写? 3286
- 4迷信说家里不能放假花 家里摆 1878
- 5香山红叶什么时候红 1493
- 6花的意思,花的解释,花的拼音 1210
- 7教师节送什么花最合适 1167
- 8勿忘我花图片 1103
- 9橄榄枝的象征意义 1093
- 10洛阳的市花 1039
