當使用 geom_tile() 使用 theme_void() 制作 ggplot 物件時,繪圖的頂部和底部有空白區域,并且當使用拼湊添加碎片時,這些會導致右側邊緣條的不對齊(如附加的螢屏截圖所示) .
請問有沒有辦法消除空格?我嘗試了一個自定義主題(下面的代碼)但它沒有幫助(有時也會在左側/右側放置空間)
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

軸標簽(尚未制作)將單獨完成,因為當包含在熱圖物件中時,它們會在拼湊后導致頂欄錯位,如第二張圖片所示。
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")
)
uj5u.com熱心網友回復:
您可以嘗試指定軸限制并禁止自動軸擴展。為因子設定軸范圍更直接:如果需要,將數字平鋪坐標轉換為字符 ( 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/435055.html
上一篇:Rggplotgeom_points未與箱線圖箱對齊
下一篇:ggplot多年來的增量
