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
ggplot2 doesn't use rgdal, and just removed rgeos in #5242. However, maptools is still used in fortify.SpatialPolygonsDataFrame() (and the examples section of the document).
Note that the reason I couldn't decide is that I was afraid this assumption
everyone has moved to sf by now
might not be really true. For example, I read a book about spatial statistics using R, which was published in 2022, and it still uses rgoes and rgdal in several places although it primarily uses sf. I thought this probably means, even if the internal implementations were already replaced with sf, these longstanding packages might still generate the result as sp (I'm not familiar with spatial statistics, so I'm not sure).
But, converting an sp object to sf should be easy, and the sp ecosystem is about to retire anyway. So, it's probably time to drop it.
Alternatively, if ggplot2 still has a sf dependency, we could switch to sf::st_union() instead. Or maybe fortify.SpatialPolygonsDataFrame() could convert to an sf object and then fortify that?
Hey there. I'm currently maintaining a very old spatial data codebase that heavily used sp for spatial data manipulation and ggplot2 for plotting. As a part of it I did both plots of point and raster data while plotting using a background shapefile like this:
I have several functions (one for points, one for points + text, one for rasters) that plot the data using custom x and y aesthetics and then plot the shapefile on top. To plot the base shapefile I'm using a single function that serves all 3 methods and uses fortify on a SpatialPolygonsDataFrame.
I take it it's because of this? Would it be possible to implement the functionality using sf as you suggest?
Or is there a way I can implement an alternative version of it in my codebase?
Thanks in advance
Yes, will definitely try to go that way, but this is an older codebase that is currently in maintenance mode, it's going to be a major change to migrate all of it to sf. I will try to start doing it gradually but it's not going to be an easy task.
@Ludecan if it's in maintenance mode, you could use renv::snapshot() to capture all the dependencies as of a certain date, and just continue to use sp.
@Ludecan if it's in maintenance mode, you could use renv::snapshot() to capture all the dependencies as of a certain date, and just continue to use sp.
Thanks @hadley, I tried your suggestion and it worked fine.
Then I realized I don't need to fortify the entire SpatialPolygonsDataFrame but rather only it's geometry, which is a SpatialPolygons object for which fortify doesn't seem to use maptools. This was the only use in my codebase so I dropped the dependency altogether.
I'll get into removing usage of rgdal and rgeos from the rest of my code but this will give me some time to do so.
Thanks for your help!
It seems now nothing can directly replace ggplot2::fortify for SpatialPolygonsDataFrame. People used broom::tidy before but it is also retired (as it also depends on maptools. This is unfortunately inconvenient.
After testing, the best solution is to paste script into ChatGTP and ask it to do the same in sf.
Activity
thomasp85 commentedon Mar 22, 2023
I'm fine with either 1 or 3
The only argument for 1 is to preserve the function of very old code as I assume everyone has moved to sf by now
yutannihilation commentedon Jun 4, 2023
https://geocompx.org/post/2023/rgdal-retirement/#solutions-for-package-developers
hadley commentedon Jun 7, 2023
I think we can drop it. We should schedule this for the next release.
yutannihilation commentedon Jun 7, 2023
Thanks, let's drop it.
Note that the reason I couldn't decide is that I was afraid this assumption
might not be really true. For example, I read a book about spatial statistics using R, which was published in 2022, and it still uses rgoes and rgdal in several places although it primarily uses sf. I thought this probably means, even if the internal implementations were already replaced with sf, these longstanding packages might still generate the result as sp (I'm not familiar with spatial statistics, so I'm not sure).
But, converting an sp object to sf should be easy, and the sp ecosystem is about to retire anyway. So, it's probably time to drop it.
hadley commentedon Jun 8, 2023
Alternatively, if ggplot2 still has a sf dependency, we could switch to
sf::st_union()instead. Or maybefortify.SpatialPolygonsDataFrame()could convert to an sf object and then fortify that?thomasp85 commentedon Jun 8, 2023
Agreed - leaning heavily into dropping it
Ludecan commentedon Nov 27, 2023
Hey there. I'm currently maintaining a very old spatial data codebase that heavily used

spfor spatial data manipulation andggplot2for plotting. As a part of it I did both plots of point and raster data while plotting using a background shapefile like this:I have several functions (one for points, one for points + text, one for rasters) that plot the data using custom x and y aesthetics and then plot the shapefile on top. To plot the base shapefile I'm using a single function that serves all 3 methods and uses
fortifyon aSpatialPolygonsDataFrame.I take it it's because of this? Would it be possible to implement the functionality using
sfas you suggest?Or is there a way I can implement an alternative version of it in my codebase?
Thanks in advance
teunbrand commentedon Nov 27, 2023
I think the recommendation from r-spatial is to move away from {sp} in favour of {sf}. More information can be found in the readme of https://github.com/r-spatial/evolution with various links to other places.
Ludecan commentedon Nov 27, 2023
Yes, will definitely try to go that way, but this is an older codebase that is currently in maintenance mode, it's going to be a major change to migrate all of it to
sf. I will try to start doing it gradually but it's not going to be an easy task.hadley commentedon Nov 27, 2023
@Ludecan if it's in maintenance mode, you could use
renv::snapshot()to capture all the dependencies as of a certain date, and just continue to use sp.Ludecan commentedon Nov 30, 2023
Thanks @hadley, I tried your suggestion and it worked fine.
Then I realized I don't need to fortify the entire
SpatialPolygonsDataFramebut rather only it's geometry, which is aSpatialPolygonsobject for whichfortifydoesn't seem to usemaptools. This was the only use in my codebase so I dropped the dependency altogether.I'll get into removing usage of
rgdalandrgeosfrom the rest of my code but this will give me some time to do so.Thanks for your help!
teunbrand commentedon Mar 5, 2024
Closing this as fixed by #5327.
liuyanguu commentedon Oct 14, 2024
It seems now nothing can directly replace
ggplot2::fortifyforSpatialPolygonsDataFrame. People usedbroom::tidybefore but it is also retired (as it also depends onmaptools. This is unfortunately inconvenient.After testing, the best solution is to paste script into ChatGTP and ask it to do the same in
sf.