-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircle_Plot_PCBs
70 lines (54 loc) · 2.58 KB
/
Circle_Plot_PCBs
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
#Circos plot of PCBs >LOQ, categorized by number of chlorines
library(tidyverse)
install.packages("ggtext")
library(ggtext)
install.packages("patchwork")
library(patchwork)
library(ggplot2)
#code from https://r-graph-gallery.com/297-circular-barplot-with-groups.html
# Set a number of 'empty bar' to add at the end of each group
data <- read.xlsx("NADA.xlsx", sheet = "percent_LOQ")
data <- read.xlsx("test.xlsx")
# Set a number of 'empty bar'
empty_bar <- 30
# Add lines to the initial dataset
to_add <- matrix(NA, empty_bar, ncol(data))
colnames(to_add) <- colnames(data)
data <- rbind(data, to_add)
data$id <- seq(1, nrow(data))
# prepare a data frame for base lines
base_data <- data %>%
group_by(group) %>%
#summarize(start=min(id), end=max(id) - empty_bar) %>%
summarize(start=min(id), end=max(id)) %>%
rowwise() %>%
mutate(title=mean(c(start, end)))
# prepare a data frame for grid (scales)
grid_data <- base_data
grid_data$end <- grid_data$end[ c( nrow(grid_data), 1:nrow(grid_data)-1)] + 1
grid_data$start <- grid_data$start - 1
grid_data <- grid_data[-1,]
# Make the plot
p <- ggplot(data, aes(x=as.factor(id), y=value, fill=group)) + # Note that id is a factor. If x is numeric, there is some space between the first bar
geom_bar(aes(x=as.factor(id), y=value, fill=group), stat="identity", alpha=0.5) +
# Add a val=50/10 lines. I do it at the beginning to make sure barplots are OVER it.
geom_segment(data=grid_data, aes(x = end, y = 50, xend = start, yend = 50), colour = "azure4", alpha=0.8, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 10, xend = start, yend = 10), colour = "azure4", alpha=0.8, size=0.3 , inherit.aes = FALSE ) +
# Add text showing the value of each 50/10 lines
annotate("text", x = rep(max(data$id),2), y = c(10, 50), label = c("10%", "50%") , color="azure4", size=1.8 , angle=0, fontface="bold", hjust=1) +
geom_bar(aes(x=as.factor(id), y=value, fill= group), stat="identity", alpha=0.5) +
scale_fill_brewer(palette="RdBu") +
ylim(-50,120) +
theme_minimal() +
theme(
legend.position = "none",
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
plot.margin = unit(rep(-1,4), "cm")
) +
coord_polar() +
# Add base line information
geom_segment(data=base_data, aes(x = start, y = -3, xend = end, yend = -3), alpha=0.8, size=0.35 , inherit.aes = FALSE, colour = "black") +
geom_text(data=base_data, aes(x = title, y = -17, label=group), hjust=0.5, colour = "black", alpha=0.8, size=1.8, angle= 0, fontface="plain", inherit.aes = FALSE)
p