1

I am trying to do the map in ´ggplot2´ in ´R´. I use this function from rgdal package:

mapa <- readOGR(dsn=path.expand("C:/~/SWE_adm_shp"),layer="SWE_adm2")

than I do:

fortify(mapa)


ggplot(mapa, aes(x = long, y = lat, group = group)) +
  geom_polygon(fill = "blue", col = "white") +
  coord_map() 

It works fine but when I look at the

head(mapa) 

I see that the names of the counties dont have the Swedish font (NAME_2)

ID_0 ISO NAME_0 ID_1         NAME_1 ID_2      NAME_2   HASC_2 CCN_2 CCA_2   TYPE_2    ENGTYPE_2
0  222 SWE Sweden    1 Östergötland    1   Ödeshög SE.OG.OD     0  <NA> Kommuner Municipality
1  222 SWE Sweden    1 Östergötland    2 Åtvidaberg SE.OG.AT     0  <NA> Kommuner Municipality
2  222 SWE Sweden    1 Östergötland    3     Boxholm SE.OG.BO     0  <NA> Kommuner Municipality
3  222 SWE Sweden    1 Östergötland    4   Finspång SE.OG.FI     0  <NA> Kommuner Municipality
4  222 SWE Sweden    1 Östergötland    5       Kinda SE.OG.KI     0  <NA> Kommuner Municipality
5  222 SWE Sweden    1 Östergötland    6  Linköping SE.OG.LI     0  <NA> Kommuner Municipality
  NL_NAME_2 VARNAME_2
0      <NA>      <NA>
1      <NA>      <NA>
2      <NA>      <NA>
3      <NA>      <NA>
4      <NA>      <NA>

How to work over that?

1

If you are in Sweden your system encoding is probably "latin1" and since you are reading in the shapefile without specifying the encoding of the text; R assumes that the encoding is your native system encoding ("latin1"). Try specifying the encoding as "UTF-8":

mapa <- readOGR(dsn=path.expand("C:/~/SWE_adm_shp"),
                layer="SWE_adm2",
                encoding="UTF-8")

Of course the reverse could also be possible (system encoding is UTF-8 and your file is latin-1), in which case this will work:

mapa <- readOGR(dsn=path.expand("C:/~/SWE_adm_shp"),
                layer="SWE_adm2",
                encoding="latin1")

You can read this for more details: https://cran.r-project.org/web/packages/rgdal/vignettes/OGR_shape_encoding.pdf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.