vlambda博客
学习文章列表

R语言可视化(六):饼图绘制


R语言可视化(六):饼图绘制
This browser does not support music or audio playback. Please play it in WeChat or another browser. R语言可视化(六):饼图绘制

R语言可视化(六):饼图绘制

06.饼图绘制


清除当前环境中的变量

rm(list=ls())

设置工作目录

setwd("C:/Users/Dell/Desktop/R_Plots/06piechart/")

基础pie函数绘制饼图

data <- read.table("demo1_piechart.txt",header = T)
head(data)
## group value
## 1 E 10
## 2 D 30
## 3 C 20
## 4 B 10
## 5 A 10

pie(data$value)

R语言可视化(六):饼图绘制

pie(data$value,
labels = data$group, # 添加标签
col = c("purple", "violetred1", "green3",
"cornsilk", "cyan") # 设置颜色
)

R语言可视化(六):饼图绘制

pie(data$value,
labels = data$group,
col = c("purple", "violetred1", "green3",
"cornsilk", "cyan"),
clockwise = T, # 逆时针排布
init.angle = 45 # 设置第一个扇区的初始角度
)

R语言可视化(六):饼图绘制

pie(data$value,
labels = data$group, # 添加标签
col = c("purple", "violetred1", "green3",
"cornsilk", "cyan"), # 设置颜色
density = 20, # 设置阴影线密度
angle = 20 + 10 * 1:5 # 设置阴影线角度
)

R语言可视化(六):饼图绘制

per.labs <- paste0(data$group,": ",round(100 * data$value / sum(data$value),2),"%")
pie(data$value,
labels = per.labs, # 添加标签
col = c("purple", "violetred1", "green3",
"cornsilk", "cyan"),
main = "pie(*, clockwise = TRUE)"
)
# 添加图例
legend("topright",legend = data$group, cex=1.0,
fill = c("purple", "violetred1", "green3", "cornsilk", "cyan")
)

R语言可视化(六):饼图绘制

n <- 200
pie(rep(1, n), labels = "", col = rainbow(n), border = NA,
main = "pie(*, labels=\"\", col=rainbow(n), border=NA,..)")

R语言可视化(六):饼图绘制

ggplot2包绘制饼图

library(ggplot2)
data <- read.table("demo1_piechart.txt",header = T)
# 计算百分比
data$percent <- paste0(round(100 * data$value / sum(data$value),2),"%")
head(data)
## group value percent
## 1 E 10 12.5%
## 2 D 30 37.5%
## 3 C 20 25%
## 4 B 10 12.5%
## 5 A 10 12.5%

ggplot(data,aes(x="", y= value, fill = group)) +
geom_bar(stat = "identity",color="white") + theme_bw() +
scale_fill_manual(values = c("purple", "violetred1", "green3", "cornsilk", "cyan")) +
theme(axis.text.x = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank()) +
labs(x="", y="") +
geom_text(aes(y = value/2 + c(0, cumsum(value)[-length(value)]),
label = paste0(group,": ", percent)), size=5) +
coord_polar(theta = "y")

R语言可视化(六):饼图绘制

ggpubr包绘制饼图

library(ggpubr)
data <- read.table("demo1_piechart.txt",header = T)
# 添加百分比标签列
data$labs <- paste0(data$group,": ",round(100 * data$value / sum(data$value),2),"%")
head(data)
## group value labs
## 1 E 10 E: 12.5%
## 2 D 30 D: 37.5%
## 3 C 20 C: 25%
## 4 B 10 B: 12.5%
## 5 A 10 A: 12.5%

ggpie(data,x="value", label = "labs",
lab.pos = "in", lab.font = "5",
fill="group", color="white",
palette = c("purple", "violetred1", "green3", "cornsilk", "cyan"))

R语言可视化(六):饼图绘制

pie3D函数绘制三维饼图

# 加载plotrix包
library(plotrix)
data <- read.table("demo1_piechart.txt",header = T)
# 添加百分比标签列
data$labs <- paste0(data$group,": ",round(100 * data$value / sum(data$value),2),"%")
head(data)
## group value labs
## 1 E 10 E: 12.5%
## 2 D 30 D: 37.5%
## 3 C 20 C: 25%
## 4 B 10 B: 12.5%
## 5 A 10 A: 12.5%

pie3D(data$value, labels = data$labs,
theta = pi/5, labelcex=1.2,
explode = 0.1, main = "3D pie chart",
col = c("purple", "violetred1", "green3", "cornsilk", "cyan"))
# 添加图例
legend("topright",legend = rev(data$group), cex=1.0, inset = 0.01,
fill = rev(c("purple", "violetred1", "green3", "cornsilk", "cyan"))
)

R语言可视化(六):饼图绘制

sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936
## [2] LC_CTYPE=Chinese (Simplified)_China.936
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C
## [5] LC_TIME=Chinese (Simplified)_China.936
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] plotrix_3.7-6 ggpubr_0.2.1 magrittr_1.5 ggplot2_3.2.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.1 knitr_1.23 tidyselect_0.2.5 munsell_0.5.0
## [5] colorspace_1.4-1 R6_2.4.0 rlang_0.4.0 stringr_1.4.0
## [9] dplyr_0.8.3 tools_3.6.0 grid_3.6.0 gtable_0.3.0
## [13] xfun_0.8 withr_2.1.2 htmltools_0.3.6 yaml_2.2.0
## [17] lazyeval_0.2.2 digest_0.6.20 assertthat_0.2.1 tibble_2.1.3
## [21] ggsignif_0.5.0 crayon_1.3.4 purrr_0.3.2 glue_1.3.1
## [25] evaluate_0.14 rmarkdown_1.13 labeling_0.3 stringi_1.4.3
## [29] compiler_3.6.0 pillar_1.4.2 scales_1.0.0 pkgconfig_2.0.2

R语言可视化(六):饼图绘制

END


R语言可视化(六):饼图绘制

R语言可视化(六):饼图绘制
R语言可视化(六):饼图绘制


更多精彩推荐,请关注我们
R语言可视化(六):饼图绘制
把时间交给阅读


您点的每个赞,我都认真当成了喜欢