2

I want to modify the following ggplot (taken and modified from this question), so that it shows clear division lines between the squares.

I have this:

enter image description here

I want this:

enter image description here

library(ggplot2)
library(reshape2)


     mm <- structure(c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE,
                       FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE,
                       FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE,
                       FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE,
                       FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE,
                       FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
                       TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
                       TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
                       TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
                       TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
                       TRUE, TRUE, TRUE), .Dim = c(10L, 10L), .Dimnames = list(NULL,
                                                                               c("n1", "n2", "n3", "n4", "n5", "n1.1", "n2.1", "n3.1", "n4.1",
                                                                                 "n5.1")))

    melted <- melt(mm)
    p <- ggplot(melted, aes(x = Var2, y = Var1, fill = value)) +
      geom_tile() +
      geom_raster(aes(fill=value)) +
       scale_fill_manual(values = c("white", "black")) +
       theme_bw() +
       theme(legend.position = "none") +
      theme(axis.title.x=element_blank(),
            axis.text.x=element_blank(),
            axis.ticks.x=element_blank(),
            axis.title.y=element_blank(),
            axis.text.y=element_blank(),
            axis.ticks.y=element_blank())

    print(p)
andandandand
  • 21,946
  • 60
  • 170
  • 271

2 Answers2

2

You can get most of the way there by mapping color to the values in addition to fill.

The only issue is that the outer borders of black squares are white. To remove this, you can modify the axis expansion so as the panel border creates a black line.

mm %>% 
  melt() %>% 
  ggplot(aes(Var2, Var1)) + 
    geom_tile(aes(fill = value, 
                  color = value)) + 
    coord_equal() + 
    scale_fill_manual(values = c("black", "white")) + 
    scale_color_manual(values = c("white", "black")) + 
    theme_bw() +
    theme(axis.title = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank(),
          panel.grid = element_blank()) + 
    guides(fill = FALSE, color = FALSE) + 
    scale_x_discrete(expand = c(0,0)) + 
    scale_y_discrete(expand = c(0,0))

enter image description here

neilfws
  • 32,751
  • 5
  • 50
  • 63
2

I like @neilfws answer. However, I think that the appearance of different color borders around the white or black tiles is actually an illusion caused by the way the eye sees contrast. So, here is an alternative - to just use gray borders around all tiles:

ggplot(melted, aes(x = Var2, y = Var1, fill = value)) +
  geom_tile(color='gray50') +
  scale_fill_manual(values = c("white", "black")) +
  theme_bw() +
  theme(legend.position = "none",
        axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        panel.grid = element_blank(),
        panel.border = element_blank())

enter image description here

dww
  • 30,425
  • 5
  • 68
  • 111