自定义Tmap调色板基于结果

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

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?

自定义Tmap调色板基于结果

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

自定义Tmap调色板基于结果


[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&#39; North Carolina shapefile that ships with the `{sf}` package. 

As a bonus I had fun with choosing the &quot;proper&quot; county names...

    library(sf)
    library(dplyr)
    library(tmap)
    
    
    shape &lt;- st_read(system.file(&quot;shape/nc.shp&quot;, package=&quot;sf&quot;)) # included with sf package
    
    shape &lt;- shape %&gt;% 
      mutate(winner = case_when(NAME == &quot;Halifax&quot; ~ &quot;Conservative&quot;,
                                NAME == &quot;Wilson&quot; ~ &quot;Labor&quot;,
                                NAME == &quot;Scotland&quot; ~ &quot;SNP&quot;,
                                NAME == &quot;Pitt&quot; ~ &quot;Libdem&quot;,
                                T ~ NA)) %&gt;% 
      filter(!is.na(winner))
    
    
    
    tm_shape(shape) +
      tm_polygons(col = &quot;winner&quot;,
                  title = &quot;Results&quot;,
                  # here is the action!!! 
                  palette = c(&quot;Conservative&quot; = &quot;blue&quot;, 
                              &quot;Labor&quot; = &quot;red&quot;,
                              &quot;SNP&quot; = &quot;yellow&quot;,
                              &quot;Libdem&quot; = &quot;orange&quot;))

[![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)

自定义Tmap调色板基于结果

英文:

You need to create a custom color palette:

library(tmap)
library(dplyr)

tmap_mode(&quot;view&quot;)
data(&quot;World&quot;)

states &lt;- World %&gt;% 
  filter(name %in% c(&quot;Austria&quot;, &quot;Germany&quot;, &quot;Poland&quot;, &quot;Hungary&quot;, &quot;Czech Rep.&quot;, &quot;Slovakia&quot;)) %&gt;% 
  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;)))

# create custom color palette
custpal = c(&quot;darkred&quot;, &quot;orange&quot;, &quot;palegreen&quot;, &quot;dodgerblue&quot;, &quot;purple3&quot;, &quot;gold&quot;)

tm_shape(states) +
    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:

确定