如何在R中给标题加上框架?

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

How to make a frame around titles in R?

问题

我正在使用tmap包制作图表。
我想知道是否可以在每个图的标题周围绘制一个边框。
我查看了tm_layout函数,但没有找到相关内容。

以下是我的代码。运行此代码时,您将获得嵌入的图像。

# 绘制每个地图
tm1 <- tm_shape(boros) +
  tm_fill(col = 'grey94') +
  tm_borders() +
  tm_shape(lci_long_sf %>% filter(Indicator == 'Diversity of Commerce')) +
  tm_polygons(col = 'prop_decile', 
              palette = "viridis",
              style = 'cat',
              title = 'Decile') +
  tm_legend(show = F) +
  tm_compass(type = "arrow", position = c('left', 'bottom'), size = 1) +
  tm_scale_bar(text.size = 0.45, position = c("left", "bottom")) +
  tm_layout(frame = F,
            main.title = 'Diversity of Commerce',
            main.title.position = c('center', 'top'),
            main.title.size = 1)

tm2 <- tm_shape(boros) +
  tm_fill(col = 'grey94') +
  tm_borders() +
  tm_shape(lci_long_sf %>% filter(Indicator == 'Diversity of Education')) +
  tm_polygons(col = 'prop_decile', 
              palette = "viridis",
              style = 'cat',
              title = 'Decile') +
  tm_legend(show = F) +
  tm_compass(type = "arrow", position = c('left', 'bottom'), size = 1) +
  tm_scale_bar(text.size = 0.45, position = c("left", "bottom")) +
  tm_layout(frame = F,
            main.title = 'Diversity of Education',
            main.title.position = c('center', 'top'),
            main.title.size = 1)

tm3 <- tm_shape(boros) +
  tm_fill(col = 'grey94') +
  tm_borders() +
  tm_shape(lci_long_sf %>% filter(Indicator == 'Diversity of Entertainment')) +
  tm_polygons(col = 'prop_decile', 
              palette = "viridis",
              style = 'cat',
              title = 'Decile') +
  tm_legend(show = F) +
  tm_compass(type = "arrow", position = c('left', 'bottom'), size = 1) +
  tm_scale_bar(text.size = 0.45, position = c("left", "bottom")) +
  tm_layout(frame = F,
            main.title = 'Diversity of Entertainment',
            main.title.position = c('center', 'top'),
            main.title.size = 1)

tm4 <- tm_shape(boros) +
  tm_fill(col = 'grey94') +
  tm_borders() +
  tm_shape(lci_long_sf %>% filter(Indicator == 'Diversity of Living')) +
  tm_polygons(col = 'prop_decile', 
              palette = "viridis",
              style = 'cat',
              title = 'Decile') +
  tm_legend(show = F) +
  tm_compass(type = "arrow", position = c('left', 'bottom'), size = 1) +
  tm_scale_bar(text.size = 0.45, position = c("left", "bottom")) +
  tm_layout(frame = F,
            main.title = 'Diversity of Living',
            main.title.position = c('center', 'top'),
            main.title.size = 1)

tm5 <- tm_shape(boros) +
  tm_fill(col = 'grey94') +
  tm_borders() +
  tm_shape(lci_long_sf %>% filter(Indicator == 'Diversity of Healthcare')) +
  tm_polygons(col = 'prop_decile', 
              palette = "viridis",
              style = 'cat',
              title = 'Decile') +
  tm_legend(show = F) +
  tm_compass(type = "arrow", position = c('left', 'bottom'), size = 1) +
  tm_scale_bar(text.size = 0.45, position = c("left", "bottom")) +
  tm_layout(frame = F,
            main.title = 'Diversity of Healthcare',
            main.title.position = c('center', 'top'),
            main.title.size = 1)

# 图例
legend <- tm_shape(lci_long_sf) +
  tm_polygons('prop_decile',
              palette = 'viridis',
              title = 'Proportion',
              style = 'cat') +
  tm_layout(legend.only = T,
            legend.position = c('center', 'center'), 
            legend.height = 3.5,
            legend.width = 4,
            asp = 0.1)

t = tmap_arrange(tm1, tm2, tm3, tm4, tm5, legend, ncol=2)

t

这是我所说的绘制边框的意思。
如何在R中给标题加上框架?

有人可以帮忙吗?
谢谢。

英文:

I am making plots using the tmap package.
I was wondering if I could draw a frame around each plot's title.
I looked into tm_layout function and did not manage to find anything relevant.

The below is my code. When you run this code, you will get the image embedded.

# plot each map
tm1 &lt;- tm_shape(boros) +
tm_fill(col = &#39;grey94&#39;) +
tm_borders() +
tm_shape(lci_long_sf %&gt;% filter(Indicator == &#39;Diversity of Commerce&#39;)) +
tm_polygons(col = &#39;prop_decile&#39;, 
palette = &quot;viridis&quot;,
style = &#39;cat&#39;,
title = &#39;Decile&#39;) +
tm_legend(show = F) +
tm_compass(type = &quot;arrow&quot;, position = c(&#39;left&#39;, &#39;bottom&#39;), size = 1) +
tm_scale_bar(text.size = 0.45, position = c(&quot;left&quot;, &quot;bottom&quot;)) +
tm_layout(frame = F,
main.title = &#39;Diversity of Commerce&#39;,
main.title.position = c(&#39;center&#39;, &#39;top&#39;),
main.title.size = 1)
tm2 &lt;- tm_shape(boros) +
tm_fill(col = &#39;grey94&#39;) +
tm_borders() +
tm_shape(lci_long_sf %&gt;% filter(Indicator == &#39;Diversity of Education&#39;)) +
tm_polygons(col = &#39;prop_decile&#39;, 
palette = &quot;viridis&quot;,
style = &#39;cat&#39;,
title = &#39;Decile&#39;) +
tm_legend(show = F) +
tm_compass(type = &quot;arrow&quot;, position = c(&#39;left&#39;, &#39;bottom&#39;), size = 1) +
tm_scale_bar(text.size = 0.45, position = c(&quot;left&quot;, &quot;bottom&quot;)) +
tm_layout(frame = F,
main.title = &#39;Diversity of Education&#39;,
main.title.position = c(&#39;center&#39;, &#39;top&#39;),
main.title.size = 1)
tm3 &lt;- tm_shape(boros) +
tm_fill(col = &#39;grey94&#39;) +
tm_borders() +
tm_shape(lci_long_sf %&gt;% filter(Indicator == &#39;Diversity of Entertainment&#39;)) +
tm_polygons(col = &#39;prop_decile&#39;, 
palette = &quot;viridis&quot;,
style = &#39;cat&#39;,
title = &#39;Decile&#39;) +
tm_legend(show = F) +
tm_compass(type = &quot;arrow&quot;, position = c(&#39;left&#39;, &#39;bottom&#39;), size = 1) +
tm_scale_bar(text.size = 0.45, position = c(&quot;left&quot;, &quot;bottom&quot;)) +
tm_layout(frame = F,
main.title = &#39;Diversity of Entertainment&#39;,
main.title.position = c(&#39;center&#39;, &#39;top&#39;),
main.title.size = 1)  
tm4 &lt;- tm_shape(boros) +
tm_fill(col = &#39;grey94&#39;) +
tm_borders() +
tm_shape(lci_long_sf %&gt;% filter(Indicator == &#39;Diversity of Living&#39;)) +
tm_polygons(col = &#39;prop_decile&#39;, 
palette = &quot;viridis&quot;,
style = &#39;cat&#39;,
title = &#39;Decile&#39;) +
tm_legend(show = F) +
tm_compass(type = &quot;arrow&quot;, position = c(&#39;left&#39;, &#39;bottom&#39;), size = 1) +
tm_scale_bar(text.size = 0.45, position = c(&quot;left&quot;, &quot;bottom&quot;)) +
tm_layout(frame = F,
main.title = &#39;Diversity of Living&#39;,
main.title.position = c(&#39;center&#39;, &#39;top&#39;),
main.title.size = 1)
tm5 &lt;- tm_shape(boros) +
tm_fill(col = &#39;grey94&#39;) +
tm_borders() +
tm_shape(lci_long_sf %&gt;% filter(Indicator == &#39;Diversity of Healthcare&#39;)) +
tm_polygons(col = &#39;prop_decile&#39;, 
palette = &quot;viridis&quot;,
style = &#39;cat&#39;,
title = &#39;Decile&#39;) +
tm_legend(show = F) +
tm_compass(type = &quot;arrow&quot;, position = c(&#39;left&#39;, &#39;bottom&#39;), size = 1) +
tm_scale_bar(text.size = 0.45, position = c(&quot;left&quot;, &quot;bottom&quot;)) +
tm_layout(frame = F,
main.title = &#39;Diversity of Healthcare&#39;,
main.title.position = c(&#39;center&#39;, &#39;top&#39;),
main.title.size = 1)  
# legend
legend &lt;- tm_shape(lci_long_sf) +
tm_polygons(&#39;prop_decile&#39;,
palette = &#39;viridis&#39;,
title = &#39;Proportion&#39;,
style = &#39;cat&#39;) +
tm_layout(legend.only = T,
legend.position = c(&#39;center&#39;, &#39;center&#39;), 
legend.height = 3.5,
legend.width = 4,
asp = 0.1)
t = tmap_arrange(tm1, tm2, tm3, tm4, tm5, legend, ncol=2)
t

如何在R中给标题加上框架?

And this is what I mean by drawing a frame.
如何在R中给标题加上框架?

Could anyone help?
Thank you.

答案1

得分: 1

使用main.title的地方改成使用panel.show = TRUE并设置合适的panel.labels,同时确保frame = TRUE。例如,你的第一个图的tm_layout应该是:

  tm_layout(frame = TRUE,
            panel.show = TRUE,
            panel.labels = 'Diversity of Healthcare')

如果将所有的tm_layout都改成这个格式,你会得到:

如何在R中给标题加上框架?

英文:

Instead of using main.title, use panel.show = TRUE with appropriatepanel.labels. Also make sure frame = TRUE. For example, your first plot's tm_layout would be:

  tm_layout(frame = TRUE,
            panel.show = TRUE,
            panel.labels = &#39;Diversity of Healthcare&#39;)

If you change all the tm_layout to this format, you get:

如何在R中给标题加上框架?

huangapple
  • 本文由 发表于 2023年8月5日 02:43:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838466.html
匿名

发表评论

匿名网友

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

确定