1

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

testmap.jpg graph1

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))
web
  • 105
  • 9
  • 1
    [`pBrackets`](https://cran.r-project.org/web/packages/pBrackets/index.html) maybe. Some [examples on SO](https://stackoverflow.com/search?q=%5Br%5D+pBrackets) – Henrik Mar 12 '21 at 16:23
  • Hi @Henrik, do you have any idea on how to print the graph with brackets into file? ggsave doesn't seem to work here. – web Mar 14 '21 at 15:48
  • Perhaps you can try some of the "standard" methods, see e.g. [here](https://stackoverflow.com/a/7144203/1851712) and `?png`. – Henrik Mar 14 '21 at 16:22

2 Answers2

2

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

Graph1+

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!

web
  • 105
  • 9
1

Here is one try. Still figuring out how to add text to the y-Axis properly. Though this is very manual and hard to scales.

library(tidyverse)
library(grid)
library(pBrackets)

testmap <- tribble(
  ~Models, ~Group, ~Brands, ~Presence, ~Country,
  "Xperia Z5", "A", "Sony", 1, "Japan",
  "Galaxy S20", "A", "Samsung", 1, "Korea",
  "Xperia XZ", "B", "Sony", 1, "Japan",
  "Galaxy Note FE", "B", "Samsung", 0, "Korea",
  "Nord", "A", "OnePlus", 1, "China")
)
ggplot(testmap, aes(x=Country, y=Models, fill=Presence))+
  geom_tile() + xlab(label="Country")+ ylab(label="Models\n\n")+
  scale_fill_gradient(name="Presence of Models", low="white",high="black")+
  theme_bw() + theme(legend.position="none")

# using grid.locator to identify the coordinate for brackets
grid.locator(unit = "native")

# add bracket to plot area
grid.brackets(60, 134, 60, 32, type = 1)
grid.brackets(60, 233, 60, 157, type = 1)
grid.brackets(60, 353, 60, 247, type = 1)

Output:

enter image description here

Sinh Nguyen
  • 4,277
  • 3
  • 18
  • 26
  • Thanks for the help! I was exploring on the grid.locator too! Any idea how to reflect the bracket on the other side like yours? My bracket was facing away from the y-axis label. – web Mar 14 '21 at 06:11
  • 1
    It depend on the `y1` & `y2` you specify. If you finding they are facing the wrong side, the revserse their value. – Sinh Nguyen Mar 14 '21 at 06:52
  • BTW, do you have any idea on how to print the graph with brackets into file? ggsave doesn't seem to work here. – web Mar 14 '21 at 15:49