简体   繁体   English

如何使用tmap手动设置R中的点颜色

[英]How to manually set colour of dots in R using tmap

I'm trying to create a map showing the location of wells based on some defined categories. 我正在尝试根据一些定义的类别创建一个显示井位置的地图。 I have categorized each well's water level relative to their full record, categorizing the water levels into terms "Highest-ever", "above-normal", "normal", "below-normal", "Lowest-ever". 我根据他们的完整记录对每个井的水位进行了分类,将水位分为“有史以来最高”,“高于正常”,“正常”,“低于正常”,“有史以来最低”。 Using tmap, I would like to show each of these conditions using the following colors "Highest-ever" = dark blue, "above-normal" = light blue, "normal"=green,"below-normal"=yellow, "Lowest-ever" = red 使用tmap,我想使用以下颜色显示每个条件“最高的”=深蓝色,“高于正常”=浅蓝色,“正常”=绿色,“低于正常”=黄色,“最低” - “= =红色

So far I have created an sf file for mapping 到目前为止,我已经创建了一个用于映射的sf文件

Library(tidyverse)
library(sf)
library(spData)
Library(tmap)
Library(tmaptools)

My data 我的数据

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"))

Create sf file for mapping 创建用于映射的sf文件

test_sf <-test %>% 
  st_as_sf(coords = c("x","y"),crs = 27200,agr="constant")

Obtain background map 获取背景地图

HB_map <- nz %>% 
  filter(Name=="Hawke's Bay") %>% 
  read_osm(type = "stamen-terrain")

Plot data over map 在地图上绘制数据

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)

The map produces the right colours but not associated with the correct categories. 地图生成正确的颜色,但与正确的类别无关。 I could change the order of the colours but the map won't always contain each category and thus the colours would be wrongly assigned again (if that makes sense) 我可以改变颜色的顺序但是地图并不总是包含每个类别,因此颜色会被错误地分配(如果这是有道理的)

You can convert your con -column to factor. 您可以将con column转换为factor。 The default order of the levels will be alphabetic, that's how you can assign the colors. 级别的默认顺序将是字母,即您可以分配颜色的方式。 If a level is empty, it will still be included in the legend. 如果某个级别为空,则它仍将包含在图例中。

test_sf$con <- as.factor(test_sf$con)

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[test_sf$con != "H",])+  
  tm_dots(col = "con", palette=c(A='cyan', B='yellow', H='blue',L='red',N='green'), stretch.palette = FALSE,size = 0.4,shape =21)

在此输入图像描述

( read_osm() doesn't work for me..) read_osm()对我不起作用..)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM