Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在同一图表中选择并绘制两天之间的夜间小时数_R_Ggplot2_Plot_Time Series - Fatal编程技术网

R 在同一图表中选择并绘制两天之间的夜间小时数

R 在同一图表中选择并绘制两天之间的夜间小时数,r,ggplot2,plot,time-series,R,Ggplot2,Plot,Time Series,我想画出在夜间检测到的个体数量,在18小时到6小时之间。我找不到在一张图上表示这一点的解决方案,以第1天的18-00小时和第2天的00-6小时为单位。有什么想法吗 我简化了这类数据: year site id date hour 2018 A 1 24/07/2018 01:58:50 2018 A 2 24/07/2018 20:44:44 2018 A 2 24/07/2018 04:07:56 2018 A 1

我想画出在夜间检测到的个体数量,在18小时到6小时之间。我找不到在一张图上表示这一点的解决方案,以第1天的18-00小时和第2天的00-6小时为单位。有什么想法吗

我简化了这类数据:

year    site    id  date    hour
2018    A   1   24/07/2018  01:58:50
2018    A   2   24/07/2018  20:44:44
2018    A   2   24/07/2018  04:07:56
2018    A   1   25/07/2018  03:46:11
2018    A   2   25/07/2018  20:04:11
2018    A   2   25/07/2018  03:51:40
2018    A   1   26/07/2018  03:29:07
2018    A   2   26/07/2018  20:08:24
2018    A   2   26/07/2018  03:05:07
2018    A   1   27/07/2018  19:56:40
2018    A   1   27/07/2018  03:38:09
2018    A   2   27/07/2018  20:08:53
2018    A   2   27/07/2018  03:27:47
2018    A   1   28/07/2018  19:59:34
2018    A   1   28/07/2018  03:54:58
2018    A   2   28/07/2018  20:11:35
我用以下示例数据绘制了此图:

我想这样做:

这是我的代码:


df <- read.table(text = "year    site    id  date    hour
2018    A   1   24/07/2018  01:58:50
2018    A   2   24/07/2018  20:44:44
2018    A   2   24/07/2018  04:07:56
2018    A   1   25/07/2018  03:46:11
2018    A   2   25/07/2018  20:04:11
2018    A   2   25/07/2018  03:51:40
2018    A   1   26/07/2018  03:29:07
2018    A   2   26/07/2018  20:08:24
2018    A   2   26/07/2018  03:05:07
2018    A   1   27/07/2018  19:56:40
2018    A   1   27/07/2018  03:38:09
2018    A   2   27/07/2018  20:08:53
2018    A   2   27/07/2018  03:27:47
2018    A   1   28/07/2018  19:59:34
2018    A   1   28/07/2018  03:54:58
2018    A   2   28/07/2018  20:11:35", header = TRUE)


df$DATETIME <- lubridate::dmy_hms(paste(df$date, df$hour))

list_comm <- split(df, df$site) # split for each site

bar_plots <- lapply(list_comm, function(x){

  #x <- list_comm[[1]]

  tab <- x %>%
    mutate(HOUR = as.numeric(strftime(DATETIME, format = "%H"))) %>%
    group_by(hour, date, year) %>%
    summarise(count = as.numeric(length(id))) %>%
    select(hour, date, year, count)

  tab %>%
    ggplot(aes(x = hour, y = count, fill = count))+
    geom_bar(stat="identity", position="dodge") +
    scale_x_continuous(breaks=c(0, 3,4,5, 12, 20, 21, 22, 23),
                       labels=c("00:00", "03:00", "04:00", "05:00", "12:00", "20:00", "21:00", "22:00", "23:00"),
                       limits=c(0,24)) +
    scale_fill_continuous(low="blue", high="red") +
    facet_wrap(date~. , ncol =1, scales="free_x") +
    labs(x = "Hour", y = "Number of passage", title = paste(unique(x$site), "-", unique(x$year))) +
    theme_classic() +
    theme(strip.text=element_text(hjust=0, face="bold")) +
    theme(panel.grid.major.x=element_blank()) +
    theme(panel.grid.minor=element_blank()) +
    theme(plot.margin=margin(30,30,30,30)) +
    theme(axis.text.x=element_text(angle = 90, vjust = 0.5, size = 8)) 

  ggsave(paste("results/04_movement_pattern/new_marked_individuals/barplot_pheno/2018-",unique(x$site),"panel_pheno_summer_sites.png"), width = 10, height=10, units = "in") #Save plot

} 
)

print(bar_plots)


无论我多么努力地寻找解决方案,我都找不到,迷失了方向。我欢迎任何建议。提前感谢您:

据我所知,问题是您弄乱了您的小时变量。我猜您计划将连续小时变量映射到x轴上。相反,您尝试在连续的刻度上绘制分类字符变量hour,这将不起作用。要将小时添加到选项卡中,我将其包含在group_by和select语句中。为了得到那种情节,我加了一个小时的变换。试试这个:

df=19&小时% ggplotaesx=HOUR1,y=count,fill=count+ geom_barstat=身份,位置=道奇+ 比例尺x连续中断=0:10, 标签=c19:00、20:00、21:00、22:00、23:00、00:00、01:00、02:00、03:00、04:00、05:00, 极限=c0,10+ 刻度\填充\连续低=蓝色,高=红色+ 面_wrapdate~,ncol=1,刻度=自由度+ labsx=小时,y=通过次数,title=pasteuniquex$站点,-,uniquex$年+ 经典主题曲+ themestrip.text=element\u texthjust=0,face=bold+ themepanel.grid.major.x=元素\u空白+ themepanel.grid.minor=元素\u空白+ 边际利润=边际利润30,30,30,30+ themeaxis.text.x=element\u textangle=90,vjust=0.5,size=8 } bar_地块$A >警告:删除了3行,其中包含缺少的值geom_bar。
我在@stefan的宝贵帮助下解决了这个问题

bar_plots <- lapply(list_comm, function(x){

  x <- list_comm[[1]]

  tab <- x %>%
    mutate(HOUR = lubridate::hour(DATETIME)) %>%
    group_by(date) %>%
    slice(which(!(date == x$date[1] & HOUR < 19))) %>% #remove morning hour of the first day of capture 
    group_by(hour, date, year, HOUR) %>%
    summarise(count = as.numeric(length(id))) %>%
    select(hour, date, year, count, HOUR) %>% 
    mutate(HOUR1 = ifelse(HOUR >= 19 & HOUR <= 24, HOUR - 19, HOUR + 6)) %>% #to inverse the x axis 
    ungroup() %>%
    mutate(date = lubridate::dmy(date) - lubridate::days(if_else(HOUR >= 12, 0, 1))) #for hours between 0 to 12 you set the date to yesterday. This way you group hours 12 to 24 from today with hours 0 to 12 from tomorrow

  #Pass to english format to plot  
  Sys.setlocale("LC_TIME", "English")

 tab %>%
   mutate(date = paste(lubridate::day(date), "to", lubridate::day(lubridate::date(tab$date)+1), lubridate::month(date, label=TRUE, abbr = FALSE),lubridate::year(date))) %>%
    ggplot(aes(x = HOUR1, y = count, fill = count))+
    geom_bar(stat="identity", position="dodge") +
    scale_x_continuous(breaks=0:10,
                       labels=c("19:00", "20:00", "21:00", "22:00", "23:00", "00:00", "01:00", "02:00", "03:00", "04:00", "05:00"),
                       limits=c(0, 10)) +
    scale_fill_continuous(low="blue", high="red") +
    facet_wrap(date~. , ncol =1, scales="free_x") +
    labs(x = "Hour", y = "Number of passage", title = paste(unique(x$site), "-", unique(x$year))) +
    theme_classic() +
    theme(strip.text=element_text(hjust=0, face="bold")) +
    theme(panel.grid.major.x=element_blank()) +
    theme(panel.grid.minor=element_blank()) +
    theme(plot.margin=margin(30,30,30,30)) +
    theme(axis.text.x=element_text(angle = 90, vjust = 0.5, size = 8)) 

} 
)
bar_plots$A
该产品:


对于我的夜间活动分析非常有用:

Hey@maryvrx,如果您可以使用类似dputheaddf的东西,10并将输出粘贴到此处,它将使您的数据集样本可供其他人复制,更多人将通过这种方式提供帮助。如果你的数据集不太长,也可以直接使用dputdf。嘿@Yach谢谢你的评论。我有一个很长的数据集,但我会尝试编辑我的问题,使其更具可复制性。嘿@stefan,事实上,我忘了把它放在我的df simplified中,但我有一个同名的DATETIME变量。但即使这样,我似乎也找不到一种方法来表示下午6点到早上6点之间的计数@maryvrx我通过df$DATETIME添加了DATETIME我使用POSIXct创建了我的DATETIME变量:df$DATETIME我在代码中更正了这个错误,并更新了我的第一个post@maryvrx棘手的一个;第一对于小时的转换,只需使用lubridate::hours。至少在我的机器上,使用您的转换导致时间偏移2小时。下一个为了得到这样的情节,我对小时进行了变换。不知道是否有更简单的wy,例如使用日期轴。也许并不完美。但至少对我来说,这看起来像是你试图达到的目的。