Created
August 15, 2016 22:26
-
-
Save RobertMyles/c4a8939180cff6620b045396ecc1146b to your computer and use it in GitHub Desktop.
My attempt to reproduce this fantastic chart: http://stackoverflow.com/questions/17069436/hierarchical-multilevel-pie-chart/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(scales) | |
library(tidyr) | |
library(dplyr) | |
rm(list=ls()) | |
df <- structure(list(Animal = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), | |
.Label = c("Buffalo", "Goat", "Sheep"), | |
class = "factor"), | |
Texture = structure(c(4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 2L, 1L, 1L, 4L, 3L, 4L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), | |
.Label = c("Hard", "Semi-Hard", "Semi-Soft", "Soft"), | |
class = "factor"), | |
Name = structure(c(16L, 9L, 3L, 21L, 5L, 4L, 10L, 2L, 12L, 11L, 8L, 14L, 1L, 7L, 22L, 15L, 6L, 20L, 18L, 17L, 19L, 13L), | |
.Label = c("Buffalo Blue", "Charolais", "Chevre Bucheron", "Clochette", "Crottin de Chavignol", "Feta", "Fleur du Maquis", "Garrotxa", "Golden Cross", "Humboldt Fog", "Idaho Goatster", "Majorero", "Manchego", "Mozzarella di Bufala Campana", "Ossau-Iraty", "Pantysgawn", "Pecorino Romano", "Pecorino Sardo", "Roncal", "Roquefort", "Sainte-Maure de Touraine", "Yorkshire Blue"), | |
class = "factor")), | |
.Names = c("Animal", "Texture", "Name"), | |
class = "data.frame", row.names = c(NA, -22L)) | |
# One version with sunburst: | |
#devtools::install_github("timelyportfolio/sunburstR") | |
library(sunburstR) | |
df1 <- df %>% | |
group_by(Animal) %>% | |
unite(col=Type, Animal:Name, sep = "-", remove=T) | |
df1$Type <- gsub(" ", "", df1$Type) | |
df1$Index <- 1 | |
sunburst(df1) | |
########################### | |
# ggplot | |
df1 <- df %>% | |
mutate(Colour = ifelse(.$Animal == "Goat", "#CD9B1D", ifelse(.$Animal == "Sheep", "#EEC900", "#FFD700"))) %>% | |
mutate(Index = 1) %>% | |
group_by(Animal) | |
First <- ggplot(df1) + geom_bar(aes(x=1, y=Animal, fill=Animal, label = Animal), position='stack', stat='identity', size=0.15) + theme(panel.grid = element_blank(), axis.title=element_blank(), legend.position="none", axis.ticks=element_blank(), axis.text = element_blank()) | |
First + coord_polar('y') | |
Second <- First + geom_bar(data=df1, aes(x=2, y=Animal, fill=Texture, group=Animal), position='stack', stat='identity', size=0.15, colour = "black") + scale_color_brewer(palette = "YlOrBr")+ scale_fill_brewer(palette = "YlOrBr") + theme(axis.title=element_blank(), legend.position="none", axis.ticks=element_blank(), axis.text = element_blank()) | |
Second + coord_polar('y') | |
Third <- Second + geom_bar(data=df1, aes(x=3, y=Animal, fill=Name), position='stack', stat='identity', size=0.15, colour = "black") + scale_fill_manual(values = c("#EEC900", "#FFD700", "#CD9B1D", "#FFD700", "#DAA520", "#EEB422", "#FFC125", "#8B6914", "#EEC591", "#FFF8DC", "#EEDFCC", "#FFFAF0", "#EEC900", "#FFD700", "#CDAD00", "#FFF68F", "#FFEC8B", "#FAFAD2", "#FFFFE0", "#CD853F", "#EED8AE", "#F5DEB3", "#FFFFFF", "#FFFACD", "#D9D9D9", "#EE7600", "#FF7F00", "#FFB90F", "#FFFFFF")) + theme(axis.title=element_blank(), legend.position="none", axis.ticks=element_blank(), axis.text.y = element_blank(), panel.background = element_rect(fill = "black")) | |
Third + coord_polar('y') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment