今天再介绍一个饼图的展示,又称为甜甜圈图。我们今天用一个新的包:ggpie来实现。
![](https://img.haomeiwen.com/i24339453/f74d83402c1895c7.png)
我们利用自带的diamonds数据来测试。
![](https://img.haomeiwen.com/i24339453/501335e59172c720.png)
用ggpie函数实现一个基本的饼图。
ggpie(data = diamonds, group_key = "cut", count_type = "full",
label_info = "all", # 标签内容:默认"count", "ratio", "all"
label_type = "horizon", # 标签形式:"none", "circle", "horizon"
label_split = NULL,
label_size = 4,
label_pos = "in", # 超过阈值的标签在内部
label_threshold = 25) # 设置阈值
![](https://img.haomeiwen.com/i24339453/a8df66dd7ef458df.png)
ggdonut(data = diamonds, group_key = "cut", count_type = "full",
label_info = "all",
label_type = "circle",
label_split = NULL,
label_size = 4,
label_pos = "in")
这个图相当于将分类变量cut再制作一个甜甜圈图,使用函数ggdonut()修饰了图片:
![](https://img.haomeiwen.com/i24339453/dce7830bb30c28ac.png)
也可以通过更详细的参数来控制内径和外径的大小。
ggdonut(data = diamonds, group_key = "cut",#分组变量
count_type = "full",#使用全部数据
label_info="all",#标签展示数字和占比
label_type="circle",#标签的类型
label_split = NULL,
label_pos="in",#标签的位置
label_size=4,#标签的大小
border_color="white",#分界线颜色
r0 = 1,#内圈直径大小
r1 = 3,#外圈直径大小
label_gap = 0.1,#标签和圈的距离
donut.label.size=4 #圈图内标签大小
)
当然,也可以使用到两个分类变量,比如cut和color,制作一个内嵌甜甜圈图(nested donut plot),使用函数ggnestedpie()实现。
ggnestedpie(data = diamonds, group_key = c("cut", "color"), count_type = "full",
inner_label_info = "all",
inner_label_split = NULL,
inner_label_threshold = 3,# 设置内层环形的阈值
inner_label_size = 2,
outer_label_type = "circle", # 设置外层环形
outer_label_pos = "in",
outer_label_info = "all")
![](https://img.haomeiwen.com/i24339453/424e176b626a0003.png)
还可以用ggrosepie制作玫瑰花瓣一样的饼图。
ggrosepie(diamonds, group_key = c("cut","color"),
count_type = "full",
label_info = "all",
tick_break = c(5000, 7000, 11000,20000), # 设置坐标刻度
donut_frac = NULL) # 中间无空心
![](https://img.haomeiwen.com/i24339453/0af409f971086a79.png)
还可以设置为中间为空心的玫瑰花瓣图。
ggrosepie(diamonds, group_key = c("cut","color"),
count_type = "full",
label_info = "all",
tick_break = c(5000, 7000, 11000,20000), # 设置坐标刻度
donut_frac = 0.3, # 中间的空心
donut_label_size = 3)
![](https://img.haomeiwen.com/i24339453/3ff48260508a0e77.png)
网友评论