R-在tmap中为地图添加点 [英] R - Adding dots to map in tmap
问题描述
我的地图制作代码根据普查数据生成地图,并将重要点绘制为tm_dots()层.我想做的是区分点的类型(例如,位置是非正式"还是商业").
My map-making code generates a map based on census data and plots important points as a tm_dots() layer. What I'd like to be able to do is differentiate between the types of dots (e.g. if the location is "Informal" or "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()层不是一种选择.我也无法重命名点图例,对此也有任何建议.
Adding several tm_dots() layers isn't an option. I also can't rename the dot legend, any advice on that too is appreciated.
感谢您的帮助!
解决方案
为了将来参考,我通过left_join
将offices
添加到了bristol
,因此将Classification
变量添加到了SpatialPolygonsDataFrame
.尽管有showNA = NA
参数,但我仍然无法显示NA值,但colorNA = NULL
仍然有效.最后一行:
tm_dots(size=0.1,col="Classification", palette = "Set1", colorNA = NULL)
For future reference, I added offices
to bristol
via left_join
, thus adding the Classification
variable to the SpatialPolygonsDataFrame
. I was having issues with it displaying NA values despite the showNA = NA
parameter, but colorNA = NULL
worked. Final line:
tm_dots(size=0.1,col="Classification", palette = "Set1", colorNA = NULL)
推荐答案
所以bristol
是多边形(SpatialPolygonDataFrame
或sf
),您想在某些多边形中绘制点吗?
So bristol
is a polygon shape (SpatialPolygonDataFrame
or sf
), and you want to plot dots in some polygons?
通常,您将具有两个级别"Informal"
和"Commercial"
的变量Offices
.然后就是tm_dots(size = 0.1, col = "Offices")
.如果由于有非正式办公室和商业办公室而要在一个多边形中放置两个点,则可以使用自己的方法(并且对一组使用xmod
和/或ymod
以防止重叠),或创建sf
对象以及所有办公室,以及一个变量Offices
,具有如上所述的两个级别.
Normally, you would have a variable Offices
, with two levels "Informal"
and "Commercial"
. Then it's just tm_dots(size = 0.1, col = "Offices")
. If you want to place two dots in one polygons because there are Informal and Commercial offices, then you can use your own approach (and use xmod
and/or ymod
for one group to prevent overlap), or create a SpatialPointsDataFrame
or sf
object with all offices, and a variable Offices
with two levels as described above.
这篇关于R-在tmap中为地图添加点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!