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?
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With