theme_void 上的 R geom_tile 在图上方和下方有空白 space
R geom_tile on theme_void has blank space above and below plot
当使用 geom_tile() 与 theme_void() 制作 ggplot 对象时
图的顶部和底部有空白区域,并且
当使用拼凑添加碎片时,这些会导致错位
右边=侧边缘条(如所附屏幕截图所示)。
请问有没有办法消除空格space?
我尝试了一个自定义主题(下面的代码)但是它
没有帮助(有时也把 space 放在 left/right 边)
library(tidyverse)
library(grid)
library(patchwork)
long <- dget('long.txt')
# Summarise data for marginal plots
in_y_df <- long %>%
group_by(in_y) %>%
summarise(value = sum(value)) %>%
mutate(value = value / sum(value)) %>%
mutate(str = paste0( round(value * 100, digits=0), '%' ))
out_x_df <- long %>%
group_by(out_x) %>%
summarise(value = sum(value)) %>%
mutate(value = value / sum(value)) %>%
mutate(str = paste0( round(value * 100, digits=0), '%' ))
# Marginal plots
py <- ggplot(in_y_df, aes(value, in_y)) +
geom_col(width = .7, fill = 'gray64') +
geom_text(aes(label = str), hjust = -0.1, size = 10 / .pt) +
scale_x_continuous(expand = expansion(mult = c(.0, .25))) +
theme_void() +
theme(plot.margin = unit(c(0,0,0,0), "mm"))
px <- ggplot(out_x_df, aes(out_x, value)) +
geom_col(width = .7, fill = 'gray8') +
geom_text(aes(label = str), vjust = -0.5, size = 10 / .pt) +
scale_y_continuous(expand = expansion(mult = c(.0, .25))) +
theme_void() +
theme(plot.margin = unit(c(0,0,0,0), "mm"))
h2 <- ggplot(long, aes(out_x, in_y, fill = value)) +
geom_tile(color = 'black', size = 0.2) +
coord_equal() +
geom_text(aes(label = value), size = 12 / .pt) +
scale_fill_gradient(low = "#E9F6E5", high = "#84CB83") +
theme_void() +
theme(legend.position = 0) +
labs(x = NULL, y = NULL, fill = NULL)
heatmap_minus_axisText <- h2
# Version with axis labels not in heatplot
yLabelsPlot <- plot_spacer()
xLabelsPlot <- plot_spacer()
squarePlot <- plot_spacer() + px + plot_spacer() +
yLabelsPlot + heatmap_minus_axisText + py +
plot_spacer() + xLabelsPlot + plot_spacer() +
plot_layout(ncol = 3, widths = c(1, 2, 1.4), heights = c(1.2, 2, 1.3))
squarePlot
坐标轴标签(尚未制作)将单独完成,因为当
包含在热图对象中,它们会导致顶部条在之后不对齐
拼接拼接,如图2所示。
heatPlot <- plot_spacer() + px + plot_spacer() +
plot_spacer() + ph + py +
plot_layout(ncol = 3, widths = c(1, 2, 0.8), heights = c(1.2, 2))
这是 dget() 制作假数据帧的文件:
structure(list(in_y = structure(c(4L, 3L, 6L, 2L, 1L, 5L, 4L,
3L, 6L, 2L, 1L, 5L, 4L, 3L, 6L, 2L, 1L, 5L, 4L, 3L, 6L, 2L, 1L,
5L, 4L, 3L, 6L, 2L, 1L, 5L, 4L, 3L, 6L, 2L, 1L, 5L, 4L, 3L, 6L,
2L, 1L, 5L), .Label = c("R5BnqKkCI3iHHfBDOd3D", "pJoPxfMkRaLbl2ESDD3s",
"8vXGhUVjAubzkubDQft5", "rtUdC7ZmBc8GfiyyJ9DF", "teSB1z4wpHzLLmqwLR4t",
"55G18mbzeqajtzO7WLLM"), class = "factor"), out_x = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 6L,
6L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 5L, 5L,
5L, 5L, 5L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("MCbgrg3f1oVlszlUUddq",
"yjqUPElzDhsAHKpVPLo6", "j3tZCBrw6qY9X2pYDc9Y", "iW2TVkR3NawpBC8p59F2",
"L1Ai51LaakWu0b47zypc", "HjkPWE5auDZ0dOH3G1L1", "WEfxf3XV0CMSWRRLHheY"
), class = "factor"), value = c(2L, 1L, 1L, 1L, 4L, 4L, 0L, 0L,
0L, 0L, 1L, 1L, 2L, 0L, 0L, 0L, 1L, 2L, 1L, 1L, 0L, 1L, 0L, 0L,
3L, 0L, 0L, 0L, 1L, 1L, 2L, 0L, 1L, 0L, 2L, 1L, 1L, 0L, 0L, 0L,
1L, 0L)), row.names = c(NA, -42L), class = "data.frame")
自定义主题
heatmap_theme <- theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.ticks.length = unit(0, "pt"),
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
plot.margin = unit(c(0,0,0,0), "mm"),
panel.spacing = unit(c(0,0,0,0), "mm")
)
您可以尝试指定轴限制并禁止自动轴扩展。为因子设置轴限制更直接:如果需要,将数字图块坐标转换为字符 (as.character
) 或因子 (as.factor
)。同时删除图例。
剥离热图示例:
data.frame(
x = c('col_1','col_2','col_1','col_2'),
y = c('row_1','row_1','row_2','row_2'),
value = runif(4)
) %>%
ggplot() +
geom_tile(aes(x, y, fill = value)) +
scale_x_discrete(limits = c('col_1','col_2'), expand = c(0,0)) +
scale_y_discrete(limits = c('row_1','row_2'), expand = c(0,0)) +
scale_fill_continuous(guide = 'none') +
theme_void()
编辑:对于复合图表,还有 cowplot
当使用 geom_tile() 与 theme_void() 制作 ggplot 对象时 图的顶部和底部有空白区域,并且 当使用拼凑添加碎片时,这些会导致错位 右边=侧边缘条(如所附屏幕截图所示)。
请问有没有办法消除空格space? 我尝试了一个自定义主题(下面的代码)但是它 没有帮助(有时也把 space 放在 left/right 边)
library(tidyverse)
library(grid)
library(patchwork)
long <- dget('long.txt')
# Summarise data for marginal plots
in_y_df <- long %>%
group_by(in_y) %>%
summarise(value = sum(value)) %>%
mutate(value = value / sum(value)) %>%
mutate(str = paste0( round(value * 100, digits=0), '%' ))
out_x_df <- long %>%
group_by(out_x) %>%
summarise(value = sum(value)) %>%
mutate(value = value / sum(value)) %>%
mutate(str = paste0( round(value * 100, digits=0), '%' ))
# Marginal plots
py <- ggplot(in_y_df, aes(value, in_y)) +
geom_col(width = .7, fill = 'gray64') +
geom_text(aes(label = str), hjust = -0.1, size = 10 / .pt) +
scale_x_continuous(expand = expansion(mult = c(.0, .25))) +
theme_void() +
theme(plot.margin = unit(c(0,0,0,0), "mm"))
px <- ggplot(out_x_df, aes(out_x, value)) +
geom_col(width = .7, fill = 'gray8') +
geom_text(aes(label = str), vjust = -0.5, size = 10 / .pt) +
scale_y_continuous(expand = expansion(mult = c(.0, .25))) +
theme_void() +
theme(plot.margin = unit(c(0,0,0,0), "mm"))
h2 <- ggplot(long, aes(out_x, in_y, fill = value)) +
geom_tile(color = 'black', size = 0.2) +
coord_equal() +
geom_text(aes(label = value), size = 12 / .pt) +
scale_fill_gradient(low = "#E9F6E5", high = "#84CB83") +
theme_void() +
theme(legend.position = 0) +
labs(x = NULL, y = NULL, fill = NULL)
heatmap_minus_axisText <- h2
# Version with axis labels not in heatplot
yLabelsPlot <- plot_spacer()
xLabelsPlot <- plot_spacer()
squarePlot <- plot_spacer() + px + plot_spacer() +
yLabelsPlot + heatmap_minus_axisText + py +
plot_spacer() + xLabelsPlot + plot_spacer() +
plot_layout(ncol = 3, widths = c(1, 2, 1.4), heights = c(1.2, 2, 1.3))
squarePlot
坐标轴标签(尚未制作)将单独完成,因为当 包含在热图对象中,它们会导致顶部条在之后不对齐 拼接拼接,如图2所示。
heatPlot <- plot_spacer() + px + plot_spacer() +
plot_spacer() + ph + py +
plot_layout(ncol = 3, widths = c(1, 2, 0.8), heights = c(1.2, 2))
这是 dget() 制作假数据帧的文件:
structure(list(in_y = structure(c(4L, 3L, 6L, 2L, 1L, 5L, 4L,
3L, 6L, 2L, 1L, 5L, 4L, 3L, 6L, 2L, 1L, 5L, 4L, 3L, 6L, 2L, 1L,
5L, 4L, 3L, 6L, 2L, 1L, 5L, 4L, 3L, 6L, 2L, 1L, 5L, 4L, 3L, 6L,
2L, 1L, 5L), .Label = c("R5BnqKkCI3iHHfBDOd3D", "pJoPxfMkRaLbl2ESDD3s",
"8vXGhUVjAubzkubDQft5", "rtUdC7ZmBc8GfiyyJ9DF", "teSB1z4wpHzLLmqwLR4t",
"55G18mbzeqajtzO7WLLM"), class = "factor"), out_x = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 6L,
6L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 5L, 5L, 5L,
5L, 5L, 5L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("MCbgrg3f1oVlszlUUddq",
"yjqUPElzDhsAHKpVPLo6", "j3tZCBrw6qY9X2pYDc9Y", "iW2TVkR3NawpBC8p59F2",
"L1Ai51LaakWu0b47zypc", "HjkPWE5auDZ0dOH3G1L1", "WEfxf3XV0CMSWRRLHheY"
), class = "factor"), value = c(2L, 1L, 1L, 1L, 4L, 4L, 0L, 0L,
0L, 0L, 1L, 1L, 2L, 0L, 0L, 0L, 1L, 2L, 1L, 1L, 0L, 1L, 0L, 0L,
3L, 0L, 0L, 0L, 1L, 1L, 2L, 0L, 1L, 0L, 2L, 1L, 1L, 0L, 0L, 0L,
1L, 0L)), row.names = c(NA, -42L), class = "data.frame")
自定义主题
heatmap_theme <- theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.ticks.length = unit(0, "pt"),
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
plot.margin = unit(c(0,0,0,0), "mm"),
panel.spacing = unit(c(0,0,0,0), "mm")
)
您可以尝试指定轴限制并禁止自动轴扩展。为因子设置轴限制更直接:如果需要,将数字图块坐标转换为字符 (as.character
) 或因子 (as.factor
)。同时删除图例。
剥离热图示例:
data.frame(
x = c('col_1','col_2','col_1','col_2'),
y = c('row_1','row_1','row_2','row_2'),
value = runif(4)
) %>%
ggplot() +
geom_tile(aes(x, y, fill = value)) +
scale_x_discrete(limits = c('col_1','col_2'), expand = c(0,0)) +
scale_y_discrete(limits = c('row_1','row_2'), expand = c(0,0)) +
scale_fill_continuous(guide = 'none') +
theme_void()
编辑:对于复合图表,还有 cowplot