-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageCount_drawing.Rmd
94 lines (74 loc) · 3.02 KB
/
ImageCount_drawing.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
---
title: "ImageCount"
author: "Ye Bi"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
# Load necessary libraries
library(ggplot2)
library(dplyr)
```
```{r}
# Load necessary libraries
library(ggplot2)
library(dplyr)
# Load the CSV files
df1 <- read.csv("../../OutputsSummary//labelled_depth_0718.csv")
df2 <- read.csv("../../OutputsSummary//labelled_depth_0801.csv")
df3 <- read.csv("../../OutputsSummary//labelled_depth_0815.csv")
df4 <- read.csv("../../OutputsSummary//labelled_depth_0829.csv")
df5 <- read.csv("../../OutputsSummary//labelled_depth_0912.csv")
df6 <- read.csv("../../OutputsSummary//labelled_depth_0927.csv")
# List of DataFrames and corresponding titles
dfs <- list(df1, df2, df3, df4, df5, df6)
titles <- c("T1", "T2", "T3", "T4", "T5", "T6")
```
```{r}
library(ggplot2)
library(dplyr)
# Process the combined DataFrame with the added total counts for each Visit
process_data <- function(df) {
# Count the number of images per Bag_ID and Visit
bag_id_counts <- df %>%
count(Bag_ID, Visit) %>%
arrange(desc(n)) %>%
group_by(Visit) %>%
mutate(Bag_ID = factor(Bag_ID, levels = Bag_ID[order(n, decreasing = TRUE)])) %>%
ungroup()
# Add columns for the total image count and unique Bag_ID count for each Visit
summary_counts <- bag_id_counts %>%
group_by(Visit) %>%
summarize(total_images = sum(n),
unique_bag_ids = n_distinct(Bag_ID))
bag_id_counts <- bag_id_counts %>%
left_join(summary_counts, by = "Visit")
return(bag_id_counts)
}
# Process the combined DataFrame
processed_data <- process_data(combined_df)
# Plot using ggplot2 with facet_wrap and total image count + unique Bag_ID count annotation
p <- ggplot(processed_data, aes(x = factor(Bag_ID), y = n, fill = Visit)) +
geom_bar(stat = "identity", alpha = 0.7, position = 'dodge', linewidth = 0.3) +
geom_hline(yintercept = 20, linetype = "dashed", color = "red") +
labs(x = "Pig ID", y = "Number of Images") +
scale_y_continuous(breaks = seq(0, max(processed_data$n, na.rm = TRUE), by = 100)) +
theme_bw() +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
panel.grid.major.x = element_blank()) +
scale_fill_brewer(palette = "Paired") +
scale_color_brewer(palette = "Paired") +
facet_wrap(~ Visit, scales = 'free') +
geom_label(data = processed_data %>%
group_by(Visit) %>%
summarize(total_images = unique(total_images),
unique_bag_ids = unique(unique_bag_ids)),
aes(x = Inf, y = Inf, label = paste("Total images:", total_images, "\nTotal pigs:", unique_bag_ids, " ")),
hjust = 1.05, vjust = 1.2, size = 3, color = "black", fill = "white", inherit.aes = FALSE, label.size = 0.5, label.padding = unit(0.2, "lines"))
print(p)
dev.print(pdf, file="../../OutputsSummary/SummaryImages/image_count.pdf", height=6, width=10)
```