Last active
November 14, 2016 12:04
-
-
Save shahadathossain91/fb8352cdb9bd83ceabfa4964e37fd1e6 to your computer and use it in GitHub Desktop.
Treemap function in R
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
######################################### | |
### quick example with Regional distribution | |
######################################### | |
library(treemap) | |
# df <- bc2_BC | |
# var <- "DistName" | |
ggTreeMap <- function(df=bc2_BC,var,pngname="tree"){ | |
tab.df <- as.data.frame(table(df[[var]])) | |
tab.df$val = round(tab.df$Freq / sum(tab.df$Freq),2)*100 | |
tab.df$label <- paste(paste(paste(tab.df$Var1, tab.df$Freq, sep = ", "), | |
tab.df$val, sep = ", "),"%", sep = "") | |
file <- paste(paste("./Pictures/",pngname,sep = ""),".png",sep = "") | |
png(filename=file,width=450, height=250) | |
treemap(tab.df, | |
index = c("label"), | |
vSize = "val", | |
vColor = "Freq", | |
type = "value", | |
palette = "RdBu") | |
dev.off() | |
} | |
ggTreeMap(var = "DistName",pngname = "region_treemap") | |
Author
shahadathossain91
commented
Nov 14, 2016
Looks pretty good!
By the way, don't add the image as PNG, rather as a file in the gist.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment