Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make relative tile sizes in ggplot2 with geom_tile?

Tags:

r

ggplot2

I'm trying to make a visualization of sets of colors and their complements. I want to use geom_tile to place them side by side. My issue is that some target colors have multiple complements, so I need to be able to split the complement tile evenly between the colors.

My test dataset is this:

test_pair = data.frame(pair_number = c(1, 1, 2, 2, 3, 3, 3), 
                       color = c('#5f75e6', '#e6d05f', '#5f75e6', '#5fb9e6', '#5f75e6', '#b9e65f', '#e68d5f'),
                       group = c('target', 'comp', 'target', 'comp', 'target', 'comp', 'comp'),
                       bin_width = c(1, 1, 1, 1, 1, 0.5, 0.5))

And my plot code:

ggplot(test_pair, aes(x = factor(group), y = factor(pair_number), width = bin_width)) +
       geom_tile(aes(fill = color)) +
       scale_fill_identity() +
       scale_x_discrete('', expand = c(0, 0)) +
       scale_y_discrete('', expand = c(0, 0)) + 
       theme_bw() +
       theme(line = element_blank(),
            panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            panel.border = element_blank(),
            plot.background = element_rect(fill = '#c4a879'),
            axis.ticks = element_blank(),
            axis.text.y = element_text(size = 14),
            axis.text.x = element_text(size = 14),
            axis.title.y = element_text(color = hex))

The plot does split one of the complement colors in half, but centers that tile and does not plot the second complement color. I want the plot to print the two complements side by side without having to specify break-points along x like in the geom_tile documentation. Anyone know how to do this?

ggplot with geom_tile not behaving as desired.

like image 964
Erin Shellman Avatar asked Jan 09 '14 19:01

Erin Shellman


1 Answers

The two colours are there. It's just that the x and y coordinates and the widths for the two tiles (colours) are the same; and thus, the two tiles (colours) overlap exactly. To see that that is the case, adjust the width of the underlying colour; for instance, change the bin_width line in your test_pair data frame to: bin_width = c(1, 1, 1, 1, 1, 0.75, 0.5)), then run the ggplot command.

To fix your problem, offset the x-positions of the two tiles. One way to do this is to use a numeric scale on the x-axis, then add the appropriate breaks and labels in scale_x_continuous(). But how to do the offsetting without specifying break points? I'm not sure.

test_pair = data.frame(pair_number = c(1, 1, 2, 2, 3, 3, 3), 
                       color = c('#5f75e6', '#e6d05f', '#5f75e6', '#5fb9e6', '#5f75e6', '#b9e65f', '#e68d5f'),
                       group = c(2, 1, 2, 1, 2, .75, 1.25),
                       bin_width = c(1, 1, 1, 1, 1, .5, .5))

ggplot(test_pair, aes(x = group, y = factor(pair_number), width = bin_width)) +
       geom_tile(aes(fill = color)) +
       scale_fill_identity() +
       scale_x_continuous('', breaks = c(1,2), labels = c("comp", "target"), expand = c(0, 0)) +
       scale_y_discrete('', expand = c(0, 0)) + 
       theme_bw()

enter image description here

like image 55
Sandy Muspratt Avatar answered Sep 27 '22 16:09

Sandy Muspratt