geom_sf_text() change position of only one text/label

Hi,

I created this plot

but I would like to change the position of the text "Středočeský."

library(ggplot)
library(sf)
ggplot(data = sf_nezamestnanost) +
  geom_sf(aes(fill = nezamestnanost), colour = "grey10",size=0.2)+
  scale_fill_gradientn(colours= rev(palette))+
  labs(title = "Nezaměstnanost v 1. čtvrtletí 2020",
       fill = "") +
  theme(legend.text.align = 1,
        legend.title.align = 0.5,
        plot.title = element_text(hjust = .1,vjust=0.2,size=18,family="Times"),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.text.x=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks=element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank())+
  geom_sf_text(aes(label =nazev),size=3,family="sans")+
  ggsave(filename="kraje.png",dpi="print")

Could you help me with that, please?

Thank you

See https://stackoverflow.com/questions/59522531/in-geom-sf-text-how-to-nudge-x-and-y-in-aesthetics and especially the very last remark by Jim Warrell.
This could help you (?)

Jakub,

when I read the remark by Jim Warrell in the article mentioned above I thought meant the following.

Because I don't have your data set I use a map of (part of) Europe as an example,
where I want (again as an example and no political intentions :wink: ) to move (nudge)
the text that indicates 'Poland' to another place.
So I

  • plot the map with the default nudges (0)
  • add the nudges (in x and y direction). They are 0 for all data with an exception for Poland.
    To avoid confusion I changed the text for Poland to 'Nudged Poland'
  • plot the map again with my own nudges (called my_nudge_x and my_nudge_y)
library("dplyr")
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library("rnaturalearth")
library("rgeos")
#> Loading required package: sp
#> rgeos version: 0.5-3, (SVN revision 634)
#>  GEOS runtime version: 3.8.0-CAPI-1.13.1 
#>  Linking to sp version: 1.4-1 
#>  Polygon checking: TRUE
library("ggplot2")

world <- ne_countries(scale = "medium", continent='europe',
                      returnclass = "sf")  

ggplot(data = world) +
  geom_sf(aes(fill=admin)) +
  coord_sf(xlim = c(-10, 40), ylim = c(35, 70))  +
  geom_sf_text(aes(label = admin),size=2,family="sans") +
  guides(fill=FALSE)
#> Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
#> give correct results for longitude/latitude data

  

world2 <- world %>%
  mutate(my_nudge_x=ifelse(admin=='Poland',15,0) , 
         my_nudge_y=ifelse(admin=='Poland',5,0) ,
         admin = ifelse(admin=='Poland','Nudged Poland',admin)
  )
   
ggplot(data = world2) +
  geom_sf(aes(fill=admin)) +
  coord_sf(xlim = c(-10, 40), ylim = c(35, 70))  +
  geom_sf_text(aes(label = admin),size=2,family="sans",
               nudge_x=world2$my_nudge_x,nudge_y=world2$my_nudge_y) +
  guides(fill=FALSE)
#> Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
#> give correct results for longitude/latitude data

Created on 2020-07-17 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

Sorry, I don't understand the solution provided.

I came across this oldish post by accident, and as the topic is close to my heart, and as I don't see it satisfactorily resolved, I propose another solution.

My solution builds upon the nudge_x and nudge_y arguments of geom_sf_text() - these can contain vectors of "nudges" in x and y direction for all labels.

In Czech context it is almost always necessary to nudge the label for Středočeský kraj (because of the gaping hole called Prague) and often for Olomoucký kraj because of its odd shape.

In tricky situations I have found it easier to place labels via sf::st_centroid() rather than the default sf::st_point_on_surface().

library(ggplot2)
library(RCzechia)

ggplot() +
  geom_sf(data = kraje("low"),
          fill = "gray95") +
  geom_sf_text(data = kraje("low"),
               aes(label = KOD_CZNUTS3),
               nudge_x = c(0, .15, rep(0, 10), 0, 0),
               nudge_y = c(0, -.2, rep(0, 10), -.15, 0),
               fun.geometry = sf::st_centroid) +
  geom_sf(data = RCzechia::republika("low"), 
          color = "gray20", 
          fill = NA, 
          lwd = 1) +
  theme_void()