Skip to content

Instantly share code, notes, and snippets.

@shahadathossain91
Last active November 14, 2016 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shahadathossain91/fb8352cdb9bd83ceabfa4964e37fd1e6 to your computer and use it in GitHub Desktop.
Save shahadathossain91/fb8352cdb9bd83ceabfa4964e37fd1e6 to your computer and use it in GitHub Desktop.
Treemap function in R
#########################################
### 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")
@shahadathossain91
Copy link
Author

region_treemap

@rfaridi
Copy link

rfaridi commented Nov 14, 2016

Looks pretty good!

@rfaridi
Copy link

rfaridi commented Nov 14, 2016

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