问题已解决,感谢asnwers
1szpjjfi1#
你需要创建一个调色板--一个命名的矢量,它将参与者的名字链接到颜色上。https://r-tmap.github.io/tmap-book/visual-variables.html#colors由于您没有共享您的数据,而且我自己查找一些英国数据也不实际,因此我将在随{sf}包提供的优秀的老北卡罗来纳州shapefile上演示该技术。作为奖励,我在选择“合适的”县名时玩得很开心。
{sf}
library(sf) library(dplyr) library(tmap) shape <- st_read(system.file("shape/nc.shp", package="sf")) # included with sf package shape <- shape %>% mutate(winner = case_when(NAME == "Halifax" ~ "Conservative", NAME == "Wilson" ~ "Labor", NAME == "Scotland" ~ "SNP", NAME == "Pitt" ~ "Libdem", T ~ NA)) %>% filter(!is.na(winner)) tm_shape(shape) + tm_polygons(col = "winner", title = "Results", # here is the action!!! palette = c("Conservative" = "blue", "Labor" = "red", "SNP" = "yellow", "Libdem" = "orange"))
62lalag42#
您需要创建自定义调色板:
library(tmap) library(dplyr) tmap_mode("view") data("World") states <- World %>% filter(name %in% c("Austria", "Germany", "Poland", "Hungary", "Czech Rep.", "Slovakia")) %>% mutate(name = factor(name, levels = c("Austria", "Germany", "Poland", "Hungary", "Czech Rep.", "Slovakia"))) # create custom color palette custpal = c("darkred", "orange", "palegreen", "dodgerblue", "purple3", "gold") tm_shape(states) + tm_polygons("name", palette = custpal)
2条答案
按热度按时间1szpjjfi1#
你需要创建一个调色板--一个命名的矢量,它将参与者的名字链接到颜色上。https://r-tmap.github.io/tmap-book/visual-variables.html#colors
由于您没有共享您的数据,而且我自己查找一些英国数据也不实际,因此我将在随
{sf}
包提供的优秀的老北卡罗来纳州shapefile上演示该技术。作为奖励,我在选择“合适的”县名时玩得很开心。
62lalag42#
您需要创建自定义调色板: