如何在geom_tile ggplot中在瓷砖的右/左端移动刻度线和标签?

时间:2018-05-14 13:19:20

标签: r ggplot2 heatmap

我无法将geom_tile中的x轴标签(包括tick marsk)移动到每个tile的右端,我还想在左端添加零。我试过休息和标签,但没有运气。使用休息和标签也不起作用。

我正在努力实现在这个答案中所做的事情,但解决方案表明doens不起作用。 How to force the x-axis tick marks to appear at the end of bar in heatmap graph?

我的数据

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

我的代码

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 个答案:

答案 0 :(得分:3)

geom_raster()geom_tile()的特殊情况接受hjustvjust的值,它控制每个图块相对于其坐标的位置。它不会出现接受colour作为瓷砖轮廓的美学,但你可以通过在瓷砖上放置网格线(适当间隔)来伪造它:

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