rm(list = ls())
library(ggplot2)
library(ggforce)
dat = data.frame(count=c(30,15, 10), category=c("A", "B", "C"),stringsAsFactors = F)
dat
## count category
## 1 30 A
## 2 15 B
## 3 10 C
dat$fraction = dat$count / sum(dat$count)
dat = dat[order(dat$fraction), ]
dat$ymax = cumsum(dat$fraction)-0.01
dat$ymin = c(0, head(dat$ymax, n=-1))+0.01
dat$all_=length(unique(dat$category))
dat$x1=dat$all_-(1:nrow(dat))*0.5+1
dat$x2=dat$all_-(1:nrow(dat))*0.5+2
######################################
#install.packages("ggplot2")
p1 <- ggplot()+aes(ymin=0)+geom_rect(data=dat,aes(fill=category,ymax=ymax, ymin=ymin, xmax=x1, xmin=x2),color = "white")+
ylim(0,1) +
xlim(c(0,3+length(unique(dat$category))))
p1

############
p1 +
coord_polar(theta="y", direction = -1) +
theme_bw() +
theme(panel.grid=element_blank()) +
theme(axis.text=element_blank()) +
theme(axis.ticks=element_blank()) +
theme(axis.title.x = element_blank()) +
theme(axis.title.y = element_blank()) +
theme(panel.border = element_blank())

################################
dat$x1 <- 0
#dat$x2 <- 4
ggplot()+aes(ymin=0)+geom_rect(data=dat,aes(fill=category,ymax=ymax, ymin=ymin, xmax=x1, xmin=x2),color = "white")+
ylim(0,1) +
xlim(c(0,3+length(unique(dat$category)))) +
coord_polar(theta="y", direction = -1) +
theme_bw() +
theme(panel.grid=element_blank()) +
theme(axis.text=element_blank()) +
theme(axis.ticks=element_blank()) +
theme(axis.title.x = element_blank()) +
theme(axis.title.y = element_blank()) +
theme(panel.border = element_blank())

################output
ggsave(paste0(Sys.Date(),"-point_pie.tiff"), plot = last_plot(),
device = "tiff", path = NULL,
scale = 1, width = 35, height = 15, units ="cm",dpi = 300, limitsize = TRUE)
#ref https://stackoverflow.com/questions/35175606/how-to-explode-donut-chart-slices-using-r-ggplot2
############################################method_2
# If you know the angle spans to plot it is easy
arcs <- data.frame(
start = seq(0, 2 * pi, length.out = 7)[-7],
end = seq(0, 2 * pi, length.out = 7)[-1],
r = rep(1:2, 3)
)
arcs
## start end r
## 1 0.000000 1.047198 1
## 2 1.047198 2.094395 2
## 3 2.094395 3.141593 1
## 4 3.141593 4.188790 2
## 5 4.188790 5.235988 1
## 6 5.235988 6.283185 2
# Behold the arcs
ggplot(arcs) +
geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = r-1, r = r, start = start,
end = end, fill = r))

ggplot(arcs) +
geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = r - 1, r = r, start = start,
end = end, fill = r), radius = unit(3, 'mm'))

###############################################
# If you got values for a pie chart, use stat_pie
states <- c(
'eaten', "eaten but said you didn\'t", 'cat took it', 'for tonight',
'will decompose slowly'
)
pie <- data.frame(
state = factor(rep(states, 2), levels = states),
type = rep(c('Pie', 'Donut'), each = 5),
r0 = rep(c(0, 0.8), each = 5),
focus = rep(c(0.2, 0, 0, 0, 0), 2),
amount = c(4, 3, 1, 1.5, 6, 6, 1, 2, 3, 2)
)
pie
## state type r0 focus amount
## 1 eaten Pie 0.0 0.2 4.0
## 2 eaten but said you didn't Pie 0.0 0.0 3.0
## 3 cat took it Pie 0.0 0.0 1.0
## 4 for tonight Pie 0.0 0.0 1.5
## 5 will decompose slowly Pie 0.0 0.0 6.0
## 6 eaten Donut 0.8 0.2 6.0
## 7 eaten but said you didn't Donut 0.8 0.0 1.0
## 8 cat took it Donut 0.8 0.0 2.0
## 9 for tonight Donut 0.8 0.0 3.0
## 10 will decompose slowly Donut 0.8 0.0 2.0
# Look at the cakes
ggplot() + geom_arc_bar(aes(
x0 = 0, y0 = 0, r0 = r0, r = 1, amount = amount,
fill = state, explode = focus
),
data = pie, stat = 'pie'
) +
facet_wrap(~type, ncol = 1) +
coord_fixed() +
theme_no_axes() +
scale_fill_brewer('', type = 'qual')

#https://ggforce.data-imaginist.com/reference/geom_arc_bar.html
#ref https://cran.microsoft.com/snapshot/2019-03-14/web/packages/ggforce/vignettes/Visual_Guide.html