geom_rect + coord_map =非常慢

时间:2016-06-05 10:04:47

标签: r ggplot2

我正在使用def load_level(level): walls = [] players = [] finishes = [] x = y = 0 for row in levels[level]: for col in row: if col == "W": walls.append(Wall((x, y))) if col == "P": players.append(Player((x, y))) if col == "F": finishes.append(Finish1((x, y))) if col == "G": finishes.append(Finish2((x, y))) if col == "H": finishes.append(Finish3((x, y))) if col == "I": finishes.append(Finish4((x, y))) x += 40.96 y += 30.72 x = 0 return walls, players, finishes walls, players, finishes = load_level(currentLevel) def Menu(): runnin = True while runnin: clock.tick(60) screen.fill(BLACK) mouseclick = pygame.mouse.get_pressed() for e in pygame.event.get(): if e.type == pygame.QUIT: pygame.quit() sys.exit(0) if e.type == pygame.KEYDOWN: if e.key == pygame.K_ESCAPE: pygame.quit() sys.exit(0) for option in options: if option.rect.collidepoint(pygame.mouse.get_pos()): option.hovered = True if mouseclick[0] == 1: if option.text == "Easy": global currentlevel, walls, players, finishes walls, players, finishes = load_level(0) currentlevel = 0 main() elif option.text == "Medium": global currentlevel, walls, players, finishes walls, players, finishes = load_level(1) currentlevel = 1 main() elif option.text == "Hard": global currentlevel, walls, players, finishes walls, players, finishes = load_level(2) currentlevel = 2 main() elif option.text == "Help": Help() else: runnin = False else: option.hovered = False option.draw() screen.blit(title_font.render("Amazeing Race", True, GREY), (130, 50)) pygame.display.update() pygame.quit() sys.exit(0) 绘制一张挪威地图,其中一个感兴趣的区域用红色矩形突出显示。如果我省略ggplot2geom_rect,则地图绘制得非常快(<1秒)。如果我同时使用 - 我需要 - 它的打印和渲染速度极慢(大约五分钟)。

我认为这与咀嚼有关 - 将矩形投影到新的坐标系上。有没有办法控制这个?

coord_map

使用低分辨率地图可以使地图绘制更快(约10秒)。

2 个答案:

答案 0 :(得分:6)

无需诉诸墨卡托近似值:

library(ggplot2)
library(maps)
library(mapdata)

norwaymap <- map_data("worldHires", "Norway")

xlim <- c(5, 10)
ylim <- c(60, 62)

ggplot() +
  geom_map(data=norwaymap, map=norwaymap,
           aes(long, lat, map_id=region),
           color=NA, fill="grey60") +
  geom_rect(data=data.frame(),
            aes(xmin=xlim[1], xmax=xlim[2], ymin=ylim[1], ymax=ylim[2]),
            color="red", fill=NA) +
  coord_map(xlim=c(3, 33), ylim=c(57, 72)) +
  ggthemes::theme_map()

enter image description here

另一个选择是使用Albers等面积圆锥投影(该区域的典型投影):

ggplot() +
  geom_map(data=norwaymap, map=norwaymap,
           aes(long, lat, map_id=region),
           color=NA, fill="grey60") +
  geom_rect(data=data.frame(),
            aes(xmin=xlim[1], xmax=xlim[2], ymin=ylim[1], ymax=ylim[2]),
            color="red", fill=NA) +
  ggalt::coord_proj("+proj=aea +lat_1=60 +lat_2=70 +lon_0=18.37", 
                    xlim=c(3, 33), ylim=c(57, 72)) +
  ggthemes::theme_map()

enter image description here

这有一个&#34;缺点&#34;正在投影的矩形(也是墨卡托,那里没有失真)。

无论哪种方式,矩形的神奇之处在于确保你只绘制一个,就像卢克所说的那样。

答案 1 :(得分:4)

使用coord_quickmap,尤其是annotate代替geom_rect来加快速度:

ggplot(norwaymap, aes(x = long, y = lat, group = group)) +
  geom_polygon(colour = NA, fill = "grey60") +
  annotate(geom="rect", xmin = xlim[1], xmax = xlim[2], ymin = ylim[1], 
           ymax = ylim[2], colour = "red", fill = NA) + 
  coord_quickmap(xlim = c(3, 33), ylim = c(57, 72))

geom_rect在同一个地方过度绘制了几个矩形,annotate只绘制了一个矩形。您可以在帮助文件中了解coord_mapcoord_quickmap之间的区别:?coord_quickmap

相关问题