Ответ 1
Мой подход к вашему вопросу является прагматичным: просто поместите слой стран Персидского залива над распределением тепловой карты. Соответственно, это сажат. Обратите внимание, однако, что тепловая карта все еще рассчитана так, как если бы она не была обрезана. Это означает, что расчет плотности не ограничивается только водным телом, но он просто обрезается визуально.
В целях воспроизводимости в следующем коде предполагается, что вы распаковали файл .rar
, предоставленный @Hammao, и выполните код в результирующей папке Persian Gulf
.
# get sample data
sample.data <- read.csv("sample.data3.csv")
Теперь нам нужно получить формы страны для стран Персидского залива. Для этого я использую rworldmap
.
# loading country shapes
library(rworldmap)
# download map of the world
worldmap <- getMap(resolution = "high") # note that for 'resolution="high"'
# you also need the "rworldxtra" pkg
# extract Persian Gulf countries...
gulf_simpl <- worldmap[worldmap$SOVEREIGNT == "Oman" |
worldmap$SOVEREIGNT == "Qatar" |
worldmap$SOVEREIGNT == "United Arab Emirates" |
worldmap$SOVEREIGNT == "Bahrain" |
worldmap$SOVEREIGNT == "Saudi Arabia" |
worldmap$SOVEREIGNT == "Kuwait" |
worldmap$SOVEREIGNT == "Iraq" |
worldmap$SOVEREIGNT == "Iran", ]
# ... and fortify the data for plotting in ggplot2
gulf_simpl_fort <- fortify(gulf_simpl)
# Now read data for the Persian Gulf, which we need to get the distances for
# the extension of the map
PG <- readOGR(dsn = ".", "iho")
PG <- readShapePoly("iho.shp")
PG <- fortify(PG)
Теперь это просто вопрос построения слоев в правильном порядке.
# generate plot
ggplot(sample.data) +
# first we plot the density...
stat_density_2d(aes(x = long, y = lat,
fill = ..level..),
geom="polygon",
alpha = 0.5) +
# ... then we plot the points
geom_point(aes(x = long, y = lat)) +
# gradient options
scale_fill_gradient(low = "green", high = "red") +
scale_alpha(range = c(0.00, 0.25), guide = FALSE) +
# and now put the shapes of the gulf states on top
geom_polygon(data = gulf_simpl_fort,
aes(x = long,
y = lat, group = group),
color = "black", fill = "white",
inherit.aes = F) +
# now, limit the displayed map only to the gulf
coord_equal(xlim = c(min(PG_fort$long), max(PG_fort$long)),
ylim = c(min(PG_fort$lat), max(PG_fort$lat))) +
theme_bw()