简体   繁体   English

按年份过滤绘制点

[英]Filter plotted points by year

I have species occurrence records plotted as points on a UK map created with ggplot2.我在用 ggplot2 创建的英国 map 上将物种发生记录绘制为点。 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?在我的 csv 中,有一列记录年份 - 如何添加过滤器以便仅绘制特定年份/年份范围内的记录而不是所有内容? Code is below, happy with map design just want to only plot some records!代码如下,对 map 的设计很满意,只想 plot 一些记录!

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

As @GregorThomas mentioned, you want to give ggplot a subset.正如@GregorThomas 提到的,你想给ggplot一个子集。 You can use filter on the data in geom_point .您可以对geom_point中的数据使用filter

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 Output

在此处输入图像描述

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 .或者正如@GregorThomas 提供的那样,您也可以在该行中使用基础 R 而不是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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM