英文:
Custom Tmap palette based on results
问题
I'm trying to create a custom palette for tmap in R where the colours represent a result from a general election.
在R中,我试图创建一个自定义调色板,其中颜色代表大选的结果。
In the below photo you can see my plot doesn't have a palette attached and is using the default colours, is it possible to assign colours to each of the possible results, eg SNP = yellow, Liberal Democrat = Orange, Labour = red and Conservative = blue?
在下面的照片中,您可以看到我的图没有附加调色板,并且使用默认颜色,是否可以为每个可能的结果分配颜色,例如,SNP = 黄色,自由民主党 = 橙色,工党 = 红色,保守党 = 蓝色?
My code for the plot currently is:
我目前的绘图代码如下:
output$map <- renderTmap({
tm_shape(filterConst()) +
tm_polygons(
"first_party",
title = "Results",
popup.vars = c("Winner:" = "first_party", "MP" = "mp"),
zindex = 401
) +
tm_layout(frame = FALSE) +
tmap_mode("view")
})
Any help would be appreciated!
将不胜感激任何帮助!
Have tried using a custom palette, but when filtering at constituency level, the palette does not represent the parties' colors.
尝试使用自定义调色板,但在选区级别筛选时,调色板不代表各党派的颜色。
英文:
I'm trying to create a custom palette for tmap in R where the colours represent a result from a general election.
In the below photo you can see my plot doesn't have a palette attached and is using the default colours, is it possible to assign colours to each of the possible results, eg SNP = yellow, Liberal Democrat = Orange, Labour = red and Conservative = blue?
My code for the plot currently is:
output$map <- renderTmap({
tm_shape(filterConst()) +
tm_polygons(
"first_party",
title = "Results",
popup.vars = c("Winner:" = "first_party", "MP" = "mp"),
zindex = 401
) +
tm_layout(frame = FALSE) +
tmap_mode("view")
})
Any help would be appreciated!
Have tried using a custom palette, but when filtering at constituency level, the palette does not represent the parties colour
答案1
得分: 1
你需要创建一个调色板 - 一个将政党名称与颜色相连的命名向量。有关更多信息,请参考 tmap 书 https://r-tmap.github.io/tmap-book/visual-variables.html#colors
由于你没有分享你的数据,而且我自己查找一些英国数据也不切实际,我将在附带 {sf}
包的好老的北卡罗来纳 shapefile 上演示这种技巧。
作为奖励,我玩得很开心地选择了"适当的"县名...
library(sf)
library(dplyr)
library(tmap)
shape <- st_read(system.file("shape/nc.shp", package="sf")) # 包含在 sf 包中
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",
# 这里是关键部分!!!
palette = c("Conservative" = "blue",
"Labor" = "red",
"SNP" = "yellow",
"Libdem" = "orange"))
[1]: https://i.stack.imgur.com/acWNw.png
<details>
<summary>英文:</summary>
You will need to create a palette - a named vector linking the names of parties to colors. For more info consider consulting the tmap Book https://r-tmap.github.io/tmap-book/visual-variables.html#colors
Since you did not share your data, and it was not practical for me to look some UK data up myself, I am demonstrating the technique on the good ol' North Carolina shapefile that ships with the `{sf}` package.
As a bonus I had fun with choosing the "proper" county names...
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"))
[![four counties in North Carolina, color coded][1]][1]
[1]: https://i.stack.imgur.com/acWNw.png
</details>
# 答案2
**得分**: 0
你需要创建一个自定义的颜色调色板:
```R
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")))
# 创建自定义颜色调色板
custpal = c("darkred", "orange", "palegreen", "dodgerblue", "purple3", "gold")
tm_shape(states) +
tm_polygons("name", palette = custpal)
英文:
You need to create a custom color palette:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论