9
有没有办法让地图上的点在地图边界内抖动?在下面的例子中,康涅狄格州西南部的抖动位置最终位于水中或相邻的状态,有没有办法让R抖动位置点但不在地图边界上?ggplot2如何在地图边界(例如美国州)内保持抖动位置?
另外,还有其他一些技巧,比如在每个城市附近创建一个表格以列出公司名称?
# create a data frame called "ct" of geolocations in two cities near the border of a US state (Connecticut). Each firm has the same lat and longitude of one of the two cities
> dput(ct)
structure(list(city = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), .Label = c("Greenwich", "Stamford"), class = "factor"),
firm = structure(c(1L, 12L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L), .Label = c("A1", "A10", "A11",
"A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A2",
"A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A3",
"A4", "A5", "A6", "A7", "A8", "A9"), class = "factor"), long = c(-73.63,
-73.63, -73.63, -73.63, -73.63, -73.55, -73.55, -73.55, -73.55,
-73.55, -73.55, -73.55, -73.55, -73.55, -73.55, -73.55, -73.55,
-73.55, -73.55, -73.55, -73.55, -73.55, -73.55, -73.55, -73.55,
-73.55, -73.55), lat = c(41.06, 41.06, 41.06, 41.06, 41.06,
41.09, 41.09, 41.09, 41.09, 41.09, 41.09, 41.09, 41.09, 41.09,
41.09, 41.09, 41.09, 41.09, 41.09, 41.09, 41.09, 41.09, 41.09,
41.09, 41.09, 41.09, 41.09)), .Names = c("city", "firm",
"long", "lat"), row.names = c(NA, -27L), class = "data.frame")
library(ggplot2)
# load the map of the United States
all_states <- map_data("state")
# choose to map the borders only of the state of Connecticut
st.map <- subset(all_states, region == "connecticut")
# plot the points for the firms with minimal jitter that still distinguishes each point
ggplot(ct, aes(long, lat)) +
geom_polygon(data=st.map, aes(x=long, y=lat, group = group), colour="grey70", fill="white") +
coord_map() +
geom_point(position=position_jitter(width=.1, height=.1), size=2)
更改每个经度或纬度一点点,因为在这个问题上,将无法工作,因为有太多的分,我希望的算法解决方案,因为我有很多情况下,这种拥挤和过境可能会出现。 https://stackoverflow.com/questions/22943110/jitter-coordinates
谢谢你的任何建议或答案。
哇,这是一个伟大的功能。如果这个函数存在于一个包中,那将会很棒! +1 – jazzurro
@shadow,那太酷了!如果我在很多州有这种情况,而且不知道哪一种会产生不适当的抖动?我的实际使用案例在数百个城市中有数千个位置,其中许多位于海洋,湖泊或州线附近。我如何让R检查过境并在需要时调用函数? – lawyeR