2

I am unable to move the x-axis labels, including tick marsk, in a geom_tile to the right end of each tile, I would also like to add the zero at the left end. I have tried breaks and labels but with no luck. Also using breaks and labels doesn't really work.

I am trying to achieve what is done in this answer but the solution suggested doens't work. How to force the x-axis tick marks to appear at the end of bar in heatmap graph?

my data

toplot=structure(list(year = c(125L, 250L, 375L, 500L, 625L, 750L, 875L, 1000L, 1125L, 1250L, 125L, 250L, 375L, 500L, 625L, 750L, 875L, 1000L, 1125L, 1250L, 125L, 250L, 375L, 500L, 625L, 750L, 875L, 1000L, 1125L, 1250L, 125L, 250L, 375L, 500L, 625L, 750L, 875L, 1000L, 1125L, 1250L, 125L, 250L, 375L, 500L, 625L, 750L, 875L, 1000L, 1125L, 1250L, 125L, 250L, 375L, 500L, 625L, 750L, 875L, 1000L, 1125L, 1250L, 125L, 250L, 375L, 500L, 625L, 750L, 875L, 1000L, 1125L, 1250L, 125L, 250L, 375L, 500L, 625L, 750L, 875L, 1000L, 1125L, 1250L), density = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("a", "b", "c", "d"), class = "factor"), value = c(-0.04, 0.07, 0.01, -0.06, -0.05, 0.04, 0.13, 0.17, 0.19, 0.19, 0.02, 0.27, 0.3, 0.3, 0.36, 0.35, 0.38, 0.5, 0.67, 0.77, -0.14, -0.08, -0.05, 0.18, 0.37, 0.4, 0.41, 0.44, 0.47, 0.73, -0.24, -0.73, -0.16, -0.06, -0.02, -0.04, -0.75, -0.74, -0.74, -0.74, -0.77, -0.17, -0.51, -0.57, -0.78, -0.79, -0.22, -0.21, -0.22, -0.73, -0.58, -0.53, -0.23, -0.24, -0.56, -0.25, -0.31, -0.34, -0.71, -0.42, -0.26, -0.25, -0.35, -0.34, -0.44, -0.44, -0.44, -0.45, -0.45, -0.45, -0.3, -0.44, -0.17, -0.18, -0.18, -0.18, -0.18, -0.48, -0.48, -0.48), type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("var1", "var2"), class = "factor")), .Names = c("year", "density", "value", "type"), row.names = c(NA, -80L), class = "data.frame") 

my code

ggplot(toplot, aes(year, density)) + geom_tile(aes(fill = value), colour = "black") + scale_fill_gradientn(colours=c('#dc180a', 'orange', 'white', '#a0e100', '#2e7cff'), values = rescale(c(-.5, 0, .8)), guide = "colorbar", limits = c(-.5, .8)) + scale_x_continuous(breaks = seq(0, 1250, 250), expand = c(0, 0)) + theme_bw(base_size = 20) + theme(axis.text.x = element_text(), axis.text.y = element_text(size = rel(1.1)), plot.title = element_text(hjust = 0.5)) + facet_wrap(~type, ncol = 2) + theme(strip.background = element_rect(fill = "#fafafa"))+ theme(strip.text = element_text(colour = 'black'), panel.spacing = unit(1, "lines")) + scale_y_discrete(expand = c(0, 0)) 

1 Answer 1

3

geom_raster() (a special case for geom_tile() accepts values for hjust and vjust, which controls the position of each tile relative to its coordinates. It doesn't appear to accept colour as an aesthetic for tile outlines, but you can fake it by placing grid lines (appropriately spaced) over the tiles:

ggplot(toplot, aes(x = year, y = density)) + geom_raster(aes(fill = value), hjust = 0) + geom_hline(yintercept = seq_along(toplot$density) + 0.5) + scale_fill_gradientn(colours=c('#dc180a', 'orange', 'white', '#a0e100', '#2e7cff'), values = rescale(c(-.5, 0, .8)), guide = "colorbar", limits = c(-.5, .8)) + scale_x_continuous(breaks = seq(0, 1250, 250), expand = c(0, 0)) + facet_wrap(~type, ncol = 2) + theme_bw(base_size = 10) + theme(panel.spacing = unit(1, "lines"), panel.background = element_blank(), panel.grid.major.x = element_line(color = "black"), panel.grid.minor.x = element_line(color = "black"), panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank(), panel.ontop = TRUE, axis.text.x = element_text(hjust = 1)) + scale_y_discrete(expand = c(0, 0)) 

plot

Sign up to request clarification or add additional context in comments.

4 Comments

Absolutely brilliant! I would have never thought of doing that. Do you know if it's possible to either move the 1250 to the left or to not display the 0 in the var2 facet?
Glad it helped. :) Sounds like adding axis.text.x = element_text(hjust = 1) to theme() will achieve what you're asking for. I'll update my answer.
There is also another slight problem. The a,b,c,d should be at the center of the cell not at the top or bottom. I have used this axis.text.y = element_text(vjust = 2) to center them. I guess I will also have to eliminate the y tick marks.
Updated answer. This can be achieved by leaving geom_raster's vjust to its default value (0.5), hiding the horizontal grid lines, & explicitly drawing horizontal lines using geom_hline at the X.5 positions instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.