在ggplot2中绘制形状文件

8

我正在尝试弄清楚如何在ggplot2中显示完整的地图,包括岛屿。 r_base和tmap都能够显示岛屿,但ggplot2无法区分岛屿与其余水域...

enter image description here

我的问题是如何使岛屿在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

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") 同时尝试使用rgdal包中的readOGR()函数而不是readShapePoly(),当您读取shape文件时它会保留所有投影和基准信息。

@ Adam Mccurdy,能否将它们设置为透明以匹配相邻的背景.. 否则我必须使用黑白主题。 - Hammao
1
@Hammo,我不太确定如何做到这一点,但我相信有办法。您可以使用values = c("skyblue", alpha("white", .5))为每种颜色设置单独的透明度。不幸的是,ggplot首先绘制大多边形,然后在上面绘制孔和分离的多边形,因此如果您增加白色的透明度,您只会看到天蓝色下面而不是实际的网格。您可以将岛屿更改为与背景相同的颜色,但您将无法获得网格线。另一个想法是类似于Arc中的擦除功能。 - admccurdy
这个问题是你必须在加强shapefile之前解决的。如果你把它作为第二个问题发布在stackexchange上,我相信会有人很快知道答案。 - admccurdy

8

除了@AdamMccurdy的答案之外,还有一些可能性可以使岛屿和相邻背景颜色相同。

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

第二种方法是试图恢复网格线。它将背景(包括网格线)绘制在面板顶部(使用panel.ontop = TRUE)。但需要调整alpha值来使背景和岛屿颜色相同。

第三种方法是将背景和岛屿颜色设置为相同(与第一种方法相同),然后在面板顶部绘制网格线。有几种方法可以实现这一点;在这里,我从原始绘图中获取网格线grob,然后将其绘制在面板顶部。因此颜色保持不变,无需使用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")

这里输入图片描述

方法2


这是一张图片,无法进行翻译。
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")

enter image description here

方法三

对于 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)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接