1、pie()

pie()是基础的绘制饼图函数,用法简单

1
2
3
4
5
6
7
8
pie(Prop, labels = LETTERS[1:5], main = "Test Piechart")

# labels:设置标签
# radius:设置半径,default 0.8
# clockwise:设置是否顺时针,默认为F
# init.angle:设置起始单元的角度
# col:设置颜色
# border与lty分别设置边的颜色与形状

image-20230311161634571

2、pie3D()

  • pie3D()是plotrix包中用于绘制3D效果饼图的函数
1
2
3
4
5
6
7
8
9
library(plotrix)
slices <- c(10, 12, 4, 16, 8)
lbls <- c("US", "UK", "Australia", "Germany", "France")
pie3D(slices,labels=lbls,explode=0.1,
      main="Pie Chart of Countries ")
# 部分参数同上,特征参数如下
# theta:设置倾斜的角度,默认为pi/6
# explode:设置分开的距离,默认为0
# height:设置高度,默认为0.1

image-20230311162107163

3、ggplot

  • 本质上是绘制变形的柱状图
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
library(tidygraph)
df <- data.frame(
  group = c("Male", "Female", "Child"),
  value = c(25, 25, 50)
) %>% 
  dplyr::mutate(pc = paste0(value/sum(value)*100,"%"))
df
#    group value  pc
# 1   Male    25 25%
# 2 Female    25 25%
# 3  Child    50 50%

ggplot(df, aes(x="", y=value, fill=group)) +
  geom_bar(width = 1, stat = "identity", color="white") +
  coord_polar("y", start=0) + 
  geom_text(aes(label = pc),
            position = position_stack(vjust = 0.5),
            size = 8) +
  theme_void()
image-20230311162632998

4、ggpubr

  • ggpubr支持方便的绘制饼图与环图
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
library(ggpubr)
library(tidygraph)
library(patchwork)

df <- data.frame(
  group = c("Male", "Female", "Child"),
  value = c(25, 25, 50)
) %>% 
  dplyr::mutate(pc = paste0(value/sum(value)*100,"%"))
df
#    group value  pc
# 1   Male    25 25%
# 2 Female    25 25%
# 3  Child    50 50%

p1 = ggpie(df, "value", label = "pc",
      lab.pos = "in", lab.font = "white",
      fill = "group", color = "white",
      palette = c("#00AFBB", "#E7B800", "#FC4E07"))
p2 = ggdonutchart(df, "value", label = "pc",
             lab.pos = "in", lab.font = "white",
             fill = "group", color = "white",
             palette = c("#00AFBB", "#E7B800", "#FC4E07"))
p1 | p2
image-20230311163105001

5、双环图

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# devtools::install_github("cardiomoon/moonBook")
# devtools::install_github("cardiomoon/webr")
library(ggplot2)
library(moonBook)
library(webr)

test=iris[,c(4,5)]
test[,1]=ceiling(test[,1])
## 将花按宽度分为1、2、3等共3类
table(test$Petal.Width, test$Species)
#   setosa versicolor virginica
# 1     50          7         0
# 2      0         43        27
# 3      0          0        23

PieDonut(test,aes(pies=Petal.Width,donuts=Species))
PieDonut(test,aes(pies=Petal.Width,donuts=Species),
         ratioByGroup=FALSE, ,labelposition=1)
PieDonut(test,aes(pies=Petal.Width,donuts=Species),
         explodeDonut=TRUE,
         explode=2, selected = c(1,2)) 

image-20230311163832000