自定义Tmap调色板基于结果

huangapple go评论99阅读模式
英文:

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:

我目前的绘图代码如下:

  1. output$map <- renderTmap({
  2. tm_shape(filterConst()) +
  3. tm_polygons(
  4. "first_party",
  5. title = "Results",
  6. popup.vars = c("Winner:" = "first_party", "MP" = "mp"),
  7. zindex = 401
  8. ) +
  9. tm_layout(frame = FALSE) +
  10. tmap_mode("view")
  11. })

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?

自定义Tmap调色板基于结果

My code for the plot currently is:

  1. output$map <- renderTmap({
  2. tm_shape(filterConst()) +
  3. tm_polygons(
  4. "first_party",
  5. title = "Results",
  6. popup.vars = c("Winner:" = "first_party", "MP" = "mp"),
  7. zindex = 401
  8. ) +
  9. tm_layout(frame = FALSE) +
  10. tmap_mode("view")
  11. })

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 上演示这种技巧。

作为奖励,我玩得很开心地选择了"适当的"县名...

  1. library(sf)
  2. library(dplyr)
  3. library(tmap)
  4. shape <- st_read(system.file("shape/nc.shp", package="sf")) # 包含在 sf 包中
  5. shape <- shape %>%
  6. mutate(winner = case_when(NAME == "Halifax" ~ "Conservative",
  7. NAME == "Wilson" ~ "Labor",
  8. NAME == "Scotland" ~ "SNP",
  9. NAME == "Pitt" ~ "Libdem",
  10. T ~ NA)) %>%
  11. filter(!is.na(winner))
  12. tm_shape(shape) +
  13. tm_polygons(col = "winner",
  14. title = "Results",
  15. # 这里是关键部分!!!
  16. palette = c("Conservative" = "blue",
  17. "Labor" = "red",
  18. "SNP" = "yellow",
  19. "Libdem" = "orange"))

自定义Tmap调色板基于结果

  1. [1]: https://i.stack.imgur.com/acWNw.png
  2. <details>
  3. <summary>英文:</summary>
  4. 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
  5. 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&#39; North Carolina shapefile that ships with the `{sf}` package.
  6. As a bonus I had fun with choosing the &quot;proper&quot; county names...
  7. library(sf)
  8. library(dplyr)
  9. library(tmap)
  10. shape &lt;- st_read(system.file(&quot;shape/nc.shp&quot;, package=&quot;sf&quot;)) # included with sf package
  11. shape &lt;- shape %&gt;%
  12. mutate(winner = case_when(NAME == &quot;Halifax&quot; ~ &quot;Conservative&quot;,
  13. NAME == &quot;Wilson&quot; ~ &quot;Labor&quot;,
  14. NAME == &quot;Scotland&quot; ~ &quot;SNP&quot;,
  15. NAME == &quot;Pitt&quot; ~ &quot;Libdem&quot;,
  16. T ~ NA)) %&gt;%
  17. filter(!is.na(winner))
  18. tm_shape(shape) +
  19. tm_polygons(col = &quot;winner&quot;,
  20. title = &quot;Results&quot;,
  21. # here is the action!!!
  22. palette = c(&quot;Conservative&quot; = &quot;blue&quot;,
  23. &quot;Labor&quot; = &quot;red&quot;,
  24. &quot;SNP&quot; = &quot;yellow&quot;,
  25. &quot;Libdem&quot; = &quot;orange&quot;))
  26. [![four counties in North Carolina, color coded][1]][1]
  27. [1]: https://i.stack.imgur.com/acWNw.png
  28. </details>
  29. # 答案2
  30. **得分**: 0
  31. 你需要创建一个自定义的颜色调色板:
  32. ```R
  33. library(tmap)
  34. library(dplyr)
  35. tmap_mode("view")
  36. data("World")
  37. states <- World %>%
  38. filter(name %in% c("Austria", "Germany", "Poland", "Hungary", "Czech Rep.", "Slovakia")) %>%
  39. mutate(name = factor(name, levels = c("Austria", "Germany", "Poland", "Hungary", "Czech Rep.", "Slovakia")))
  40. # 创建自定义颜色调色板
  41. custpal = c("darkred", "orange", "palegreen", "dodgerblue", "purple3", "gold")
  42. tm_shape(states) +
  43. tm_polygons("name", palette = custpal)

自定义Tmap调色板基于结果

英文:

You need to create a custom color palette:

  1. library(tmap)
  2. library(dplyr)
  3. tmap_mode(&quot;view&quot;)
  4. data(&quot;World&quot;)
  5. states &lt;- World %&gt;%
  6. filter(name %in% c(&quot;Austria&quot;, &quot;Germany&quot;, &quot;Poland&quot;, &quot;Hungary&quot;, &quot;Czech Rep.&quot;, &quot;Slovakia&quot;)) %&gt;%
  7. mutate(name = factor(name, levels = c(&quot;Austria&quot;, &quot;Germany&quot;, &quot;Poland&quot;, &quot;Hungary&quot;, &quot;Czech Rep.&quot;, &quot;Slovakia&quot;)))
  8. # create custom color palette
  9. custpal = c(&quot;darkred&quot;, &quot;orange&quot;, &quot;palegreen&quot;, &quot;dodgerblue&quot;, &quot;purple3&quot;, &quot;gold&quot;)
  10. tm_shape(states) +
  11. tm_polygons(&quot;name&quot;, palette = custpal)

自定义Tmap调色板基于结果

huangapple
  • 本文由 发表于 2023年2月14日 08:48:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定