【发布时间】:2019-10-13 13:17:58
【问题描述】:
我正在尝试根据一些定义的类别创建显示井位置的地图。我将每口井的水位相对于它们的完整记录进行了分类,将水位分类为“最高”、“高于正常”、“正常”、“低于正常”、“最低”。使用 tmap,我想使用以下颜色显示这些条件中的每一个“最高”= 深蓝色,“高于正常”= 浅蓝色,“正常”=绿色,“低于正常”=黄色,“最低-ever" = 红色
到目前为止,我已经创建了一个用于映射的 sf 文件
Library(tidyverse)
library(sf)
library(spData)
Library(tmap)
Library(tmaptools)
我的数据
test <- structure(list(well = c(3698L, 3697L, 4702L, 15001L, 1501L, 3737L,
1674L, 5988L, 1475L, 15017L), con = c("N", "B", "H", "B", "L",
"B", "N", "A", "N", "B"), x = c(2834091L, 2838342L, 2802911L,
2845228L, 2834408L, 2834452L, 2838641L, 2834103L, 2803192L, 2929417L
), y = c(6166870L, 6165512L, 6125649L, 6174527L, 6161309L, 6168216L,
6170055L, 6164397L, 6140763L, 6227467L)), row.names = c(NA, -10L
), class = c("tbl_df", "tbl", "data.frame"))
为映射创建 sf 文件
test_sf <-test %>%
st_as_sf(coords = c("x","y"),crs = 27200,agr="constant")
获取背景图
HB_map <- nz %>%
filter(Name=="Hawke's Bay") %>%
read_osm(type = "stamen-terrain")
在地图上绘制数据
qtm(HB_map)+ #this is part of tmap and used to draw a thematic map plot
tm_shape(nz %>% filter(Name=="Hawke's Bay"))+ #define data source
tm_borders(col = "grey40", lwd = 2, lty = "solid", alpha = NA)+
tm_shape(test_sf)+
tm_dots("con",palette=c(H='blue',A='cyan',N='green',B='yellow',L='red'),stretch.palette = FALSE,size = 0.4,shape =21)
地图生成正确的颜色,但未与正确的类别相关联。我可以更改颜色的顺序,但地图并不总是包含每个类别,因此颜色会再次被错误分配(如果有意义的话)
【问题讨论】: