Skip to content

Unable to add text annotation to geom_sf plot #879

Closed

Description

@sschloss1

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.

Activity

  1. yutannihilation commented on Oct 31, 2018

    @yutannihilation
    Contributor

    Could you file this issue on ggplot2's repo? I guess coord_sf() forgets to squash Inf within the range, which coord_cartesian() does.

  2. sschloss1 commented on Oct 31, 2018

    @sschloss1
    Author

    Filed with ggplot2 at tidyverse/ggplot2#2971

  3. yutannihilation commented on Oct 31, 2018

    @yutannihilation
    Contributor

    Thanks! I think you can close this issue here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions