Skip to content

Unable to add text annotation to geom_sf plot #2971

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

Closed
sschloss1 opened this issue Oct 31, 2018 · 1 comment · Fixed by #2972
Closed

Unable to add text annotation to geom_sf plot #2971

sschloss1 opened this issue Oct 31, 2018 · 1 comment · Fixed by #2972

Comments

@sschloss1
Copy link

I would like to use relative positioning (0-1 scale) to add a text annotation to a ggplot object that uses geom_sf. I've tried two different methods that were mentioned here, but both seem to fail with different results.

First attempt, using Hadley's suggestion does produce any annotation:

library(ggplot2)
ggplot(nc, aes(fill=AREA)) + 
  geom_sf() + 
  annotate("text", -Inf, Inf, label = "Top-left", hjust = 0, vjust = 1)

I never see "Top-left" on the plot no matter what values I've tried for hjust or vjust.

Second attempt, using textGrob (via this comment):

library(sf)
library(ggplot2)
library(grid)
nc = st_read(system.file("shape/nc.shp", package="sf"))
ggplot(nc) +geom_sf(aes(fill = AREA))+
	annotation_custom(textGrob(label="xx"))

This results in "Error in grid.Call.graphics(C_setviewport, vp, TRUE): non-finite location and/or size for viewport". I get the same results if I specify x and y locations in textGrob with different units (npc, cm, lines).

Any help would be most welcome. Thanks.

(This was originally posted to the sf repo but was posted here at the suggestion of yutannihilation who said "I guess coord_sf() forgets to squash Inf within the range, which coord_cartesian() does.")

@lock
Copy link

lock bot commented May 26, 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/

@lock lock bot locked and limited conversation to collaborators May 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant