我正在嘗試制作一個沒有任何邊距的圖,但我認為使用“expand_limits”會導致在我的圖中添加邊距。
在下面的示例中,我將兩個圖重疊以嘗試說明我的觀點。我創建了兩個重疊的條形圖,其中一個我使用 expand_limits 將繪圖區域設定為包括 y=1.5 作為最大值。但是,當我繪制一個具有相同最大值的矩形時,很明顯在 y 最大值上方沒有空白。
如何阻止 y=1.5 上方的空白區域(即下方矩形上方)的出現?
library(ggplot2)
library(tidyverse)
library(dplyr)
library(patchwork)
set.seed(16)
dataf1 <- tibble(
percent=runif(4,0,1),
group=c("Group A", "Group B", "Group C", "Group D")
)
dataf2 <- tibble(
percent=runif(4,0,1),
group=c("Group E", "Group F", "Group G", "Group H")
)
theme_update(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks =element_blank(),
plot.margin=unit(c(0,0,0,0), "cm"),
panel.spacing=unit(c(0,0,0,0), "cm"))
fig1 <- ggplot(data=dataf1, aes(y=percent,x=group))
geom_bar(stat="identity")
fig2 <- ggplot(data=dataf2, aes(y=percent,x=group))
geom_bar(stat="identity") theme(panel.background = element_blank())
expand_limits(y=1.5)
geom_rect(xmin=1,xmax=4,ymin=0,ymax=1.5,fill=NA,color="black")
layout <- c(
area(t=1,l=1,b=8,r=8),
area(t=3,l=2,b=10,r=9))
fig1 fig2 plot_layout(design=layout)
uj5u.com熱心網友回復:
這僅僅是我對評reprexexpand的說法scale_y_continuous()。
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.1.1
library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.1
#> Warning: package 'tidyr' was built under R version 4.1.1
#> Warning: package 'readr' was built under R version 4.1.1
library(dplyr)
library(patchwork)
set.seed(16)
dataf1 <- tibble(
percent=runif(4,0,1),
group=c("Group A", "Group B", "Group C", "Group D")
)
dataf2 <- tibble(
percent=runif(4,0,1),
group=c("Group E", "Group F", "Group G", "Group H")
)
theme_update(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks =element_blank(),
plot.margin=unit(c(0,0,0,0), "cm"),
panel.spacing=unit(c(0,0,0,0), "cm"))
fig1 <- ggplot(data=dataf1, aes(y=percent,x=group))
geom_bar(stat="identity")
fig2 <- ggplot(data=dataf2, aes(y=percent,x=group))
geom_bar(stat="identity") theme(panel.background = element_blank())
expand_limits(y=1.5)
geom_rect(xmin=1,xmax=4,ymin=0,ymax=1.5,fill=NA,color="black")
layout <- c(
area(t=1,l=1,b=8,r=8),
area(t=3,l=2,b=10,r=9))
fig1 fig2 plot_layout(design=layout)

fig3 <- fig2 scale_y_continuous(expand = c(0, 0))
fig1 fig3 plot_layout(design = layout)

由reprex 包(v2.0.1)于 2021 年 12 月 7 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376434.html
上一篇:ggplot折線圖的問題
