ggpie饼图绘制(ggpubr)
2021-03-30 本文已影响0人
Zee_李海海
前段时间,使用ggplot2绘制饼图时,不停调整参数,图例,浪费许多时间。这几天刚好看到ggpubr包中有绘制饼图的函数ggpie,就使用自己的数据去测试一下。
ggpie()
Usege:
ggpie(data,x,label = x,lab.pos = c("out", "in"),
lab.adjust = 0,lab.font = c(4, "bold", "black"),
font.family = "",color = "black",fill = "white",
palette = NULL,size = NULL,ggtheme = theme_pubr(),
...
)
#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").
font.family, character vector specifying font family.
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().
Example
#测试数据集
data<-read.csv(table.csv)
head(data)
Var1 Freq Value
1 ACTH cells 473 0.07727496
2 EC 464 0.07580461
3 FSC 453 0.07400752
4 Generative-cells 131 0.02140173
5 GH cells 643 0.10504819
6 Gonadotropes 288 0.04705114
7 Melanotrophs 573 0.09361215
8 Mult 1 529 0.08642379
9 Mult 2 531 0.08675053
10 Mult 3 115 0.01878778
11 Pericytes 84 0.01372325
12 PRL cells 1376 0.22479987
13 WBC (C1qa) 461 0.07531449
#第一列是样本类型,第二列是每个样本的数目,第三列是占比
1
library(ggplot2)
library(ggpubr)
data$Value1<-paste0(round(data$Value*100,2),"%")
data$Vare<-paste0(data$Var1,"(",data$Value1,")")
p<-ggpie(data,"Freq",label=data$Value1,
fill="Vare",color="white",lab.font = c(3,"black"),lab.pos="out")
+theme(legend.position = "right",legend.text = element_text(size=8))
+labs(fill="Cell Type") #lab.pos=c("out","in")
p
pie01.png
2
p<-ggpie(data,"Freq",label=data$Value1,fill="Vare",
color="white",lab.font = c(2,"black"),lab.pos="in",
palette=c("#DC143C","#0000FF","#20B2AA","#FFA500","#9370DB","#98FB98","#F08080","#1E90FF","#7CFC00","#FFFF00","#808000","#FF00FF","#FA8072"))
+theme(legend.position = "right",legend.text = element_text(size=8,color="black"))
+labs(fill="Cell Type") #palette :可以更改饼图的配色,label可以换成其他对象
p
pie02.png