与往常一样,您有两个选择-在图外进行注释,或者创建两个(或三个!)图并将其组合。
两种选择都需要反复试验。希望您不需要经常使用此方法,也不需要根据不同的比例将其完全自动化。
library(ggplot2)
library(patchwork)
textframe <- data.frame( #making the frame for the text labels.
x = c(-Inf, Inf),
y = -50,
labels = c("Source1: mtcars dataset", "Source2: Not mtcars dataset"))
图外的选项1注释
# requires manual trial and error with plot margin and y coordinate...
# therefore less optimal
ggplot(mtcars, aes( mpg, hp)) +
geom_point() +
geom_text(data = textframe, aes(x, y, label = labels), hjust = c(0,1)) +
coord_cartesian(ylim = c(0,350), clip = 'off') +
theme(plot.margin = margin(b = 50, 5,5,5, unit = 'pt'))
选项2两个图,将它们组合。在这里使用patchwork
。我个人更喜欢此选项。
p1 <-
ggplot(mtcars, aes( mpg, hp)) +
geom_point()
p2 <-
ggplot(mtcars, aes( mpg, hp)) +
geom_blank() +
geom_text(data = textframe,
aes(x, y = Inf, label = labels),
hjust = c(0,1),
vjust = 1) +
theme_void()
p1/p2 +plot_layout(heights = c(1, 0.1))
由reprex软件包(v0.3.0)创建于2020-04-04
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句