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

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

How to make a frame around titles in R?

问题

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

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

  1. # 绘制每个地图
  2. tm1 <- tm_shape(boros) +
  3. tm_fill(col = 'grey94') +
  4. tm_borders() +
  5. tm_shape(lci_long_sf %>% filter(Indicator == 'Diversity of Commerce')) +
  6. tm_polygons(col = 'prop_decile',
  7. palette = "viridis",
  8. style = 'cat',
  9. title = 'Decile') +
  10. tm_legend(show = F) +
  11. tm_compass(type = "arrow", position = c('left', 'bottom'), size = 1) +
  12. tm_scale_bar(text.size = 0.45, position = c("left", "bottom")) +
  13. tm_layout(frame = F,
  14. main.title = 'Diversity of Commerce',
  15. main.title.position = c('center', 'top'),
  16. main.title.size = 1)
  17. tm2 <- tm_shape(boros) +
  18. tm_fill(col = 'grey94') +
  19. tm_borders() +
  20. tm_shape(lci_long_sf %>% filter(Indicator == 'Diversity of Education')) +
  21. tm_polygons(col = 'prop_decile',
  22. palette = "viridis",
  23. style = 'cat',
  24. title = 'Decile') +
  25. tm_legend(show = F) +
  26. tm_compass(type = "arrow", position = c('left', 'bottom'), size = 1) +
  27. tm_scale_bar(text.size = 0.45, position = c("left", "bottom")) +
  28. tm_layout(frame = F,
  29. main.title = 'Diversity of Education',
  30. main.title.position = c('center', 'top'),
  31. main.title.size = 1)
  32. tm3 <- tm_shape(boros) +
  33. tm_fill(col = 'grey94') +
  34. tm_borders() +
  35. tm_shape(lci_long_sf %>% filter(Indicator == 'Diversity of Entertainment')) +
  36. tm_polygons(col = 'prop_decile',
  37. palette = "viridis",
  38. style = 'cat',
  39. title = 'Decile') +
  40. tm_legend(show = F) +
  41. tm_compass(type = "arrow", position = c('left', 'bottom'), size = 1) +
  42. tm_scale_bar(text.size = 0.45, position = c("left", "bottom")) +
  43. tm_layout(frame = F,
  44. main.title = 'Diversity of Entertainment',
  45. main.title.position = c('center', 'top'),
  46. main.title.size = 1)
  47. tm4 <- tm_shape(boros) +
  48. tm_fill(col = 'grey94') +
  49. tm_borders() +
  50. tm_shape(lci_long_sf %>% filter(Indicator == 'Diversity of Living')) +
  51. tm_polygons(col = 'prop_decile',
  52. palette = "viridis",
  53. style = 'cat',
  54. title = 'Decile') +
  55. tm_legend(show = F) +
  56. tm_compass(type = "arrow", position = c('left', 'bottom'), size = 1) +
  57. tm_scale_bar(text.size = 0.45, position = c("left", "bottom")) +
  58. tm_layout(frame = F,
  59. main.title = 'Diversity of Living',
  60. main.title.position = c('center', 'top'),
  61. main.title.size = 1)
  62. tm5 <- tm_shape(boros) +
  63. tm_fill(col = 'grey94') +
  64. tm_borders() +
  65. tm_shape(lci_long_sf %>% filter(Indicator == 'Diversity of Healthcare')) +
  66. tm_polygons(col = 'prop_decile',
  67. palette = "viridis",
  68. style = 'cat',
  69. title = 'Decile') +
  70. tm_legend(show = F) +
  71. tm_compass(type = "arrow", position = c('left', 'bottom'), size = 1) +
  72. tm_scale_bar(text.size = 0.45, position = c("left", "bottom")) +
  73. tm_layout(frame = F,
  74. main.title = 'Diversity of Healthcare',
  75. main.title.position = c('center', 'top'),
  76. main.title.size = 1)
  77. # 图例
  78. legend <- tm_shape(lci_long_sf) +
  79. tm_polygons('prop_decile',
  80. palette = 'viridis',
  81. title = 'Proportion',
  82. style = 'cat') +
  83. tm_layout(legend.only = T,
  84. legend.position = c('center', 'center'),
  85. legend.height = 3.5,
  86. legend.width = 4,
  87. asp = 0.1)
  88. t = tmap_arrange(tm1, tm2, tm3, tm4, tm5, legend, ncol=2)
  89. 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.

  1. # plot each map
  2. tm1 &lt;- tm_shape(boros) +
  3. tm_fill(col = &#39;grey94&#39;) +
  4. tm_borders() +
  5. tm_shape(lci_long_sf %&gt;% filter(Indicator == &#39;Diversity of Commerce&#39;)) +
  6. tm_polygons(col = &#39;prop_decile&#39;,
  7. palette = &quot;viridis&quot;,
  8. style = &#39;cat&#39;,
  9. title = &#39;Decile&#39;) +
  10. tm_legend(show = F) +
  11. tm_compass(type = &quot;arrow&quot;, position = c(&#39;left&#39;, &#39;bottom&#39;), size = 1) +
  12. tm_scale_bar(text.size = 0.45, position = c(&quot;left&quot;, &quot;bottom&quot;)) +
  13. tm_layout(frame = F,
  14. main.title = &#39;Diversity of Commerce&#39;,
  15. main.title.position = c(&#39;center&#39;, &#39;top&#39;),
  16. main.title.size = 1)
  17. tm2 &lt;- tm_shape(boros) +
  18. tm_fill(col = &#39;grey94&#39;) +
  19. tm_borders() +
  20. tm_shape(lci_long_sf %&gt;% filter(Indicator == &#39;Diversity of Education&#39;)) +
  21. tm_polygons(col = &#39;prop_decile&#39;,
  22. palette = &quot;viridis&quot;,
  23. style = &#39;cat&#39;,
  24. title = &#39;Decile&#39;) +
  25. tm_legend(show = F) +
  26. tm_compass(type = &quot;arrow&quot;, position = c(&#39;left&#39;, &#39;bottom&#39;), size = 1) +
  27. tm_scale_bar(text.size = 0.45, position = c(&quot;left&quot;, &quot;bottom&quot;)) +
  28. tm_layout(frame = F,
  29. main.title = &#39;Diversity of Education&#39;,
  30. main.title.position = c(&#39;center&#39;, &#39;top&#39;),
  31. main.title.size = 1)
  32. tm3 &lt;- tm_shape(boros) +
  33. tm_fill(col = &#39;grey94&#39;) +
  34. tm_borders() +
  35. tm_shape(lci_long_sf %&gt;% filter(Indicator == &#39;Diversity of Entertainment&#39;)) +
  36. tm_polygons(col = &#39;prop_decile&#39;,
  37. palette = &quot;viridis&quot;,
  38. style = &#39;cat&#39;,
  39. title = &#39;Decile&#39;) +
  40. tm_legend(show = F) +
  41. tm_compass(type = &quot;arrow&quot;, position = c(&#39;left&#39;, &#39;bottom&#39;), size = 1) +
  42. tm_scale_bar(text.size = 0.45, position = c(&quot;left&quot;, &quot;bottom&quot;)) +
  43. tm_layout(frame = F,
  44. main.title = &#39;Diversity of Entertainment&#39;,
  45. main.title.position = c(&#39;center&#39;, &#39;top&#39;),
  46. main.title.size = 1)
  47. tm4 &lt;- tm_shape(boros) +
  48. tm_fill(col = &#39;grey94&#39;) +
  49. tm_borders() +
  50. tm_shape(lci_long_sf %&gt;% filter(Indicator == &#39;Diversity of Living&#39;)) +
  51. tm_polygons(col = &#39;prop_decile&#39;,
  52. palette = &quot;viridis&quot;,
  53. style = &#39;cat&#39;,
  54. title = &#39;Decile&#39;) +
  55. tm_legend(show = F) +
  56. tm_compass(type = &quot;arrow&quot;, position = c(&#39;left&#39;, &#39;bottom&#39;), size = 1) +
  57. tm_scale_bar(text.size = 0.45, position = c(&quot;left&quot;, &quot;bottom&quot;)) +
  58. tm_layout(frame = F,
  59. main.title = &#39;Diversity of Living&#39;,
  60. main.title.position = c(&#39;center&#39;, &#39;top&#39;),
  61. main.title.size = 1)
  62. tm5 &lt;- tm_shape(boros) +
  63. tm_fill(col = &#39;grey94&#39;) +
  64. tm_borders() +
  65. tm_shape(lci_long_sf %&gt;% filter(Indicator == &#39;Diversity of Healthcare&#39;)) +
  66. tm_polygons(col = &#39;prop_decile&#39;,
  67. palette = &quot;viridis&quot;,
  68. style = &#39;cat&#39;,
  69. title = &#39;Decile&#39;) +
  70. tm_legend(show = F) +
  71. tm_compass(type = &quot;arrow&quot;, position = c(&#39;left&#39;, &#39;bottom&#39;), size = 1) +
  72. tm_scale_bar(text.size = 0.45, position = c(&quot;left&quot;, &quot;bottom&quot;)) +
  73. tm_layout(frame = F,
  74. main.title = &#39;Diversity of Healthcare&#39;,
  75. main.title.position = c(&#39;center&#39;, &#39;top&#39;),
  76. main.title.size = 1)
  77. # legend
  78. legend &lt;- tm_shape(lci_long_sf) +
  79. tm_polygons(&#39;prop_decile&#39;,
  80. palette = &#39;viridis&#39;,
  81. title = &#39;Proportion&#39;,
  82. style = &#39;cat&#39;) +
  83. tm_layout(legend.only = T,
  84. legend.position = c(&#39;center&#39;, &#39;center&#39;),
  85. legend.height = 3.5,
  86. legend.width = 4,
  87. asp = 0.1)
  88. t = tmap_arrange(tm1, tm2, tm3, tm4, tm5, legend, ncol=2)
  89. 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应该是:

  1. tm_layout(frame = TRUE,
  2. panel.show = TRUE,
  3. 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:

  1. tm_layout(frame = TRUE,
  2. panel.show = TRUE,
  3. 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:

确定