Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205343

In geom_sf_text, how to nudge x and y in aesthetics?

$
0
0

I would like to nudge the x and y position of text in ggplot2, using sf. The justification of text can be modified in aesthetics, which then requires nudging to be also modified similarly, but nudge_x and nudge_y can't be used in aesthetics.

Using sp and fortified data frames, I could modify x and y in aesthetics ( x=(x+hBump), y=(y+vBump) . I don't know if that is possible in sf.

Here are some data:

Cities <- read.table( text=
  "City long    lat hJust   vJust   hBump   vBump
  GrandJunction -108.550649 39.063871   1   1   -1100   -1000
  Gunnison  -106.925321 38.545825   0   1   0   -1000
  Tincup    -106.47836  38.754439   1   0   0   800",
  header=TRUE )

Convert to sf

Cities.sf <- st_as_sf(
  x = Cities, 
  coords = c("long", "lat"),
  crs = "+proj=longlat +datum=WGS84"
)

ggplot (the coord_sf is just so labels don't get off)

ggplot(Cities.sf) + 
  geom_sf() + 
  coord_sf(xlim = c(-109, -106.3), ylim = c(38.4, 39.2)) +
  geom_sf_text(aes(label = City, hjust = hJust, vjust = vJust))

enter image description here

Now if we nudge for one, the others may get messed up. Need aesthetic nudging!

ggplot(Cities.sf) + 
  geom_sf() + 
  coord_sf(xlim = c(-109, -106.3), ylim = c(38.4, 39.2)) +
  geom_sf_text(
    aes(label = City, hjust = hJust, vjust = vJust),
    nudge_x = 0.025, nudge_y = -0.025
  )

enter image description here Side question - I guess if I transform the object to another crs using meters, then the nudge units have to change from degrees to meters? That is the units that hBump and vBump are in.


Viewing all articles
Browse latest Browse all 205343

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>