I am attempting my first spatial maps in ggplot
:
I am quite hapy with the map and used the code:
g <-ggplot(data = world) +
geom_sf() +
coord_sf(crs = st_crs(4326)) +
coord_sf(xlim = c(-10.5, -5.70), ylim = c(51.30, 55.30)) #+ xlab("Longitude") + ylab("Latitude") # sets frame
g <- g + labs(x="Longitude",y="Latitude")
g
g<- g+ geom_point(data = mapdatasize, aes(x = Y, y = X.y), size= mapdatasize$no, show.legend = TRUE) #dont know why it doesnt show legend...?
g<- g+ scale_size_continuous(name = "No of Animals") ##doesnt work neither?
g
g <- g+ annotation_scale(location = "br",style="ticks", width_hint = 0.5) + #bl is bottom left, br bottom right
annotation_north_arrow(location = "br", which_north = "true",
pad_x = unit(0, "in"), pad_y = unit(0.2, "in"), #northarrow and scale bar
style = north_arrow_fancy_orienteering)
g <- g + theme_classic(base_size = 15)
g<- g + theme(panel.border = element_rect(linetype = "solid", fill = NA))
g
I am missing my legend of the point sizes though. I don't know why ggplot
doesn't show the legend even though I use legend = T
? My try with scale_size_continuous
doesn't do it neither.
Numbers that define the size range from 1-4 I believe (no of animals at this location).
Any advice?
Just place the size= mapdatasize$no
inside the aes()
:
library(giscoR)
library(ggplot2)
library(sf)
library(dplyr)
library(ggspatial)
# I need to mock your data somehow...
world <- gisco_get_countries(region = "Europe")
# Mock your animal data
mapdatasize <- world %>%
filter(ISO3_CODE == "IRL") %>%
st_sample(20) %>%
st_coordinates() %>%
as.data.frame()
names(mapdatasize) <- c("Y", "X.y")
mapdatasize$no <- sample(1:4, 20, replace = TRUE)
# End mocking data
ggplot(data = world) +
geom_sf() +
coord_sf(
crs = st_crs(4326),
xlim = c(-10.5, -5.70), ylim = c(51.30, 55.30)
) +
# Here it goes!
geom_point(data = mapdatasize, aes(x = Y, y = X.y, size = no)) +
# Ready
labs(x = "Longitude", y = "Latitude") +
annotation_scale(location = "br", style = "ticks", width_hint = 0.5) +
annotation_north_arrow(
location = "br", which_north = "true",
pad_x = unit(0, "in"), pad_y = unit(0.2, "in"),
style = north_arrow_fancy_orienteering
) +
theme_classic(base_size = 15) +
theme(panel.border = element_rect(linetype = "solid", fill = NA))