Building a form file in ggplot2

I am trying to figure out how to display my full map in gglot2, including the island. Both r_base and tmap were able to map the islands, but ggplot2 was unable to distinguish the island from the rest of the water ... enter image description here .

My question is: how to make the islands appear in ggplot2?

See the code I used below.

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

Persian Gulf fill fill called iho

PG <- readShapePoly("iho.shp")

form file is available here

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

plot with r_base

Q<-plot(PG)

Corresponds to Figure A

Build with tmap

qtm(PG)

Corresponds to figure B

convert to dataframe

AG <- fortify(PG)

Plot with ggplot2

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

Corresponds to figure C

+5
source share
2 answers

You need to tell ggplot that you want the holes to fill with a different color .. for example:

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")

Also try the readOGR() function from the rgdal package instead of readShapePoly() , which stores all the projection information and data when reading the form file.

+2
source

In addition to @AdamMccurdy's answer: there are some possibilities to get the same color for the islands and adjacent background.

The first sets the fill color of the islands, and the background color is the same. But the grid lines are under the polygon and thus disappear.

The second attempt is to return the grid lines. It displays a background (which includes grid lines) on top of the panel (using panel.ontop = TRUE ). But this is a bit of a violin that adjusts alpha values ​​to get the same background and color of the island.

The third sets the background and island color to the same (as in the first), then displays the grid lines on top of the panel. There are several ways to do this; here, I grab the grob grid lines from the original graph, and then draw them on top of the panel. Thus, the colors remain unchanged and do not need alpha transparency.

 library(ggplot2) library (rgdal) library (rgeos) library(maptools) PG <- readOGR("iho.shp", layer = "iho") AG <- fortify(PG) 

Method 1

 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") 

enter image description here

Method 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

Method 3

 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[3,4]$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.3-4-3-4") # Draw the edited grill on top of the panel grid.draw(grill) upViewport(0) 

enter image description here

+3
source

Source: https://habr.com/ru/post/1242023/


All Articles