You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I suspect this is because draw_panel() applies sf_as_grob() on every row. Is it possible to do this more efficiently, e.g. st_combine() per group so that it plots one MULTIPOINT that contains 1000 points?
Reacted by Dana Paige Seidel, Jonas Schöley, Ben Schneider, Tomás Barcellos and Rich Pauloo
Furthermore ggplot(xy_points_dataframe) + geom_point() + coord_map() renders massively faster on my system compared to ggplot(point_sf) + geom_sf(). Dot-density maps of population counts are completely out of the question using geom_sf() on my system. They work wonderfully with geom_point() though...
# Draw a point at the centroid coordinates of each European region
library(tidyverse)
library(eurostat)
library(sf)
# using geom_sf
system.time({
p1 <-
eurostat_geodata_60 %>%
filter(LEVL_CODE == 3) %>%
st_centroid() %>%
ggplot() + geom_sf() + coord_sf(crs = st_crs(3785))
print(p1)
})
## user system elapsed
## 10.634 0.179 6.710
# using geom_point
system.time({
p2 <-
eurostat_geodata_60 %>%
filter(LEVL_CODE == 3) %>%
st_centroid() %>% st_coordinates() %>% as_data_frame() %>%
ggplot() + geom_point(aes(x = X, y = Y)) + coord_map(projection = 'mercator')
print(p2)
})
## user system elapsed
## 0.889 0.004 0.896
Personally I’m not sure if it is the goal of ggplot2 to optimise the input data. It might lead to unexpected results, e.g. in the stacking order of the different features...
If a fail-safe approach could be found it might be worth it but otherwise just documenting that multiple POINT will render slowly...
There’s no problem combining geom_sf and geom_point btw
Basically I agree with @thomasp85, but I feel this is unacceptably slow and should be fixed.
I don't want to let ggplot2 be the tool that only those who know how to "optimize" the data can use.
In the case of #2655, it was not the ggplot2's fault and it seems there's nothing we can do. But, in this case, considering plot.sfc_POINT() and plot.sfc_MULTIPOINT() can plot this 10+ times faster, I think something is wrong with the current implementation.
That said, I don't think my PR is a good way to fix this. Maybe is it better to implement st_as_grob() methods for sfc classes first on sf's side...?
At some point, we may have to bite the bullet and see if we can speed up grid. It seems absurd that we cannot draw thousands of points as individual grobs without experiencing extreme speed penalties. The same applies to drawing many individual text labels. Otherwise we will have to keep making design choices in ggplot2 that are driven by limitations of grid rather than by what would be a clean design.
I agree - as I’ve stated before I really hope to get some time to review the whole stack with performance in mind
My problem in combining POINT to multipoint is that it will result in all points being the same colour - also if a line or polygon were mixed in with the points, it would loose its ordering in the drawing stack
Hmm, thanks. I didn't notice the point that we can speed up grid. It's reasonable to wait for it and avoid adding complex tweaks. OK, please close this issue anytime.
One thing, this is NOT true, if you are talking about combining POINTs in ggplot2:
combining POINT to multipoint is that it will result in all points being the same colour
If this is about combining them outside ggplot2, you are right. That's why I felt this should happen inside draw_panel() to map aesthetics like colour.... (About the other concerns, I think you are right.)
Sorry, may I confirm one more thing? I wonder if this is really true.
It seems absurd that we cannot draw thousands of points as individual grobs without experiencing extreme speed penalties.
As I showed above (#2718 (comment)), most of the time is spent for sf_grob(), which is to build grobs, not to draw grobs. IIUC, improving grid can save time for draw.grid(), but this looks not the case to me.
I think many things are slow. If I look at the code for utils::modifyList() I want to cry. I think ggplot2 needs a clean and fast replacement of it for the purpose for which it is used. grid::gpar() also does tons of things that slow it down.
Considering that ultimately all gpar() does is create a list this is pretty awful:
Another example. I realize that my code reorders the list, but that may or may not matter. For aesthetics or other graphical parameters that are always named it doesn't matter. And I bet by implementing the entire function in C++ we could have similar or better speed without the reordering problem.
(Not sure why the maximum is so high though for update_list(). Maybe I'm missing something.)
I just wanted to loop @tmastny in here. We're both interested in improving performance and have been recently profiling ggplot2 and grid code. Helpfully Tim has more background in compiled code than I do. Likely this thread is not the place to organize them but Tim and I would be happy to talk more about known slowpoints or a wishlist of things to investigate.
Thanks so much for the nice codes and explanations!
If I look at the code for utils::modifyList() I want to cry.
I didn't notice that, but you are right. In fact, my profvis shows utils::modifyList() takes 3/4 of the time of lapply(..., sf_grob()) (The rest 1/4 is the overhead of the loop by lapply()...?).
But, for gpar() and pointsGrob(), which is called inside st_as_grob.POINT(), it seems to matter little here. So, to me, this is rather the problem on ggplot2's side for this particular issue. Generally saying, I agree with you in that grid can be improved significantly and it does benefit the performance of ggplot2 :)
Likely this thread is not the place to organize them
I'm not sure if this is the typical performance issue of ggplot2, but I'm happy to keep this issue open for that kind of discussion. 👍
Activity
clauswilke commentedon Jun 26, 2018
Could you check whether there is any relation to issue #2655?
yutannihilation commentedon Jun 26, 2018
Thanks, I roughly think this is a different issue because I'm using Windows (sorry I forgot to write this). But, I will check further.
yutannihilation commentedon Jun 27, 2018
Here's the result of profvis of the code that plots 1000
POINTs. This showsst_grob()takes most of the time.yutannihilation commentedon Jun 27, 2018
Here's a reprex. This shows
st_combine()significantly save time.Created on 2018-06-28 by the reprex package (v0.2.0).
Session info
yutannihilation commentedon Jun 28, 2018
Here's a rough implementation for this. Does this make sense? If so, I will try to make the code ready to merge.
(Sorry, I just wanted to make a PR on my repo to show codes, but I mistakenly created here... 🙄 )
https://github.com/tidyverse/ggplot2/pull/2722/files
jschoeley commentedon Jun 28, 2018
Furthermore
ggplot(xy_points_dataframe) + geom_point() + coord_map()renders massively faster on my system compared toggplot(point_sf) + geom_sf(). Dot-density maps of population counts are completely out of the question usinggeom_sf()on my system. They work wonderfully withgeom_point()though...thomasp85 commentedon Jun 28, 2018
Personally I’m not sure if it is the goal of ggplot2 to optimise the input data. It might lead to unexpected results, e.g. in the stacking order of the different features...
If a fail-safe approach could be found it might be worth it but otherwise just documenting that multiple POINT will render slowly...
There’s no problem combining geom_sf and geom_point btw
yutannihilation commentedon Jun 28, 2018
Basically I agree with @thomasp85, but I feel this is unacceptably slow and should be fixed.
I don't want to let ggplot2 be the tool that only those who know how to "optimize" the data can use.
In the case of #2655, it was not the ggplot2's fault and it seems there's nothing we can do. But, in this case, considering
plot.sfc_POINT()andplot.sfc_MULTIPOINT()can plot this 10+ times faster, I think something is wrong with the current implementation.That said, I don't think my PR is a good way to fix this. Maybe is it better to implement
st_as_grob()methods forsfcclasses first on sf's side...?clauswilke commentedon Jun 28, 2018
At some point, we may have to bite the bullet and see if we can speed up grid. It seems absurd that we cannot draw thousands of points as individual grobs without experiencing extreme speed penalties. The same applies to drawing many individual text labels. Otherwise we will have to keep making design choices in ggplot2 that are driven by limitations of grid rather than by what would be a clean design.
E.g., I find this comment on
geom_label()quite frustrating:https://ggplot2.tidyverse.org/reference/geom_text.html#geom-label
We shouldn't need two different geoms to draw text labels, one that is fast and one that can draw a colored background. It's not that complicated a problem.
thomasp85 commentedon Jun 28, 2018
I agree - as I’ve stated before I really hope to get some time to review the whole stack with performance in mind
My problem in combining POINT to multipoint is that it will result in all points being the same colour - also if a line or polygon were mixed in with the points, it would loose its ordering in the drawing stack
yutannihilation commentedon Jun 28, 2018
Hmm, thanks. I didn't notice the point that we can speed up grid. It's reasonable to wait for it and avoid adding complex tweaks. OK, please close this issue anytime.
One thing, this is NOT true, if you are talking about combining POINTs in ggplot2:
If this is about combining them outside ggplot2, you are right. That's why I felt this should happen inside
draw_panel()to map aesthetics likecolour.... (About the other concerns, I think you are right.)yutannihilation commentedon Jun 28, 2018
Sorry, may I confirm one more thing? I wonder if this is really true.
As I showed above (#2718 (comment)), most of the time is spent for
sf_grob(), which is to build grobs, not to draw grobs. IIUC, improving grid can save time fordraw.grid(), but this looks not the case to me.jschoeley commentedon Jun 28, 2018
I‘ve experienced the performance hit in the drawing, not the building stage.
clauswilke commentedon Jun 28, 2018
I think many things are slow. If I look at the code for
utils::modifyList()I want to cry. I think ggplot2 needs a clean and fast replacement of it for the purpose for which it is used.grid::gpar()also does tons of things that slow it down.Considering that ultimately all
gpar()does is create a list this is pretty awful:Created on 2018-06-28 by the reprex package (v0.2.0).
clauswilke commentedon Jun 28, 2018
Another example. I realize that my code reorders the list, but that may or may not matter. For aesthetics or other graphical parameters that are always named it doesn't matter. And I bet by implementing the entire function in C++ we could have similar or better speed without the reordering problem.
(Not sure why the maximum is so high though for
update_list(). Maybe I'm missing something.)Created on 2018-06-28 by the reprex package (v0.2.0).
dpseidel commentedon Jun 28, 2018
I just wanted to loop @tmastny in here. We're both interested in improving performance and have been recently profiling ggplot2 and grid code. Helpfully Tim has more background in compiled code than I do. Likely this thread is not the place to organize them but Tim and I would be happy to talk more about known slowpoints or a wishlist of things to investigate.
clauswilke commentedon Jun 28, 2018
One more example. Just by circumventing grid error checking and creating grobs directly we can get a ~3 fold speed increase for points.
grid.newpage() pushViewport(viewport()) grid.draw(do.call(gList, f3()))Created on 2018-06-28 by the reprex package (v0.2.0).
yutannihilation commentedon Jun 28, 2018
Thanks so much for the nice codes and explanations!
I didn't notice that, but you are right. In fact, my profvis shows
utils::modifyList()takes 3/4 of the time oflapply(..., sf_grob())(The rest 1/4 is the overhead of the loop bylapply()...?).But, for
gpar()andpointsGrob(), which is called insidest_as_grob.POINT(), it seems to matter little here. So, to me, this is rather the problem on ggplot2's side for this particular issue. Generally saying, I agree with you in that grid can be improved significantly and it does benefit the performance of ggplot2 :)I'm not sure if this is the typical performance issue of ggplot2, but I'm happy to keep this issue open for that kind of discussion. 👍
yutannihilation commentedon Mar 6, 2019
I believe this is closed by #3164 👍
lock commentedon Sep 2, 2019
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/