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))