R ggplot2,拼图-被相邻地块边缘遮挡的轴标题部分
我有两张折线图,一张叠在另一张下面,用“拼凑”的方法。我想为堆栈添加一个通用的y轴标题,因此我在两个折线图的上部添加了y轴标签。堆叠后,我发现,y轴标签的下部被下部折线图的边距遮挡(如下图中的绿色和黄色绘图背景所示)。下面是代码和结果R ggplot2,拼图-被相邻地块边缘遮挡的轴标题部分,r,ggplot2,plot,axis,patchwork,R,Ggplot2,Plot,Axis,Patchwork,我有两张折线图,一张叠在另一张下面,用“拼凑”的方法。我想为堆栈添加一个通用的y轴标题,因此我在两个折线图的上部添加了y轴标签。堆叠后,我发现,y轴标签的下部被下部折线图的边距遮挡(如下图中的绿色和黄色绘图背景所示)。下面是代码和结果 # data x<- 1:256 x y<- runif(256, 0, 10) y data <- data.frame(x,y) head(data) # lc1 # Geom properties lc1<- ggplot(dat
# data
x<- 1:256
x
y<- runif(256, 0, 10)
y
data <- data.frame(x,y)
head(data)
# lc1
# Geom properties
lc1<- ggplot(data, aes(x=x, y=y)) +
geom_line()
# Scale
lc1<- lc1 +
expand_limits(x = 0, y = 0) +
scale_x_continuous(
expand = c(0, 0),
breaks = c(0, 32, 64, 96, 128, 160, 192, 224, 256),
label = NULL
) +
scale_y_continuous(expand = c(0, 0),
breaks = c(0, 5, 10))
# Aspect ratio
lc1 <- lc1 + theme(aspect.ratio = 0.15)
# Panel properties
lc1 <- lc1 +
theme(panel.background = element_blank()) +
theme(
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
legend.position = "none"
)
# Axes lines, ticks, axis text
lc1 <- lc1 +
theme(
axis.line.x = element_line (colour = "grey", size = 0.5),
axis.line.y = element_line(colour = "black", size = 0.5),
axis.ticks.x = element_blank(),
axis.ticks.y = element_line(colour = "black", size = 0.5),
axis.ticks.length = unit(.15, "cm")
) +
theme(
axis.text.y = element_text(
color = "black",
face = "plain",
size = 6,
margin = margin(
t = 0,
r = 2,
b = 0,
l = 0
),
angle = 0,
vjust = 0,
hjust = 0
)
)
# Title, caption, axes labels
lc1<- lc1 +
ylab(label = "Y-axis label (unit)") +
theme(axis.title.x = element_text(
color = "black",
size = 10,
face = "bold",
hjust = 0
))
# Plot margins
lc1 <- lc1 +
theme(plot.margin = unit(c(-0.55,0,-0.53,0), "cm"))
# plot background
lc1<- lc1 + theme(plot.background = element_rect(fill = "green"))
lc1
# lc2
# Geom properties
lc2<- ggplot(data, aes(x=x, y=y)) +
geom_line()
# Scale
lc2<- lc2 +
expand_limits(x = 0, y = 0) +
scale_x_continuous(
expand = c(0, 0),
breaks = c(0, 32, 64, 96, 128, 160, 192, 224, 256),
label = NULL
) +
scale_y_continuous(expand = c(0, 0),
breaks = c(0, 5, 10))
# Aspect ratio
lc2 <- lc2 + theme(aspect.ratio = 0.15)
# Panel properties
lc2 <- lc2 +
theme(panel.background = element_blank()) +
theme(
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
legend.position = "none"
)
# Axes lines, ticks, axis text
lc2 <- lc2 +
theme(
axis.line.x = element_line (colour = "grey", size = 0.5),
axis.line.y = element_line(colour = "black", size = 0.5),
axis.ticks.x = element_blank(),
axis.ticks.y = element_line(colour = "black", size = 0.5),
axis.ticks.length = unit(.15, "cm")
) +
theme(
axis.text.y = element_text(
color = "black",
face = "plain",
size = 6,
margin = margin(
t = 0,
r = 2,
b = 0,
l = 0
),
angle = 0,
vjust = 0,
hjust = 0
)
)
# Title, caption, axes labels
lc2<- lc2 +
ylab(label = "") +
theme(axis.title.x = element_text(
color = "black",
size = 10,
face = "bold",
hjust = 0.5
))
# Plot margins
lc2 <- lc2 +
theme(plot.margin = unit(c(-0.55,0,-0.53,0), "cm"))
# plot background
lc2<- lc2 + theme(plot.background = element_rect(fill = "yellow"))
lc2
# stacking
library(patchwork)
p<- (lc1/lc2)
p
#数据
x您只需设置组合图的标题(而不是lc1
或lc2
)。您需要为lc1
和lc2
设置空白y标签,就像您为lc2
所做的那样(例如ylab(label=”“)
)
p <- (lc1/lc2) + ylab(label = "Y-axis label (unit)")
p我在下面回答了您的问题,但我只是想在您似乎正在学习R时添加一个注释。通常,最好在脚本开始时加载所有包(例如library(patchwork)
),这会给您一些额外的样式点;)。坚持下去!你做得很好!:)谢谢μ介子;我一定会记住你的宝贵建议!回答得很好@Muon,你知道如何在两个图之间居中放置ylab吗?@derekcoran你可以使用hjust
或vjust
移动文本元素。尝试将此添加到绘图中<代码>+主题(axis.title.y=元素\文本(hjust=-4))