我在 ggplot2 中创建了两个方面图。我想在绘图区域外添加一个箭头。多个问题试图解决这个问题: How to draw lines out of plot area in ggplot2? 在 ggplot2 生成的图下方显示文本
但我不能让我的例子工作。另外,我希望有一种更简单的方法来实现这一点?
我试图增加plot.margins和使用coord_cartesian(),但都没有帮助。
相反,我想要:
我的虚拟示例:
# read library to assess free data
library(reshape2)
library(ggplot2)
ggplot(tips,
aes(x=total_bill,
y=tip/total_bill)) +
geom_point(shape=1) +
facet_grid(. ~ sex) +
# define the segment outside the plot
geom_segment(aes(x = 10,
y = -0.25,
xend = 10,
yend = 0),
col = "red",
arrow = arrow(length = unit(0.3, "cm"))) +
theme_bw() +
# limit the displayed plot extent
coord_cartesian(ylim = c(0, 0.75)) +
# increase plot margins - does not help
theme(plot.margin = unit(c(1,1,1,0), "lines"))
Run Code Online (Sandbox Code Playgroud)
您可以使用中的参数clip="off"在面板外绘制coord_cartesian。我还使用expand参数 ofscale_y将 y 轴从零开始。在y开始位置上有一点手动选择。
所以你的例子
library(reshape2)
library(ggplot2)
ggplot(tips,
aes(x=total_bill,
y=tip/total_bill)) +
geom_point(shape=1) +
facet_grid(. ~ sex) +
annotate("segment", x=12, y=-0.05, xend=12, yend=0,
col="red", arrow=arrow(length=unit(0.3, "cm"))) +
scale_y_continuous(expand=c(0,0))+
theme_bw() +
coord_cartesian(ylim = c(0, 0.75), clip="off") +
theme(plot.margin = unit(c(1,1,1,0), "lines"))
Run Code Online (Sandbox Code Playgroud)
(我改变geom_segment对annotate你没有映射美学)
其中产生
| 归档时间: |
|
| 查看次数: |
3889 次 |
| 最近记录: |