R - 使用tmap()的绘图不显示图例

时间:2017-10-25 15:50:16

标签: r plot tmap

我使用tmap包。我使用qtm()函数绘制地图,但不显示图例。实际上,我知道图例会自动显示,除非您指定隐藏它。

我也尝试使用tm_add_legend()解决方法,但它不起作用。

MyPlot

这是我的代码:

library(sp) ; library(rgdal) ; library(rgeos) ;
library(tmap) ; library(maptools)

Directory <- ""

MapFrance <- readOGR(dsn = paste0(Directory, "Departements"),  layer = "DEPARTEMENT")

MapFrance@data$Score <- round(runif(96, min = 1, max = 10), 0)

brk <- c(1, 2.5, 5, 7.5, 10)
ClassScore <- classIntervals(MapFrance@data$Score, style = "fixed", fixedBreaks = brk)
Palette <- brewer.pal(length(brk) - 1, "Greens")

MapFrance@data$Categorie <- as.character(cut(MapFrance@data$Score,
                                             breaks = ClassScore$brks,
                                             labels = Palette,
                                             include.lowest = TRUE))

ExampleData <- MapFrance[MapFrance$CODE_REG == "11", ]

qtm(ExampleData, fill = "Categorie", text = "CODE_DEPT", text.size = "AREA", style = "gray",
    text.root = 5, fill.title = "Opinion scale")

指向地理数据的链接: https://drive.google.com/file/d/1wksMnkufQPdOfSbhWoZbPKMqlcLcll_j

1 个答案:

答案 0 :(得分:0)

有三个优势的解决方案:

  • 解决传奇问题
  • 解决标题问题
  • 避免classIntervals调用以在情节
  • 之前对连续变量进行离散化

代码:

library(sp) ; library(rgdal) ; library(rgeos) ;
library(tmap) ; library(maptools)

Directory <- ""

MapFrance <- readOGR(dsn = paste0(Directory, "Departements"),  layer = "DEPARTEMENT")

MapFrance@data$Score <- round(runif(96, min = 1, max = 10), 0)

ExampleData <- MapFrance[MapFrance$CODE_REG == "11", ]

brk <- c(1, 2.5, 5, 7.5, 10)

tm_shape(ExampleData) +
  tm_fill("Score",
          title = "Satisfaction score",
          style = "fixed",
          breaks = brk,
          palette = brewer.pal(length(brk) - 1, "Greens"),
          auto.palette.mapping = FALSE) +
  tm_borders() +
  tm_text("CODE_DEPT",
          size = "AREA",
          root = 10) +
tm_layout(main.title = "Satisfaction score in France",
          legend.position = c("left", "bottom"))

情节:

ThePlot