[英]R - Plot with tmap() does not display the legend
I use the tmap
package. 我使用tmap
包。 I plot a map with the qtm()
function but the legend is not displayed. 我使用qtm()
函数绘制了地图,但图例未显示。 Indeed, I understand that the legend is automatically display, unless you specify to hide it. 的确,我知道图例会自动显示,除非您指定将其隐藏。
I also try to use a work-around with the tm_add_legend()
but it doesn't work. 我也尝试对tm_add_legend()
使用tm_add_legend()
但是它不起作用。
Here is my code : 这是我的代码:
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")
And a link to the geographic data : https://drive.google.com/file/d/1wksMnkufQPdOfSbhWoZbPKMqlcLcll_j 以及指向地理数据的链接: https : //drive.google.com/file/d/1wksMnkufQPdOfSbhWoZbPKMqlcLcll_j
A solution who has three advantages : 具有三个优点的解决方案:
classIntervals
call to discretize the continuous variable before the plot 避免在classIntervals
前使用classIntervals
调用离散化连续变量 The code : 编码 :
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"))
The plot : 剧情 :
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.