To merge maps of land without water and contour plots and remove the ocean areas, you can follow these steps:
library(ggplot2)
library(maptools)
library(raster)
land <- readShapePoly("land.shp")
contours <- raster("elevation.tif")
contours.df <- fortify(contours)
merged.df <- merge(land, contours.df, by = "id")
ggplot(merged.df) +
geom_polygon(aes(x = long, y = lat, group = group, fill = value)) +
coord_equal() +
theme_void()
To obscure the ocean areas, you can add a layer with a polygon that covers the entire plot area with a color that matches your plot background. For example:
ggplot(merged.df) +
geom_polygon(aes(x = long, y = lat, group = group, fill = value)) +
coord_equal() +
theme_void() +
geom_polygon(data = world, aes(long, lat, group = group), fill = "white")
To fill in the ocean areas with a color, you can use the ggrepel package to create a layer with text labels that cover the ocean areas, and set the text color to match your plot background. For example:
library(ggrepel)
ggplot(merged.df) +
geom_polygon(aes(x = long, y = lat, group = group, fill = value)) +
coord_equal() +
theme_void() +
geom_text_repel(data = world, aes(long, lat, label = " "), size = 10, color = "white")
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-04-20 11:00:00 +0000
Seen: 10 times
Last updated: Feb 07 '23
What is the method of inserting y-axis labels into a coord_polar chart in ggplot?
Why is ggplot displaying decimal values in the legend when the column contains numerical values?
What is the method for merging two plots in R studio with ggplot?
In ggplot, certain bin intervals in the legend are lacking color.