ggplot绘制饼图
ggplot, facet, piechart: placing text in the middle of pie chart slices
基本饼图并添加数字
dat = read.table(text = "Channel Volume Cnt
AGENT high 8344
AGENT medium 5448
AGENT low 23823
KIOSK high 19275
KIOSK medium 13554
KIOSK low 38293", header=TRUE)
ggplot(data = dat, aes(x = 0, y = Cnt, fill = Volume)) +
geom_bar(stat = "identity") +
geom_text(aes(label = Cnt), position = position_stack(vjust = 0.5)) +
scale_x_continuous(expand = c(0,0)) +
coord_polar(theta = "y") +
facet_wrap(Channel ~ .)
因为两个列总和不等,因此会出现一个pie图不完全(无法通过设置scale='free'解决,因为会报错),
解决:(只对facet_wrap有用)
Awkward interaction between faceting, polar coordinates, and free scales
cp <- coord_polar(theta = "y")
cp$is_free <- function() TRUE
ggplot(data = dat, aes(x = 0, y = Cnt, fill = Volume)) +
geom_bar(stat = "identity") +
geom_text(aes(label = Cnt), position = position_stack(vjust = 0.5)) +
scale_x_continuous(expand = c(0,0)) +
cp+
facet_wrap(Channel ~ ., scales = "free")
中间空心的饼图
## 方法一:扩大x轴的范围:lims(x = c(-1, 0.5))
## tips:扩大范围的需要根据不同的数据集修改,下限扩大越多,空心部分越大,上限扩大越多,整个饼图在画布的占比越小
cp <- coord_polar(theta = "y")
cp$is_free <- function() TRUE
ggplot(data = dat, aes(x = 0, y = Cnt, fill = Volume)) +
geom_bar(stat = "identity") +
geom_text(aes(label = Cnt), position = position_stack(vjust = 0.5)) +
cp+
lims(x = c(-1,0.5)) +
facet_wrap(Channel ~ ., scales = "free")
## 方法二:再添加一条bar
## tips:新加列的x轴位置越低,空心越大,x轴位置不能低于原图图形x轴下限,否则不会造成空心
cp <- coord_polar(theta = "y")
cp$is_free <- function() TRUE
ggplot(data = dat, aes(x = 0, y = Cnt, fill = Volume)) +
geom_bar(stat = "identity") +
geom_text(aes(label = Cnt), position = position_stack(vjust = 0.5)) +
cp+
geom_col(aes(x=-1,y=0))+
facet_wrap(Channel ~ ., scales = "free")
添加比例
## 使用geom_text
cp <- coord_polar(theta = "y")
cp$is_free <- function() TRUE
ggplot(data = dat, aes(x = 0, y = Cnt, fill = Volume)) +
geom_bar(stat = "identity") +
geom_text(aes(label = scales::percent(Cnt/sum(Cnt))), position = position_stack(vjust = 0.5)) +
cp+
geom_col(aes(x=-1,y=0))+
facet_wrap(Channel ~ ., scales = "free")
添加文本和比例
## 使用geom_text
cp <- coord_polar(theta = "y")
cp$is_free <- function() TRUE
ggplot(data = dat, aes(x = 0, y = Cnt, fill = Volume)) +
geom_bar(stat = "identity") +
geom_text(aes(label = paste0(Channel,'\n',Volume ,'\n',scales::percent(Cnt/sum(Cnt)))), position = position_stack(vjust = 0.5)) +
cp+
geom_col(aes(x=-1,y=0))+
facet_wrap(Channel ~ ., scales = "free")
中间有两个圈的饼图
cp <- coord_polar(theta = "y")
cp$is_free <- function() TRUE
ggplot(data = dat, aes(x = Channel, y = Cnt, fill = Volume)) +
geom_bar(stat = "identity") +
geom_text(aes(label = paste0(Channel,'\n',Volume ,'\n',scales::percent(Cnt/sum(Cnt)))), position = position_stack(vjust = 0.5)) +
cp+
geom_col(aes(x=-0.5,y=0))+
facet_wrap(. ~ ., scales = "free")
## 换一个数据集,中间能对齐的两个圈
pie_data <- data.frame(category=c('A','B','C','A','B','C'),
year=c(2020,2020,2020,2021,2021,2021),
sales=c(40,30,20,20,30,40))
ggplot(data=pie_data,aes(x=factor(year),y=sales,fill=category))+
geom_col(width=1,color='white')+
geom_text(aes(label = scales::percent(sales/sum(sales))), position = position_stack(vjust = 0.5)) +
coord_polar(theta = 'y')+
geom_col(aes(x=0,y=0))+
theme_void()
ggpubr绘制饼图
与ggplot2类似,这里介绍下空心饼图与实心饼图拼图
pie_data <- data.frame(category=c('A','B','C','A','B','C'),
year=c(2020,2020,2020,2021,2021,2021),
sales=c(40,30,20,20,30,40))