[英]Add horizontal lines outside plot area in ggplot2
我试过找到答案,但似乎没有任何效果。 下面的第一张图是在 ggplot2 中绘制的散点图,经过 Overleaf 上特定的 LaTeX 期刊模板后处理。 我想重新创建图表而不必使用模板。
不幸的是,我无法弄清楚如何绘制将标题区域和注释区域(分别)与主要 plot 区域分开的水平线(参见红色箭头。
我怎样才能做到这一点?
哦,第二张图片是由下面的代码生成的。
谢谢!
library(ggplot2)
theme_set(theme_bw()) # pre-set the bw theme.
data("midwest", package = "ggplot2")
# Scatterplot
gg <- ggplot(midwest, aes(x=area, y=poptotal)) +
geom_point(aes(col=state, size=popdensity)) +
geom_smooth(method="loess", se=F) +
xlim(c(0, 0.1)) +
ylim(c(0, 500000)) +
labs(y="Population",
x="Area",
title="Figure 4: Scatterplot",
caption = "Source: midwest") +
theme(plot.background = element_rect(colour="black",size=1))
plot(gg)
您可以设置coord_cartesian(clip = "off")
并添加几个annotation_custom
调用。 这允许相对于面板进行绘图,而无需指定相对于数据的坐标:
ggplot(midwest, aes(x=area, y=poptotal)) +
geom_point(aes(col=state, size=popdensity)) +
geom_smooth(method="loess", se=F) +
xlim(c(0, 0.1)) +
ylim(c(0, 500000)) +
labs(y="Population",
x="Area",
title="FIGURE 4: Scatterplot",
caption = "Source: midwest") +
coord_cartesian(clip = "off") +
annotation_custom(grid::linesGrob(x = c(-0.12, 1.19), y = c(1.03, 1.03))) +
annotation_custom(grid::linesGrob(x = c(-0.12, 1.19), y = c(-.07, -.07))) +
theme(plot.background = element_rect(colour="black", size = 1),
plot.title = element_text(size = 16, face = 2, vjust = 5, hjust = -0.2),
plot.margin = margin(20, 20, 20, 20))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.