ggpubr包系列学习教程(十四)

使用ggpie函数绘制饼图


加载所需R包

library(ggpubr)

基本用法:

Usage

ggpie(data, x, label = NULL, lab.pos = c("out", "in"), lab.adjust = 0,
      lab.font = c(4, "bold", "black"), color = "black", fill = "white",
      palette = NULL, size = NULL, ggtheme = theme_classic(), ...)

常用参数:

Arguments

data    #a data frame
x     #variable containing values for drawing.
label    #variable specifying the label of each slice.
lab.pos    #character specifying the position for labels. Allowed values are "out" (for outside) or "in" (for inside).
lab.adjust    #numeric value, used to adjust label position when lab.pos = "in". Increase or decrease this value to see the effect.
lab.font    #a vector of length 3 indicating respectively the size (e.g.: 14), the style (e.g.: "plain", "bold", "italic", "bold.italic") and the color (e.g.: "red") of label font. For example lab.font= c(4, "bold", "red").
color, fill    #outline and fill colors.
palette    #the color palette to be used for coloring or filling by groups. Allowed values include "grey" for grey color palettes; brewer palettes e.g. "RdBu", "Blues", ...; or custom color palette e.g. c("blue", "red"); and scientific journal palettes from ggsci R package, e.g.: "npg", "aaas", "lancet", "jco", "ucscgb", "uchicago", "simpsons" and "rickandmorty".
size    #Numeric value (e.g.: size = 1). change the size of points and outlines.
ggtheme    #function, ggplot2 theme name. Default value is theme_pubr(). Allowed values include ggplot2 official themes: theme_gray(), theme_bw(), theme_minimal(), theme_classic(), theme_void()
...    #other arguments to be passed to be passed to ggpar().

使用示例:

Examples

# Data: Create some data
df <- data.frame(
  group = c("Male", "Female", "Child"),
  value = c(25, 25, 50))

head(df)
##    group value
## 1   Male    25
## 2 Female    25
## 3  Child    50
# Basic pie charts
p1 <- ggpie(df, "value", label = "group")
p1
p1
# Change color
# Change fill color by group
# set line color to white
# Use custom color palette
p2 <- ggpie(df, "value", label = "group",
      fill = "group", color = "white",
      palette = c("#00AFBB", "#E7B800", "#FC4E07") )
p2
p2
# Change label
# Show group names and value as labels
labs <- paste0(df$group, " (", df$value, "%)")
labs
## [1] "Male (25%)"   "Female (25%)" "Child (50%)"
p3 <- ggpie(df, "value", label = labs,
      fill = "group", color = "white",
      palette = c("#00AFBB", "#E7B800", "#FC4E07"))
p3
p3
# Change the position and font color of labels
p4 <- ggpie(df, "value", label = labs,
      lab.pos = "in", lab.font = "white",
      fill = "group", color = "white",
      palette = c("#00AFBB", "#E7B800", "#FC4E07"))
p4
p4

参考来源:

https://www.rdocumentation.org/packages/ggpubr/versions/0.1.4/topics/ggpie

sessionInfo()
## R version 3.5.1 (2018-07-02)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: OS X El Capitan 10.11.3
## 
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] zh_CN.UTF-8/zh_CN.UTF-8/zh_CN.UTF-8/C/zh_CN.UTF-8/zh_CN.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] bindrcpp_0.2.2   ggpubr_0.1.7.999 magrittr_1.5     ggplot2_3.0.0   
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.18     rstudioapi_0.7   bindr_0.1.1      knitr_1.20      
##  [5] tidyselect_0.2.4 munsell_0.5.0    colorspace_1.3-2 R6_2.2.2        
##  [9] rlang_0.2.2      stringr_1.3.1    plyr_1.8.4       dplyr_0.7.6     
## [13] tools_3.5.1      grid_3.5.1       gtable_0.2.0     withr_2.1.2     
## [17] htmltools_0.3.6  assertthat_0.2.0 yaml_2.2.0       lazyeval_0.2.1  
## [21] rprojroot_1.3-2  digest_0.6.16    tibble_1.4.2     crayon_1.3.4    
## [25] purrr_0.2.5      glue_1.3.0       evaluate_0.11    rmarkdown_1.10  
## [29] labeling_0.3     stringi_1.2.4    compiler_3.5.1   pillar_1.3.0    
## [33] scales_1.0.0     backports_1.1.2  pkgconfig_2.0.2

你可能感兴趣的:(ggpubr包系列学习教程(十四))