6

I am using the following code to try to preserve geom elements that are out of bounds of the plotting area but it still seems to be clipping them beyond a certain distance above the plotting area.

g <- ggplot(iris, aes(x = Species, y = Petal.Length)) +
  stat_summary(geom = 'bar', fun.y = mean) +
  geom_point() +
  scale_y_continuous(limits = c(0,8), expand = c(0,0), oob = function(x, ...) x) +
  geom_text(label = 'obText', aes(x = 2, y = 9)) #+ 
  # theme(plot.margin = unit(c(60,5.5,5.5,5.5), "points"),
  #       aspect.ratio = 1)

gb <- suppressWarnings(ggplot_build(g))
gt <- ggplot_gtable(gb)
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid::grid.newpage()
grid::grid.draw(gt)

Any ideas on why this is and how to correct it? If I uncomment the theme argument, I can get close to what I want but this changes the aspect ratio of the plotting area.

John Gagnon
  • 825
  • 1
  • 8
  • 20
  • I'm curious why would you want to do that? – Tung Jun 28 '18 at 20:20
  • This is just an example; what I really want it for is to allow visualization of text that will be out of the boundaries of the plot – John Gagnon Jun 28 '18 at 20:23
  • Is there a need to limit the bounds in `scale_y_continuous`? Since there's data outside those limits, the plot will clip out that data if you specify the limits like that. – phalteman Jun 28 '18 at 20:25
  • Yes, I don't want the plot size to change to accommodate the text but I want the text visible. – John Gagnon Jun 28 '18 at 20:27
  • @JohnGagnon: then you should add that case to your example – Tung Jun 28 '18 at 20:28
  • 1
    @Tung I have updated the question with a better example of what I am looking for. – John Gagnon Jun 28 '18 at 20:32
  • Ook, so you want to plot `ggplot(...) + geom_point(...) + stat_summary(...)`, and have the top of the plot to cut off at `y=6`, but you want a large chunk of white space above the plot. And you want to show the points which are greater than 6 in the white space, along with some text. Right? – sam Jun 28 '18 at 21:13
  • @Sam I updated the question with semi-working example. However, I'd like to not have to specify the margins. Looking for something that works similar to however including legends works...They show up outside the plotting area and the plot scales accordingly to accommodate them... – John Gagnon Jun 28 '18 at 21:27

2 Answers2

13

Not sure if this is what you're looking for but you can use clip = 'off' option in ggplot 3.0.0 to get the text show up

See also this answer for more information

# install.packages("devtools")
# devtools::install_github("tidyverse/ggplot2")

library(ggplot2)

g <- ggplot(iris, aes(x = Species, y = Petal.Length)) +
  stat_summary(geom = 'bar', fun.y = mean) +
  geom_point() +
  scale_y_continuous(limits = c(0,8), expand = c(0,0), oob = function(x, ...) x) +
  geom_text(label = 'obText', aes(x = 2, y = 9), check_overlap = TRUE) +
  # this will allow the text outside of the plot panel
  coord_cartesian(clip = 'off') +
  theme(plot.margin = margin(4, 2, 2, 2, "cm"))
g

Created on 2018-06-28 by the reprex package (v0.2.0.9000).

Tung
  • 26,371
  • 7
  • 91
  • 115
1

If you want to see the points, you can change the value of oob =...

oob = function(x, ...) x

enter image description here

oob = squish

enter image description here

oob = censor

enter image description here

squish and censor are part of the scales package.

Note that the mean changes in both cases; squish lowers the value of the points above 6, and censor removes the points above 6.

Community
  • 1
  • 1
sam
  • 501
  • 1
  • 4
  • 11
  • Thanks, I am aware of the `scales` package functions `squish` and `censor` but they don't quite do what I want. I would like the spacing of the geom elements to remain the same but not get clipped from the output – John Gagnon Jun 28 '18 at 20:34