Skip to content

Instantly share code, notes, and snippets.

@TonyLadson TonyLadson/WP_treemap.R
Last active Sep 3, 2019

Embed
What would you like to do?
Making a treemap for hydrological purposes. See the blog https://tonyladson.wordpress.com/2019/09/02/using-a-treemap-to-show-gauged-areas/
library(treemap)
library(tidyverse)
wp <- tribble(~catchment, ~sub_catchment, ~area,
'Bunyip', 'Gauged', 697,
'Bunyip', 'Ungauged', 479,
'Lang Lang', 'Gauged', 272,
'Lang Lang', 'Ungauged', 151,
'Bass', 'Gauged', 233,
'Bass', 'Ungauged', 33,
'Other', 'Gauged', 0,
'Other','Ungauged', 1460,
'Cardinia', 'Gauged', 117,
'Cardinia', 'Ungauged', 281)
# summary data
wp %>%
summarise(area_sum = sum(area))
wp %>%
group_by(sub_catchment) %>%
summarise(area_sum = sum(area))
wp <- wp %>%
group_by(catchment) %>%
mutate(area_tot = sum(area)) # we'll use total area of each catchment to order the treemap
# Much of this is taken from the R graph gallery
# https://www.r-graph-gallery.com/236-custom-your-treemap.html
treemap(dtf = wp, # Data frame for plotting
index=c("catchment","sub_catchment"), # sub-catchments will be grouped within catchments
vSize="area", # plot by area
type="index",
title = "",
algorithm = "pivotSize", # using the pivotSize algorither means we can force plotting in a particular order
sortID = "area_tot", # Order is based on catchment area
fontsize.labels=c(30,15), # size of labels. Give the size per level of aggregation: size for group, size for subgroup, sub-subgroups...
fontcolor.labels=c("white","white"), # Color of labels
fontface.labels=c(2,1), # Font of labels: 1,2,3,4 for normal, bold, italic, bold-italic...
bg.labels=c("transparent"), # Background color of labels
align.labels=list(
c("center", "center"),
c("centre", "top")
), # Where to place labels in the rectangle?
overlap.labels=0.5, # number between 0 and 1 that determines the tolerance of the overlap between labels. 0 means that labels of lower levels are not printed if higher level labels overlap, 1 means that labels are always printed. In-between values, for instance the default value .5, means that lower level labels are printed if other labels do not overlap with more than .5 times their area size.
inflate.labels=F, # If true, labels are bigger when rectangle is bigger,
border.lwds = c(5,1) # Width of border around groups and subgroups
)
########## Gauged areas from the four catchments v the rest
wp %>%
filter(sub_catchment == 'Gauged')
wp %>%
filter(sub_catchment == 'Ungauged')
wp %>%
group_by(sub_catchment) %>%
summarise(area_sum = sum(area))
# # A tibble: 2 x 2
# sub_catchment area_sum
# <chr> <dbl>
# 1 Gauged 1319
# 2 Ungauged 2404
wp <- wp %>%
filter(sub_catchment == 'Gauged') %>%
select(catchment, area) %>%
mutate(area = if_else(catchment == 'Other', 2404, area)) %>%
mutate(catchment = if_else(catchment == "Other", "Ungauged", catchment))
wp %>%
summarise(area_sum = sum(area))
# Much of this is taken from the R graph gallery
# https://www.r-graph-gallery.com/236-custom-your-treemap.html
treemap(dtf = wp, # Data frame for plotting
index=c("catchment"), # sub-catchments will be grouped within catchments
vSize="area", # plot by area
type="index",
title = "",
algorithm = "pivotSize", # using the pivotSize algorither means we can force plotting in a particular order
sortID = "area", # Order is based on catchment area
fontsize.labels=c(30,15), # size of labels. Give the size per level of aggregation: size for group, size for subgroup, sub-subgroups...
fontcolor.labels=c("white","white"), # Color of labels
fontface.labels=c(2,1), # Font of labels: 1,2,3,4 for normal, bold, italic, bold-italic...
bg.labels=c("transparent"), # Background color of labels
align.labels=list(
c("center", "center"),
c("centre", "top")
), # Where to place labels in the rectangle?
overlap.labels=0.5, # number between 0 and 1 that determines the tolerance of the overlap between labels. 0 means that labels of lower levels are not printed if higher level labels overlap, 1 means that labels are always printed. In-between values, for instance the default value .5, means that lower level labels are printed if other labels do not overlap with more than .5 times their area size.
inflate.labels=F, # If true, labels are bigger when rectangle is bigger,
border.lwds = c(5,1) # Width of border around groups and subgroups
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.