Skip to content

Feature request: make clipping configurable #2536

Closed
@clauswilke

Description

@clauswilke

I frequently would like to be able to draw things that fall outside of the plot panel. This is currently not possible, as clipping is strictly enforced and not configurable. I think it would be nice if this could be switched on or off. First, as motivation, an example where it would be useful. Then my proposal of how it could be implemented.

Consider this plot:

mt_named <- data.frame(mtcars, name = row.names(mtcars))

p <- ggplot(mt_named, aes(mpg, reorder(name, mpg))) + 
  geom_point(size = 2.5, color = "#0072B2") +
  geom_text(aes(x = mpg + .5, label = name), hjust = 0, size = 3) +
  scale_y_discrete(name = NULL, breaks = NULL, expand = c(0, 1)) +
  theme_bw() +
  theme(panel.border = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line.x = element_line(),
        plot.margin = margin(6, 45, 6, 6))
p

screen shot 2018-04-27 at 12 59 24 pm

The top-most text label is cut off because it extends beyond the plot area. To make it fit, I could extend the axis range, but that would extend the axis line as well. It looks more elegant to switch of clipping and have the text label extend outside of the plot area:

g <- ggplotGrob(p)
index <- grep("panel", g$layout$name)
g$layout$clip[index] = "off"
grid::grid.newpage()
grid::grid.draw(g)

screen shot 2018-04-27 at 1 00 14 pm

I would like to propose to address this by making clipping a feature of the coordinate system. For example, the above plot could be generated by something like this:

p + coord_cartesian(clip = "off")

This feature requires very minimal changes to the coord code and the facet layouting code. I'll see if I can put together a pull request implementing this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    coord 🗺️featurea feature request or enhancementwipwork in progress

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions