Skip to content

Instantly share code, notes, and snippets.

@lovrot
Last active November 10, 2015 08:51
Show Gist options
  • Save lovrot/2ed47338c24f3cb8d7ef to your computer and use it in GitHub Desktop.
Save lovrot/2ed47338c24f3cb8d7ef to your computer and use it in GitHub Desktop.
[Humour] Pie chart pyramid
## A paraphrase of http://flowingdata.com/2014/12/01/pie-chart-pyramid/
## See also https://github.com/lovrot/pie-chart-pyramid
library(ggplot2)
theme_set(theme_bw())
palette_part <- c(
"Sky"="deepskyblue3",
"Sunny side of pyramid"="yellow1",
"Shady side of pyramid"="yellow3")
h <- 1.1
alpha_degrees <- 20 # between 0 and 90
alpha <- alpha_degrees/180*pi
x_1 <- cos(pi/4 - alpha)
x_2 <- sin(pi/4 - alpha)
beta <- c(atan(x_1/h), atan(x_2/h))
binoculars <- data.frame(
part=factor(
c("Sky", "Sunny side of pyramid",
"Shady side of pyramid", "Sky"),
levels=c("Sky", "Sunny side of pyramid",
"Shady side of pyramid")),
radians=c(pi - beta[1], beta[1] + beta[2],
beta[1] - beta[2], pi - beta[1]))
gg <- ggplot(data=binoculars,
aes(x=factor(1), y=radians, fill=part)) +
geom_bar(width=1, stat="identity", position="fill") +
scale_x_discrete(breaks=NULL, name="") +
scale_y_continuous(breaks=NULL, name="") +
scale_fill_manual(values=palette_part, name="") +
theme(
panel.grid.major.x=element_blank(),
panel.border=element_blank()) +
coord_polar(theta="y", direction=-1)
plot(gg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment