如何在 R 中制作可视化动物再诱捕时间的情节
How to make a plot visualizing timing of animal retraps in R
我有一个大型的标记动物数据库(从 2011 年到 2014 年)。我的目标是以每个被标记的个体在标记时都有自己的行开始的方式可视化数据,即第一次被捕获并在最后一次被捕获时结束。这两个事件(以及之间的重新捕获)将用完整的圆圈标记。应该在事件的时间安排中给出额外的信息(空心圆圈),当个体没有被抓住时,我知道它在那个时候还活着time(大概下图会更好解释)。
是否有任何R包(函数)能够创建这样的情节?例如一些直接用于动物诱捕数据的包?
我试着按照my.data
画了剧情
my.data <- data.frame(individual = c("ind_01","ind_02","ind_01",
"ind_02","ind_02","ind_03",
"ind_04","ind_04","ind_03",
"ind_05","ind_06","ind_03",
"ind_05","ind_02","ind_06"),
day = rep(1, times = 15),
month = c(2,2,9,9,4,4,4,11,3,3,3,8,8,12,12),
year = c(rep(c(2011,2012,2013),
times = c(4,4,7))))
你可以使用 ggplot
来做到这一点,我对数据进行了一些转换,基本上是将数据添加到所有个体中添加所有可能的陷阱日期。
如果个体没有被困住,如果没有看到动物,则在铸造的数据框中添加一个 NA。然后,我将两次目击之间的所有 NA 更改为 not_seen
,融化数据以绘制它并添加一个包含第一次目击年份的列。
library(reshape2)
#make a date column with the day, month and year
my.data$date <- as.Date(paste(my.data$day,my.data$month,my.data$year,sep="-"),format="%d-%m-%Y")
#cast the data to have individuals as columns and dates as row
cast_data<-dcast(my.data,date~individual,value.var="individual")
rownames(cast_data)<-cast_data$date
cast_data <- cast_data[,-1]
#replace the NAs that are between animal sightings with "not_seen"
obs_data <- apply(cast_data,2,function(x){
seen_dates <- which(!is.na(x))
x[seen_dates] <- "seen"
x[setdiff(seq(seen_dates[1],tail(seen_dates,1)),seen_dates)]<-"not_seen"
x
})
#melt the data and add a first_seen column that has the year of first sighting
data<-as.data.frame(melt(obs_data))
head(data)
colnames(data) <- c("obs_date","ind_id","obs")
data<-data[!is.na(data$obs),]
data$obs_date<-as.Date(data$obs_date)
for (i in unique(data$ind_id)){
data[data$ind_id==i,4] <- format(data[data$ind_id==i,1][1],"%Y")
}
colnames(data) <- c("obs_date","ind_id","obs","first_seen")
#use ggplot to make the plot
ggplot(data,aes(obs_date,ind_id,colour=first_seen))+geom_point(aes(shape=obs),size=10)+scale_x_date()+geom_line()+scale_shape_manual(values=c(1,19))
我有一个大型的标记动物数据库(从 2011 年到 2014 年)。我的目标是以每个被标记的个体在标记时都有自己的行开始的方式可视化数据,即第一次被捕获并在最后一次被捕获时结束。这两个事件(以及之间的重新捕获)将用完整的圆圈标记。应该在事件的时间安排中给出额外的信息(空心圆圈),当个体没有被抓住时,我知道它在那个时候还活着time(大概下图会更好解释)。
是否有任何R包(函数)能够创建这样的情节?例如一些直接用于动物诱捕数据的包?
我试着按照my.data
画了剧情
my.data <- data.frame(individual = c("ind_01","ind_02","ind_01",
"ind_02","ind_02","ind_03",
"ind_04","ind_04","ind_03",
"ind_05","ind_06","ind_03",
"ind_05","ind_02","ind_06"),
day = rep(1, times = 15),
month = c(2,2,9,9,4,4,4,11,3,3,3,8,8,12,12),
year = c(rep(c(2011,2012,2013),
times = c(4,4,7))))
你可以使用 ggplot
来做到这一点,我对数据进行了一些转换,基本上是将数据添加到所有个体中添加所有可能的陷阱日期。
如果个体没有被困住,如果没有看到动物,则在铸造的数据框中添加一个 NA。然后,我将两次目击之间的所有 NA 更改为 not_seen
,融化数据以绘制它并添加一个包含第一次目击年份的列。
library(reshape2)
#make a date column with the day, month and year
my.data$date <- as.Date(paste(my.data$day,my.data$month,my.data$year,sep="-"),format="%d-%m-%Y")
#cast the data to have individuals as columns and dates as row
cast_data<-dcast(my.data,date~individual,value.var="individual")
rownames(cast_data)<-cast_data$date
cast_data <- cast_data[,-1]
#replace the NAs that are between animal sightings with "not_seen"
obs_data <- apply(cast_data,2,function(x){
seen_dates <- which(!is.na(x))
x[seen_dates] <- "seen"
x[setdiff(seq(seen_dates[1],tail(seen_dates,1)),seen_dates)]<-"not_seen"
x
})
#melt the data and add a first_seen column that has the year of first sighting
data<-as.data.frame(melt(obs_data))
head(data)
colnames(data) <- c("obs_date","ind_id","obs")
data<-data[!is.na(data$obs),]
data$obs_date<-as.Date(data$obs_date)
for (i in unique(data$ind_id)){
data[data$ind_id==i,4] <- format(data[data$ind_id==i,1][1],"%Y")
}
colnames(data) <- c("obs_date","ind_id","obs","first_seen")
#use ggplot to make the plot
ggplot(data,aes(obs_date,ind_id,colour=first_seen))+geom_point(aes(shape=obs),size=10)+scale_x_date()+geom_line()+scale_shape_manual(values=c(1,19))