geom_pie | ggfan |ggplot examples

geom_pie | ggfan |ggplot examples

Install

##install.packages("ggfan") # or
##devtools::install_github("jasonhilton/ggfan")

Quick Start

Reference:

library(ggplot2)

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
library(ggplot2)
library(ggthemes)

pie <- ggplot(df, aes(x="", y=value, fill=group))+
geom_bar(width = 1, stat = "identity") + coord_polar("y", start=0) + theme_map()
pie
ggsave("pie.png", w=3.17, h=2.92)

aQ18Wd.md.png

pie +  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
ggsave('pie2.png', wi=4.6, hei=3.76)

pie2

pie +  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9")) +  geom_text(aes(y = value/3 + c(0, cumsum(value)[-length(value)]),  
label = paste(value*100/sum(value),"%", sep='')))

pie3

Ring pie

pie +  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9")) +
geom_text(aes(y = value/3 + c(0, cumsum(value)[-length(value)]),
label = paste(value*100/sum(value),"%", sep='')))+
expand_limits(x=c(-1,1))

pie4

Multi-layer

TB = rbind(df,df)
TB$Sub = c(10,15,15,15,10,35)
TB$Group = c(rep("A",3),rep("B",3))

ggplot(TB) + geom_bar(data=TB[1:3,],aes(x="A",y=value[1:3], fill=group),stat = 'identity') +
geom_bar(aes(x="B",y =Sub, fill=Group),stat='identity')+
coord_polar('y')

Rose Plot

Reference: 九茶

library(ggplot2)
dt = data.frame(A = c(2, 7, 4, 10, 1), B = c('B','A','C','D','E'))
ggplot(dt, aes(x = B, y = log(1+A), fill = B)) +
geom_bar(stat = "identity", alpha = 0.7) +
coord_polar() + theme_light()

rose

pie 3D

For pie3D, you’d like to install plotrix first.

install.packages("plotrix")
library("plotrix")

x <-c(10,20,30,40,50)
label <-c("Alabama", "Alaska", "Arizona","Arkansas", "California")
pie3D(x,labels=label,explode=0.1,main="PieChart of Countries ")

pie5

More

pie

author: 陈娜

Pie Pie3D Fan
pie pie3D plotrix

© 陈娜 2015

SALVAGING THE PIE

img

geom_pie | ggfan |ggplot examples

https://karobben.github.io/2020/06/18/R/geom_pie/

Author

Karobben

Posted on

2020-06-18

Updated on

2024-01-22

Licensed under

Comments