Home > Software design >  Filter plotted points by year
Filter plotted points by year

Time:12-17

I have species occurrence records plotted as points on a UK map created with ggplot2. In my csv, there is a column for record year - how do I add a filter so that only records from a specific year/year range are plotted rather than everything? Code is below, happy with map design just want to only plot some records!

library(ggplot2)
library("sf")
library(rnaturalearth)
library(rnaturalearthdata)
library(ggspatial)
book1points <- read.csv("removeddirectoryforprivacy.csv")
theme_set(theme_bw())
world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)
ggplot(data = world)  
  geom_sf()  
  geom_point(data = book1points, aes(x = lon, y = lat), size = 0.4, shape = 22, fill = "darkred")   
  xlab("Longitude")   ylab("Latitude")  
  ggtitle("Book1")  
  annotation_scale(location = "bl", width_hint = 0.3)  
  annotation_north_arrow(location = "bl", which_north = "true", 
                         pad_x = unit(0.22, "in"), pad_y = unit(0.2, "in"),
                         style = north_arrow_fancy_orienteering)  
  coord_sf(xlim=c(-11.5,3), ylim=c(49,61), expand = FALSE)  
  theme(panel.grid.major = element_line(color = gray(0.5), linetype = "dashed", size = 0.2), panel.background = element_rect(fill = "aliceblue"))

CodePudding user response:

As @GregorThomas mentioned, you want to give ggplot a subset. You can use filter on the data in geom_point.

library(tidyverse)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(ggspatial)

ggplot(data = world)  
  geom_sf()  
  geom_point(
    data = book1points %>% dplyr::filter(year == 2021),
    aes(x = lon, y = lat),
    size = 0.4,
    shape = 22,
    fill = "darkred"
  )  
  xlab("Longitude")   ylab("Latitude")  
  ggtitle("Book1")  
  annotation_scale(location = "bl", width_hint = 0.3)  
  annotation_north_arrow(
    location = "bl",
    which_north = "true",
    pad_x = unit(0.22, "in"),
    pad_y = unit(0.2, "in"),
    style = north_arrow_fancy_orienteering
  )  
  coord_sf(xlim = c(-11.5, 3),
           ylim = c(49, 61),
           expand = FALSE)  
  theme(
    panel.grid.major = element_line(
      color = gray(0.5),
      linetype = "dashed",
      size = 0.2
    ),
    panel.background = element_rect(fill = "aliceblue")
  )

Output

enter image description here

Or if you have multiple years, then you could use:

data = book1points %>% dplyr::filter(year %in% 2020:2021)

Or as @GregorThomas provided, you can also use base R in that line instead of dplyr.

data = subset(book1points, year == 2020)
data = subset(book1points, year %in% 2020:2021)

Data

book1points <- structure(list(
  lon = c(-2.1767,-0.44086,-2.791934,-1.253848,-0.253848),
  lat = c(54.236969, 53.144121, 52.386316, 52.932735,
        52.932735),
  year = c(2020, 2021, 2020, 2021, 2021)
),
class = "data.frame",
row.names = c(NA,-5L))
  • Related