8

我试图弄清楚如何在 gglot2 中显示我的完整地图,包括岛屿 r_base 和 tmap 都能够显示岛屿,但 ggplot2 无法将岛屿与其他水体区分开来 在此处输入图像描述......

我的问题是如何让岛屿出现在 ggplot2 中?

请参阅下面我使用的代码。

library(ggplot2)
library (rgdal)
library (rgeos)
library(maptools)
library(tmap)

加载称为 iho 的波斯湾形状填充

PG <- readShapePoly("iho.shp")

形状文件可在此处获得

http://geo.vliz.be:80/geoserver/wfs?request=getfeature&service=wfs&version=1.0.0&typename=MarineRegions:iho&outputformat=SHAPE-ZIP&filter=%3CPropertyIsEqualTo%3E%3CPropertyName%3Eid%3C%2FPropertyName%3E%3CLiteral %3E41%3C%2FLiteral%3E%3C%2FPropertyIsEqualTo%3E

用 r_base 绘图

Q<-plot(PG)

对应图A

用 tmap 绘图

qtm(PG)

对应图B

转换为数据框

AG <- fortify(PG)

用 ggplot2 绘图

ggplot()+ geom_polygon(data=AG, aes(long, lat, group = group), colour = alpha("darkred", 1/2), size = 0.7, fill = 'skyblue', alpha = .3)

对应图 C

4

2 回答 2

9

您需要告诉 ggplot 您希望用不同的颜色填充孔。例如:

ggplot()+ geom_polygon(data=AG, aes(long, lat, group = group, fill = hole), colour = alpha("darkred", 1/2), size = 0.7) + scale_fill_manual(values = c("skyblue", "white")) + theme(legend.position="none")

还可以尝试readOGR()使用 rgdal 包中的函数,而不是readShapePoly()在您读取形状文件时保留所有投影和基准信息。

于 2016-02-01T19:12:20.837 回答
8

除了@AdamMccurdy 的回答:有一些可能性可以为岛屿和相邻背景获得相同的颜色。

第一个设置岛屿的填充颜色和背景颜色相同。但是网格线在多边形下方,因此消失了。

第二个是尝试恢复网格线。它在面板顶部绘制背景(包括网格线)(使用panel.ontop = TRUE)。但调整 alpha 值以获得相同的背景和岛屿颜色有点麻烦。

第三个将背景和岛的颜色设置为相同(与第一个相同),然后在面板顶部绘制网格线。有几种方法可以做到这一点;在这里,我从原始图中抓取网格线,然后将它们绘制在面板顶部。因此颜色保持不变,并且不需要 alpha 透明度。

library(ggplot2)
library (rgdal)
library (rgeos)
library(maptools)

PG <- readOGR("iho.shp", layer = "iho")    
AG <- fortify(PG)

方法一

bg = "grey92"
ggplot() + 
   geom_polygon(data = AG, aes(long, lat, group = group, fill = hole), 
      colour = alpha("darkred", 1/2), size = 0.7) + 
   scale_fill_manual(values = c("skyblue", bg)) + 
   theme(panel.background = element_rect(fill = bg),
         legend.position = "none")

在此处输入图像描述

方法二

ggplot() + 
   geom_polygon(data = AG, aes(long, lat, group = group, fill = hole), 
      colour = alpha("darkred", 1/2), size = 0.7) + 
   scale_fill_manual(values = c("skyblue", "grey97")) + 
   theme(panel.background = element_rect(fill = alpha("grey85", .5)),
         panel.ontop = TRUE, 
         legend.position = "none")

在此处输入图像描述

方法三

更新到 ggplot 版本 3.0.0 的小修改

library(grid)
bg <- "grey92"
p <- ggplot() + 
   geom_polygon(data = AG, aes(long, lat, group = group, fill = hole), 
      colour = alpha("darkred", 1/2), size = 0.7) + 
   scale_fill_manual(values = c("skyblue", bg)) + 
   theme(panel.background = element_rect(fill = bg),
         legend.position = "none")

# Get the ggplot grob
g <- ggplotGrob(p)

# Get the Grid lines
grill <- g[7,5]$grobs[[1]]$children[[1]]

# grill includes the grey background. Remove it.
grill$children[[1]] <- nullGrob()  

# Draw the plot, and move to the panel viewport
p
downViewport("panel.7-5-7-5")

# Draw the edited grill on top of the panel
grid.draw(grill)
upViewport(0)

但是这个版本可能对 ggplot 的更改更健壮一些

library(grid)
bg <- "grey92"
p <- ggplot() + 
   geom_polygon(data = AG, aes(long, lat, group = group, fill = hole), 
      colour = alpha("darkred", 1/2), size = 0.7) + 
   scale_fill_manual(values = c("skyblue", bg)) + 
   theme(panel.background = element_rect(fill = bg),
         legend.position = "none")

# Get the ggplot grob
g <- ggplotGrob(p)

# Get the Grid lines
grill <- getGrob(grid.force(g), gPath("grill"), grep = TRUE)

# grill includes the grey background. Remove it.
grill = removeGrob(grill, gPath("background"), grep = TRUE)

# Get the name of the viewport containing the panel grob.
# The names of the viewports are the same as the names of the grobs.
# It is easier to select panel's name from the grobs' names
names = grid.ls(grid.force(g))$name
match = grep("panel.\\d", names, value = TRUE)

# Draw the plot, and move to the panel viewport
grid.newpage(); grid.draw(g)
downViewport(match)

# Draw the edited grill on top of the panel
grid.draw(grill)
upViewport(0)

在此处输入图像描述

于 2016-07-06T04:25:07.977 回答