This makes the colour proportion summaries look more pretty with
similar colours grouped together
I have tried other variations such as ordering by hue or
lightness/darkness but nothing looked as good
library(TSP)
#> Warning: package 'TSP' was built under R version 3.6.3# Unique colours in reduced colour imageunique_cols<- unique(i$col)
# Return a vector from the TSP code that allows me to sort the colours
set.seed(1)
col_order<-unique_cols %>%
col2rgb() %>%
t() %>%
dist() %>%
as.TSP() %>%
solve_TSP(control=list(repetitions=1000))
#> Warning: executing %dopar% sequentially: no parallel backend registered# Turn colour into factor with levels TSP orderi<-i %>% mutate(col=factor(col, levels=unique_cols[col_order])) %>% as_tibble()
Define grid page and dimensions for the different sections and draw
all components
# Define proportional sizes for the main image, the buffer between image and # summary and the summary panelswidth<-0.75buffer<-0.02vertical_strip<-0.1horizontal_strip<-0.1# Define the new page and the grid panel viewport
grid.newpage()
pushViewport(
viewport(
width= unit(1, "snpc"),
height= unit(1, "snpc"),
layout=
grid.layout(nrow=3,
ncol=3,
widths= unit(c(ifelse(w>=h, width, width/asp),
buffer,
vertical_strip),
"snpc"),
heights= unit(c(ifelse(w>=h, width*asp, width),
buffer,
horizontal_strip),
"snpc"))))
# Draw the image
pushViewport(viewport(layout.pos.row=1, layout.pos.col=1))
grid.draw(ggplotGrob(main))
grid.rect(gp= gpar(fill=NA, col=1))
popViewport()
# Draw the horizontal summaries on the right
pushViewport(viewport(layout.pos.row=1, layout.pos.col=3))
grid.draw(ggplotGrob(ybar))
grid.rect(gp= gpar(fill=NA, col=1))
popViewport()
# Draw the vertical summary at the bottom
pushViewport(viewport(layout.pos.row=3, layout.pos.col=1))
grid.draw(ggplotGrob(xbar))
grid.rect(gp= gpar(fill=NA, col=1))
popViewport()