英文:
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
有人可以帮忙吗?
谢谢。
英文:
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 <- 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
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
And this is what I mean by drawing a frame.
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
都改成这个格式,你会得到:
英文:
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 = 'Diversity of Healthcare')
If you change all the tm_layout
to this format, you get:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论