绘制边距裁剪的特征

Plot features cropped by Margin

当我编译以下 MWE 时,我观察到最大点 (3,5) 的边距明显 cut/cropped。

为简单起见,下面的示例被大幅缩减。

在我的实际数据中,如果相应的 x 轴美学位于最大 x 值上,则以下内容都受到手动限制我的 coord_cartesian 的影响。

MWE

library(ggplot2)
library("grid")

print("Program started")

n = c(0.1,2, 3, 5) 
s = c(0,1, 2, 3)  
df = data.frame(n, s)

gg <- ggplot(df, aes(x=s, y=n))
gg <- gg + geom_point(position=position_dodge(width=NULL), size = 1.5) 
gg <- gg + geom_line(position=position_dodge(width=NULL))

gg <- gg + coord_cartesian( ylim = c(0, 5), xlim = c((-0.05)*3, 3));

print(gg)

print("Program complete - a graph should be visible.")

为了适当地显示我的数据,我会考虑使用以下任何可能的方法(受到 x 轴标签本身从未被切割的观察的影响):

  1. 使边距透明,这样点就不会被切掉
    • 除非该点被绘图区域而不是边缘切割
  2. 将带有绘图区域的面板置于最前面
    • 除非该点被绘图区域而不是边缘切割,否则顺序是独立的
  3. 使用 xlim = c((-0.05)*3, (3*0.05)) 来扩展轴范围但实施一些 hack 以不显示最大点 3 之后的悬垂轴条?
    • 我原来是这样的,但我被告知要删除 3 之后的悬垂,因为它是不可接受的。

这就是选项 1 的意思吗:

gg <- ggplot(df, aes(x=s, y=n)) +
  geom_point(position=position_dodge(width=NULL), size = 3) +
  geom_line(position=position_dodge(width=NULL)) + 
  coord_cartesian(xlim=c(0,3), ylim=c(0,5))

# Turn of clipping, so that point at (3,5) is not clipped by the panel grob
gg1 <- ggplot_gtable(ggplot_build(gg))
gg1$layout$clip[gg1$layout$name=="panel"] <- "off"
grid.draw(gg1)