我正在创建三个独立的图,然后将它们拼凑起来。我制作的第三个图使用facet_grid 来添加显示年份的辅助x 轴。但我认为这在某种程度上扰乱了我的 y 轴标签的对齐方式。 对于第三个图表,y 轴标签现在连接到图表,中间没有空格,就像前两个图表一样。
#First graph
presence <- ggplot(df_long, aes(x = date)) +
geom_bar(aes(y = total_presence, fill = "Presence"), stat = "identity", width = 1, position = "stack") +
geom_bar(aes(y = total_song, fill = "Song"), stat = "identity", width = 1, position = "stack") +
geom_bar(data = filter(df_long, is.na(total_presence) & is.na(total_song)),
aes(x=date, y =24), fill = "azure3", stat="identity", width = 1, position = "stack") +
scale_fill_manual(values = c("Presence" = "olivedrab", "Song" = "olivedrab3"), name = "Detections") +
scale_y_continuous(name = "Presence", breaks = seq(0,24,by=2), limits = c(0,24),expand=c(0,0))+
scale_x_date(date_labels = "%b", date_breaks="1 month", expand= c(0,0))+
labs(x=NULL, y = "Total Detections", title = paste(location)) +
theme_minimal() +
theme(axis.title.y = element_text(color = "black"),
axis.text.y = element_text(color = "black"),
axis.ticks.y=element_line(color="black"),
panel.grid = element_blank(),
legend.position = "right",
plot.title = element_text(hjust = 0.5))
#Second graph
ice_temp <- ggplot(df_long, aes(x=date)) +
geom_bar(aes(y=Proportion,fill=Ice_Type), stat = "identity", position = "stack", width = 1) +
geom_line(aes(y = (sst_mean/4)+0.5), color = "red", size = 0.4) +
#labs(x=NULL, y="Sea Ice Proportion", title = paste("Daily Sea Ice Cover and SST at", location, year_start, "-", year_end))+
labs(x=NULL, y="Sea Ice Proportion", title = NULL)+
scale_fill_manual(values = c(
"VeryOpenDriftIce" = "#7FDBFF",
"OpenDriftIce" = "#0074D9",
"CloseDriftIce" = "#00457e",
"VeryCloseDriftIce" = "#001f3f",
"FastIce" = "red"
)) +
theme_minimal() +
scale_y_continuous(limits=c(0, NA), expand=c(0,0),
sec.axis = sec_axis(~ (.-0.5)*4, name = "Sea Surface Temperature (°C)",
breaks = seq(-2,2,by=1)))+
scale_x_date(date_labels = "%b", date_breaks="1 month", expand= c(0,0))+
theme(panel.grid = element_blank(), legend.position = "right",
axis.ticks = element_line(),
axis.title.y.right = element_text(color = "red"),
axis.text.y.right = element_text(color = "red"),
axis.ticks.y.right=element_line(color="red"))
#third graph
zooc_chl <- ggplot(df_long, aes(x=date)) +
geom_line(aes(y=zooc_mean),color="#D55E00", size = 0.4) +
geom_line(aes(y=chl_mean),color="#0072B2",size=0.4)+
#labs(x=NULL, y="<span style = 'color:#D55E00;'>Zooplankton [g/m<sup>2</sup>]</span> \n<span style = 'color:#0072B2;'>Chlorophyll a [mg/m<sup>3</sup>]</span>", title = paste("Primary Production, Zooplankton and Chlorophyll a at", location, year_start, "-", year_end))+
labs(x=NULL, y="<span style = 'color:#D55E00;'>Zooplankton [g/m<sup>2</sup>]</span> \n<span style = 'color:#0072B2;'>Chlorophyll a [mg/m<sup>3</sup>]</span>", title = NULL)+
scale_y_continuous(breaks = seq(0,3,by=0.5), limits = c(0,3),expand=c(0,0))+
scale_x_date(date_labels = "%b", date_breaks="1 month", expand= c(0,0))+
facet_grid(~ year(date), space= "free", scales="free",switch="x") +
theme_minimal() +
theme(axis.title.y=element_markdown(),
strip.placement = "outside",
axis.ticks = element_line(),
strip.background = element_rect(fill=NA,colour="grey50"),
panel.spacing=unit(0,"cm"))
#combine graphs
combined_plot <- presence / ice_temp / zooc_chl
我尝试用 margins 或 vjust/hjust 修复这个问题,但没能修复它。 有谁知道我可以做什么来对齐 y 轴标签?
如果您能提供一个包含少量数据的最小可重现示例,那就更好了。
使用最少的虚构数据,并通过删除导致问题的代码,看起来
theme(strip.placement = "right")
就是原因。请参阅下面的对齐和未对齐版本:
library(tidyverse)
library(patchwork)
df <-
tibble(
date = seq(ymd("2024-01-01"), length.out = 100, by = "day"),
y = rnorm(100, 1, 10)
)
p1 <- ggplot(df, aes(date)) +
geom_bar()
p2 <- ggplot(df, aes(date)) +
geom_bar()
p3 <- ggplot(df, aes(date, y)) +
geom_line() +
labs(x = NULL, y = "some text", title = "Aligned") +
facet_grid(~ year(date), space = "free", scales = "free", switch = "x")
# Aligned
p1 / p2 / p3
# Misaligned
p4 <- p3 + theme(strip.placement = "right") + ggtitle("Misaligned")
p1 / p2 / p4
创建于 2024-04-10,使用 reprex v2.1.0