I have a dataset like this
data=data.frame(Var1 = c('a', 'b'), Freq=c(31, 48))
And I want to make a ggplot pie chart. I do this as below
library(ggplot2)
tbl <- transform(data,
freq = cumsum(Freq),
perc = Freq/sum(Freq),
pos = (cumsum(Freq) - 0.5 * Freq) / sum(Freq))
ggplot(data=tbl, aes(x="", y=perc, fill = factor(Var1))) +
geom_bar(width = 0.8, stat="identity") +
coord_polar(theta="y",start = 0, direction = 1) +
geom_text(aes(label=paste(round(perc*100),"%",sep=""), y=pos),
color="grey20" ,size=15) +
scale_fill_manual(tbl$Var1,values = c("coral2", "red")) +
theme_bw() +
theme (panel.border = element_blank(),legend.title = element_blank(),
axis.ticks = element_blank(),axis.title.x = element_blank(),
axis.title.y = element_blank(), axis.text.x = element_blank(),
axis.line=element_blank(),panel.grid.major=element_blank(),
legend.background = element_rect(fill="transparent"), legend.position=c(0.5,0.1),
legend.direction="vertical", legend.text=element_text(size = 20))
As you can see text directions and graphics are different. And I use coord_polar(theta="y",start = 0, direction = 1) that direction = 1 means the clockwise direction. But actually direction of pies is anticlockwise. What I do wrong?


