Avoid clipping of points along axis in ggplot

淺唱寂寞╮ 提交于 2019-11-26 16:36:32

Try this,

q <- qplot(1:10,1:10,size=I(10)) + scale_y_continuous(expand=c(0,0))
gt <- ggplot_gtable(ggplot_build(q))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)

With the release of ggplot2 version 3.0.0, you can simply use coord_cartesian(clip = 'off').

library(ggplot2)

qplot(x = 1:10, y = 1:10, size=I(10)) + 
  scale_y_continuous(expand=c(0,0)) +
  coord_cartesian(clip = 'off') +
  labs(title = "coord_cartesian(clip = 'off')")

If you're using one of the less commonly-used coord_* systems or functions (e.g. coord_polar or coord_flip), then you can use the clip = 'off' argument there, too.

my_plot + 
coord_flip(clip = 'off')
broussea

You can use attribute expand() on the scale_y
Exemple for 10% each side of y scale :

ggplot(mydata, aes(y = value, x = mydate)) +
  geom_point() +
  scale_y_continuous(expand = c(0.1,0.1))

If you were using base graphics you could use clip().

plot(1:4)
clip(-0.5, 4.1, -0.5, 4.1)
points(0.85, 1, col = 'red', cex = 2)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!