Home > Enterprise >  How to add sample size and number of records to y labels ggplot
How to add sample size and number of records to y labels ggplot

Time:10-03

I've got a dataset in which there are records of species sightings per month and site and I am working on some abundance plots with ggplot2. I would like to add to the y axis the total number of samplings and the total number of sightings.

Let's say that on site A there were 5 samplings on different dates:

Sam = c("01-01-1990","10-01-1990","01-03-1990","21-04-1990","17-05-1993","01-01-1990","10-01-1990","25-02-1995","01-01-1990","06-12-1990"),
Sp1 = c(2,4,7,3,1,9,4,6,4,7),
Location = c(rep("A", 5), rep("B", 3), rep("C", 2)),
Time= c(1,2,2,3,5,4,5,9,3,7)

I would like ggplot to display on the y axis something like:


A (5,17) 
B (3,13)
.
.
.
And so on

A (5,17) represents the total number of samplings in location A (N=5) and the cumulative sum of Sp1 in those 5 samplings (N=17).

I've tried something like below but don't get the expected outcome.

ylabs <- paste(gsub("/","-\n",names(table(tmp_i$Sp1)),fixed=T),
                 "(N=",table(tmp_i$Sp1),")",sep="")

scale_y_discrete(labels=ylabs)

I would like to do this in a batch for all different species and locations.Any help would be greatly appreciated.

Thanks!

CodePudding user response:

Not sure about how your final plot should look like. But using e.g. dplyr you could add a column to your dataset which beside the Location contains the number of samplings and the total number of sightings and which could then be mapped on the y aes:

dat <- data.frame(
  Sam = c("01-01-1990","10-01-1990","01-03-1990","21-04-1990","17-05-1993","01-01-1990","10-01-1990","25-02-1995","01-01-1990","06-12-1990"),
  Sp1 = c(2,4,7,3,1,9,4,6,4,7),
  Location = c(rep("A", 5), rep("B", 3), rep("C", 2)),
  Time= c(1,2,2,3,5,4,5,9,3,7)
)

library(dplyr, warn = FALSE)
library(ggplot2)

dat_sum <- dat %>%
  group_by(Location) %>%
  mutate(loc_label = paste0(Location, " (", n(), ",", sum(Sp1), ")")) %>%
  ungroup()

ggplot(dat_sum, aes(Time, loc_label))  
  geom_point(size = 2)

  • Related