2017-03-04 43 views
1

我有一个分成组的一些地图数据,想画一个facet_wrap比例的地图,以适应点FACET_WRAP

这是代码,我到目前为止有:

library(dplyr) 
library(ggplot2) 

test_points =data.frame(map = c(rep("AAA", 5),rep("BBB", 5),rep("CCC", 5),rep("DDD", 5)), 
          LAT = c(runif(5, -90, -45), runif(5, -45, 0), runif(5, 0, 45), runif(5, 45, 90)), 
         LON = c(runif(5, -180, -90), runif(5, -90,0), runif(5, 0, 90), runif(5, 90, 180))) 

map_lim = test_points %>% 
      group_by(map) %>% 
      summarise(y_min = floor(min(LAT)), 
         y_max = ceiling(max(LAT)), 
         x_min = floor(min(LON)), 
         x_max = ceiling(max(LON))) 

worldmap = map_data("world") 

ggplot() + 
    geom_polygon(data = worldmap, 
       aes(x = long, y = lat, group = group)) + 
    geom_point(data = test_points, 
       aes(x = LON, y = LAT, group= map, colour = map)) + 
    #coord_cartesian(
    #  ylim=c(map_lim[ map_lim$map==map, 2], map_lim[ map_lim$map==map, 3]), 
    #  xlim=c(map_lim[ map_lim$map==map, 4], map_lim[ map_lim$map==map, 5]) 
    #) + 
    coord_cartesian(
      ylim=c(map_lim$y_min, map_lim$y_max), 
      xlim=c(map_lim$x_min, map_lim$x_max) 
    ) + 
    facet_wrap(~ map, scales = "free") 

你可以看到我已经尝试了限制坐标的两种方法..既不工作。

我得到的是整个世界......每个都有五点。

是否可以独立缩放每个地图?

+0

尝试使用geom_map而不是geom_polygon –

+0

我试过这个,它似乎没有工作。我也尝试将4个极限参数加入到test_points DF中。这也没有用。 我想我将不得不创建4个单独的地图,然后将它们放在一起在同一页上。 – SteveG

+0

出了什么问题? –

回答

0

geom_map是优于geom_polygon,因为它会自动截断由点等所需要的区域

worldmap <- map_data("world") 

ggplot() + 
    geom_map(data = worldmap, map = worldmap, mapping = aes(map_id = region)) + 
    geom_point(data = test_points, 
      aes(x = LON, y = LAT, group= map, colour = map)) + 
    coord_equal() + 
    facet_wrap(~ map, scales = "free") 

唯一的问题是,scales = "free"不与非笛卡尔COORDS玩,所以你不能使用coord_map或特定的制图投影。如果你的纬度相似,那么coord_fixed可能会更好。

+0

我不知道我在做什么错...可能在mapping = aes(map_id = region) 这给了我想要的东西! – SteveG