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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ S3method(st_zm,sfg)
S3method(str,sfc)
S3method(summary,sfc)
S3method(t,sgbp)
S3method(text,sf)
S3method(transform,sf)
S3method(vec_cast.sfc,default)
S3method(vec_cast.sfc,sfc)
Expand Down
19 changes: 19 additions & 0 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,25 @@ sf.colors = function (n = 10, cutoff.tails = c(0.35, 0.2), alpha = 1, categorica
}
}

# Add text to an existing graphic
#
#' @param labels character, text to draw (one per row of input)
#' @param of_largest_polygon logical, passed on to \code{st_centroid}
#' @name plot
#' @export
#' @details Adds text to an existing graphic. Text is placed at the centroid of
#' 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)

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

xy = st_coordinates(x)
text(xy[,1], xy[,2], labels = labels, ...)
}



# get the aspect ratio of a bounding box, for geodetic coords true scale at mid latitude:
get_asp = function(bb) {
asp = diff(bb[c(2,4)])/diff(bb[c(1,3)])
Expand Down
20 changes: 11 additions & 9 deletions man/dbWriteTable.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions man/plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading