如何在R

时间:2017-08-15 12:50:20

标签: r ggplot2 fonts character-encoding

我正在ggplot2 R中尝试制作地图。我在rgdal包中使用此功能:

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

比我:

fortify(mapa)

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

它工作正常,但是当我看到

head(mapa)

我看到县的名字没有瑞典字体(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>

如何解决这个问题?

> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=Swedish_Sweden.1252  LC_CTYPE=Swedish_Sweden.1252    LC_MONETARY=Swedish_Sweden.1252
[4] LC_NUMERIC=C                    LC_TIME=Swedish_Sweden.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base 

1 个答案:

答案 0 :(得分:3)

尝试在阅读中添加encoding参数:

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

to specify the encoding您的文件使用的字符集。这是一个很好的选择,它将是UTF-8。