我无法将 geom_tile 中的 x 轴标签(包括刻度线)移动到每个图块的右端,我还想在左端添加零。我尝试过休息和标签,但没有运气。使用中断和标签也不起作用。
我试图实现这个答案中所做的事情,但建议的解决方案不起作用。如何强制 x 轴刻度线出现在热图中条形的末尾? https://stackoverflow.com/questions/10710463/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))