testmap <- read.table("R/test",header=TRUE,sep="\t")
testmap
Models Group Brands Presence Country
1 Xperia Z5 A Sony 1 Japan
2 Galaxy S20 A Samsung 1 Korea
3 Xperia XZ B Sony 1 Japan
4 Galaxy Note FE B Samsung 0 Korea
5 Nord A OnePlus 1 China
I obtained the presence/absence of the models plotted based on the country with the following code.
ggplot(testmap, aes(x=Country, y=Models, fill=Presence))+
geom_tile() + xlab(label="Country")+ ylab(label="Models")+
scale_fill_gradient(low="white",high="black")+
theme_bw() + theme(legend.position="none")
I would like to group the models according to their corresponding brands. (Technically, draw a square bracket to show each of the brand group.)
I tried with facet_grid as suggested in previous post, but it grouped up the Models in the plot area as well. I would like to keep the whole plot area as one. Is it possible to generate the graph exactly as above but just using square brackets to group them up according to the brand?
Attempt with facet_grid:
ggplot(testmap, aes(x=Country, y=Models, fill=Presence))+
geom_tile() + xlab(label="Country")+ ylab(label="Models")+
scale_fill_gradient(low="white",high="black")+
facet_grid(Brands ~., scales="free", space="free_y",switch="y")+
theme_bw() + theme(strip.placement="outside", strip.background=element_rect(fill="white"), axis.title=element_blank(),legend.position="none",strip.text.y.left = element_text(angle = 0))
Thanks to the help from Henrik and Sinh Nguyen, I managed to obtain what I would like to have -- specifically: square bracket to group the y-axis label.
library(ggplot2)
library(grid)
library(pBrackets)
ggplot(testmap, aes(x=Country, y=Models, fill=Presence))+
geom_tile() + xlab(label="Country")+ ylab(label="Models\n\n\n\n\n\n\n\n")+
scale_fill_gradient(low="white",high="black")+
theme_bw() + theme(legend.position="none")
grid.locator(unit="native")
grid.brackets(140, 198, 140, 78, lwd=1, ticks=NA, type = 4)
grid.brackets(140, 321, 140, 320, lwd=0.5, ticks=NA, type = 4)
grid.brackets(140, 556, 140, 436, lwd=1, ticks=NA, type = 4)
grid.text(x=unit(35,'native'), y=unit(140,'native'),
label=expression(paste('Sony'),'type=4'), hjust = 0, vjust=0)
grid.text(x=unit(35,'native'), y=unit(325,'native'),
label=expression(paste('OnePlus'),'type=4'), hjust = 0, vjust=0)
grid.text(x=unit(35,'native'), y=unit(496,'native'),
label=expression(paste('Samsung'),'type=4'), hjust = 0, vjust=0)
I used grid.locator(unit="native")
to estimate the position. It was "estimated" as the brackets appeared to be slightly away from intended position.
Also, h
in grid.brackets
can be used to adjust the horizontal length of the brackets.
There are more flexibility on the type and adjustment for the brackets and font, which is available in the manual: https://cran.r-project.org/web/packages/pBrackets/pBrackets.pdf
It wasn't perfect but I guess at least it solved the problem. But still welcome any suggestion to improve on the visualization.
Hope this answer will be helpful to someone with the same problem!
Thanks!