R - 在 tmap 中添加点到地图
R - Adding dots to map in tmap
我的地图制作代码根据人口普查数据生成地图,并将重要点绘制为 tm_dots() 图层。我想要做的是区分点的类型(例如,如果位置是 "Informal" 或 "Commercial")。
tm_shape(bristol) + tm_fill("population", palette = "YlOrRd",
auto.palette.mapping = TRUE,
title = "Bristol Population",
breaks = c(0,5,10,15,20,25), colorNA = "darkgrey") + tm_borders("grey25",alpha = 0.7, lwd = 0.1) +
tm_dots("n", size=0.1,col="green", shapeNA = NA, title = "Spaces") +
tm_legend(text.size=1,title.size=1.2,position=c("left","top")) +
tm_layout(legend.outside = TRUE, legend.outside.position = "bottom", title.snap.to.legend = TRUE)
我要找的基本上是:
tm_dots("n", size=0.1,col=Classification, shapeNA = NA, title = "Spaces")
添加多个 tm_dots() 层不是一种选择。我也无法重命名圆点图例,对此也有任何建议。
感谢您的帮助!
解决方案
为了将来参考,我通过 left_join
将 offices
添加到 bristol
,从而将 Classification
变量添加到 SpatialPolygonsDataFrame
。尽管有 showNA = NA
参数,但我在显示 NA 值时遇到了问题,但 colorNA = NULL
有效。最后一行:
tm_dots(size=0.1,col="Classification", palette = "Set1", colorNA = NULL)
我明白了,你需要另一个 tm_shape() 才能工作。仍然没有让 title() 正确显示,但一步一步。
tm_shape(bristol) + tm_fill("population", palette = "YlOrRd", auto.palette.mapping = TRUE,
title = "Bristol Population",
breaks = c(0,5,10,15,20,25), colorNA = "darkgrey") + tm_borders("grey25",alpha = 0.7, lwd = 0.1) +
tm_dots("Informal_Offices", size=0.1,col="green", shapeNA = NA, title = "Informal Offices") +
tm_shape(bristol) + tm_dots("Commercial_Offices", size=0.1,col="white",shapeNA=NA, title="Commercial Offices") +
tm_legend(text.size=1,title.size=1.2,position=c("left","top")) +
tm_layout(legend.outside = TRUE, legend.outside.position = "bottom", title.snap.to.legend = TRUE)
Result
所以bristol
是一个多边形形状(SpatialPolygonDataFrame
或sf
),你想在一些多边形中绘制点?
通常,您会有一个变量 Offices
,具有两个级别 "Informal"
和 "Commercial"
。那么就是tm_dots(size = 0.1, col = "Offices")
了。如果你想在一个多边形中放置两个点,因为有非正式办公室和商业办公室,那么你可以使用你自己的方法(并对一组使用 xmod
and/or ymod
以防止重叠) ,或创建一个包含所有办公室的 SpatialPointsDataFrame
或 sf
对象,以及一个包含上述两个级别的变量 Offices
。
我的地图制作代码根据人口普查数据生成地图,并将重要点绘制为 tm_dots() 图层。我想要做的是区分点的类型(例如,如果位置是 "Informal" 或 "Commercial")。
tm_shape(bristol) + tm_fill("population", palette = "YlOrRd",
auto.palette.mapping = TRUE,
title = "Bristol Population",
breaks = c(0,5,10,15,20,25), colorNA = "darkgrey") + tm_borders("grey25",alpha = 0.7, lwd = 0.1) +
tm_dots("n", size=0.1,col="green", shapeNA = NA, title = "Spaces") +
tm_legend(text.size=1,title.size=1.2,position=c("left","top")) +
tm_layout(legend.outside = TRUE, legend.outside.position = "bottom", title.snap.to.legend = TRUE)
我要找的基本上是:
tm_dots("n", size=0.1,col=Classification, shapeNA = NA, title = "Spaces")
添加多个 tm_dots() 层不是一种选择。我也无法重命名圆点图例,对此也有任何建议。
感谢您的帮助!
解决方案
为了将来参考,我通过 left_join
将 offices
添加到 bristol
,从而将 Classification
变量添加到 SpatialPolygonsDataFrame
。尽管有 showNA = NA
参数,但我在显示 NA 值时遇到了问题,但 colorNA = NULL
有效。最后一行:
tm_dots(size=0.1,col="Classification", palette = "Set1", colorNA = NULL)
我明白了,你需要另一个 tm_shape() 才能工作。仍然没有让 title() 正确显示,但一步一步。
tm_shape(bristol) + tm_fill("population", palette = "YlOrRd", auto.palette.mapping = TRUE,
title = "Bristol Population",
breaks = c(0,5,10,15,20,25), colorNA = "darkgrey") + tm_borders("grey25",alpha = 0.7, lwd = 0.1) +
tm_dots("Informal_Offices", size=0.1,col="green", shapeNA = NA, title = "Informal Offices") +
tm_shape(bristol) + tm_dots("Commercial_Offices", size=0.1,col="white",shapeNA=NA, title="Commercial Offices") +
tm_legend(text.size=1,title.size=1.2,position=c("left","top")) +
tm_layout(legend.outside = TRUE, legend.outside.position = "bottom", title.snap.to.legend = TRUE)
Result
所以bristol
是一个多边形形状(SpatialPolygonDataFrame
或sf
),你想在一些多边形中绘制点?
通常,您会有一个变量 Offices
,具有两个级别 "Informal"
和 "Commercial"
。那么就是tm_dots(size = 0.1, col = "Offices")
了。如果你想在一个多边形中放置两个点,因为有非正式办公室和商业办公室,那么你可以使用你自己的方法(并对一组使用 xmod
and/or ymod
以防止重叠) ,或创建一个包含所有办公室的 SpatialPointsDataFrame
或 sf
对象,以及一个包含上述两个级别的变量 Offices
。