当使用facet_grid()时,我希望能够创建一个只具有最外边界的图.这个问题以前已经回答过了:ggplot2 outside panel border when using facet
但是这个例子对我不起作用,因为我需要面标签在图的底部,当使用这个解决方案时,顶部边界上的每个网格之间都有一个空间.
#Example for stack
df <- structure(list(Lake = c("Lake_x", "Lake_x", "Lake_x", "Lake_x"
), Site = c("Lake_x-1", "Lake_x-2", "Lake_x-3", "Lake_x-4"),
Season = structure(1:4, levels = c("Season1", "Season2",
"Season3", "Season4"), class = "factor"), Nitrogen = c(450L,
390L, 930L, 950L)), row.names = c(NA, -4L), class = "data.frame")
df$Season
#order Season
df$Season <- factor(df$Season, levels=c("Season1", "Season2", "Season3", "Season4"))
ggplot(df, aes(fill = Site, y = Nitrogen , x = Site)) +
geom_bar(stat="identity", position = position_dodge(width = 6), width = 0.4) +
scale_fill_manual(
"Site",
values = c("black", "black", "black", "black"),
) +
theme_classic() +
theme(panel.border = element_rect(colour = "black", fill=NA, size=1),
text = element_text(size = 8, family = "sans"),
) + labs(title = 'Area 8', x = "", y = expression(paste("Total Nitrogen (µg/L)"))) +
scale_y_continuous(expand = c(0,0)) +
scale_x_discrete(NULL, expand = c(0.2, 0.2)) +
theme(legend.position ="none") +
facet_grid(~Season, scales = "free_x", switch = 'x') +
theme(panel.background = element_blank(),
panel.spacing = unit(0, "line"),
strip.background = element_blank(),
strip.placement = "outside") + theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5)) + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
What current output looks like
我想三个黑色的水平边界的阴谋被删除,使它看起来像一个阴谋.
注意:我知道在本例中可以在没有facet_grid()的情况下绘制绘图,但这只是一个示例,我的实际绘图肯定需要使用facet_grid,所以在使用此函数时需要解决外部边界问题.