tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.54k stars 2.03k forks source link

feature request: give access to intermediate coordinates of geom_ : path + segment + curve #2372

Closed statsccpr closed 6 years ago

statsccpr commented 6 years ago

At first, i posted in a related topic that's been closed https://github.com/tidyverse/ggplot2/issues/2280#issuecomment-351476755 The above thread would be the use case of this feature request here.

Although @hadley 's response to the originating closed feature request said it's better as an extension package. I think the fundamental root feature that would allow for all of this to work is the feature request in this independent thread here, which seems to be a 'vanilla' ggplot output.

If a user has access to the intermediate coordinates that ggplot2 uses to display

users can add additional plot elements based on the intermediate coordinates

In the original use case, the intermediate coordinates can be used to place custom arrowhead locations other than the enforced 'end' of the segment, say placing the arrowheads in the 'midpoint' of the segment

I dug around and see that geom_curve() uses grid::curveGrob()

but i can't quite figure out 1) how curveGrob() returns the coordinate values. 2) how ggplot2 stores or returns these intermediate coordinate values, output from 1

The big picture would be, once the curveGrob() coordinates are identified, the ggplot2 user could extract that from

p = ggplot() + geom_curve()

str(p)

p$geom_curve$coordinates

# whose values would be a data.frame looking like 
# c(curve_id, x_1, y_1)

Then use it like

dat_midpoints = (p$geom_curve$coordinates)[c(1,3,5),]
p2 = ggplot() + geom_curve(...,arrow=NULL) + geom_segment(data=dat_midpoints ,arrow=arrow())
hadley commented 6 years ago

I don't think ggplot2 has the coordinates - they're computed by grid, and hence this would be more appropriate in an extension package.

thomasp85 commented 6 years ago

These values are all calculated at draw time so they cannot even be accessed in grid. ggforce has a couple of geoms, such as arcs, beziers, and splines (and straight lines) that calculates the points directly and can thus be accessed...

statsccpr commented 6 years ago

I see, so it seems like ggplot passes its arguments down the internal pipeline to grid::curve then @thomasp85 explanation would answer the questions 1) and 2) posed in the original post with "you can not"