Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add text method #2399

Merged
merged 2 commits into from
May 22, 2024
Merged

add text method #2399

merged 2 commits into from
May 22, 2024

Conversation

btupper
Copy link
Contributor

@btupper btupper commented May 20, 2024

Hi,

It very much appeals to my inner laziness to have a text() method. For any sf object this will automatically place a label at each feature's centroid. If somebody wanted finer control, they could make a POINT table to plot. I know you don't want to add more stuff to sf, but this feels pretty unobstrusive.

What do you think?

library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.5.0, PROJ 9.0.0; sf_use_s2() is TRUE
nc = st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE)
plot(nc["SID74"], reset = FALSE)
text(nc, labels = substring(nc$NAME,1,1))

Created on 2024-05-20 with reprex v2.0.2

@edzer
Copy link
Member

edzer commented May 20, 2024

I think this is a good idea, but please

  • add of_largest_polygon = FALSE after the ... and pass this on to st_centroid
  • don't use |> in the function (it would make sf no longer work with R versions prior to 4.1)

Maybe row.names(x) is a more sensible default for labels?

@btupper
Copy link
Contributor Author

btupper commented May 20, 2024

Removing pipes gave me a good laugh - I forgot there was a time before pipes.

#' each feature in \code{x}. Provide POINT features for full control of placement.
#' @examples
#' text(nc, labels = substring(nc$NAME,1,1))
text.sf = function(x, labels = row.names, ..., of_largest_polygon = FALSE){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

labels = row.names(x)

#' text(nc, labels = substring(nc$NAME,1,1))
text.sf = function(x, labels = row.names, ..., of_largest_polygon = FALSE){
x = st_geometry(x)
x = st_centroid(x, of_largest_polygon = FALSE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of_largest_polygon = of_largest_polygon

edzer added a commit that referenced this pull request May 22, 2024
edzer added a commit that referenced this pull request May 22, 2024
@edzer edzer merged commit fbf0cb2 into r-spatial:main May 22, 2024
1 of 9 checks passed
edzer added a commit that referenced this pull request May 22, 2024
@edzer
Copy link
Member

edzer commented May 22, 2024

Thanks!

@btupper
Copy link
Contributor Author

btupper commented May 22, 2024

Well, thank you for your patience with my embarrassing show.

@edzer
Copy link
Member

edzer commented May 22, 2024

Next up would be points and lines methods, right?

@btupper
Copy link
Contributor Author

btupper commented May 22, 2024

Oh, geez, I hadn't thought of that. There is quite a menagerie that might be included...

polygon, polypath, symbols and sunflowerplot

I have been thinking about using sf for a project using flow cytometry data... the geospatial tools would work just fine for gate analyses.

@edzer
Copy link
Member

edzer commented May 22, 2024

too bad that xy.coords is not a generic...

edzer added a commit that referenced this pull request May 22, 2024
@edzer
Copy link
Member

edzer commented May 22, 2024

I think I'll stop here, for lines it is less trivial what it should do.

@btupper
Copy link
Contributor Author

btupper commented May 23, 2024

Oooo, I see. I assumed that all of the graphics package functions were generic. Bummer. Well, thanks for text and points!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants