Show progress of calculations in ggplot2

Viewed 222

I was wondering how to show the progress of a ggplot2 or ggmap operation while it is calculating. I'm working with some very large shapefiles and they can take several minutes to display the plot. I know there are several packages and functions that allow you to insert a progress bar or percentage during a calculation (eg. the "progress" package), but I can't figure out how to insert it inside the ggplot operation.

So, example below:

library (ggplot2)
library (sf)
     
ggplot() +
  geom_sf(data = "myshapefile.shp", size = 1, color = "black", fill = "red") + 
  ggtitle("Testplot") + 
  coord_sf()

This shapefile takes several minutes to render into a plot. Rather than just sitting there waiting for it to show up and not knowing when it will finish or if it is stuck. I would like to see some kind of progress (bar or percentage, doesn't matter - eg. showing when it is 10%, 20%, 30%, etc. done).

1 Answers

Hadley Wickham, the creator of ggplot2, says a progress bar is not possible here.

enter image description here

His suggestion to simplify the shapefile will remove vertices to a certain tolerance, and will usually make it render faster without any perceivable difference in the figure. It can be done like so.

library (ggplot2)
library (sf)
my_shapefile <- read_sf("myshapefile.shp")

# You'll have to test different tolerance levels here. Higher
# values will produce more rigid looking shapes which will render faster. 
# I would try values of 0.01, 0.1, 1, and 10.
my_shapefile <- st_simplify(my_shapefile, dTolerance = 0.1)

ggplot() +
  geom_sf(data = my_shapefile, size = 1, color = "black", fill = "red") + 
  ggtitle("Testplot") + 
  coord_sf()
Related