我有两个折线图,一个用“拼图”堆叠在另一个的下面。我想为堆栈添加一个通用的y轴标题,因此我在两行charts.After堆叠的上方添加了Y轴标签。我发现,y轴标签的下半部分被下部折线图的边缘遮挡(如下面附图中的绿色和黄色绘图背景所示).Here是代码和结果
# 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

所以我试着通过将下部图表的左边距从0减少到-1来解决这个问题,但是它没有改变边距并且轴标题仍然被阻塞。如果两个图表的左边距都减小到-1,y轴标题,则不再显示刻度,如下图所示。绿色和黄色填充矩形是绘图背景。

有没有人能帮我找到解决方案?如果你有什么想法,我还可以尝试什么?谢谢!
发布于 2020-04-05 09:07:38
您可以简单地设置组合图的标题(而不是lc1或lc2)。您需要为lc1和lc2设置空白的y标签,就像您已经为lc2设置的一样(例如,ylab(label = "")。
p <- (lc1/lc2) + ylab(label = "Y-axis label (unit)")

https://stackoverflow.com/questions/61036035
复制相似问题