I would need help in order to draw a complex plot with geom_segments along a circular figure.
I have a dataframe such as
Seq start end
1 S1 61.25 5
2 S2 30.00 35
3 S3 40.00 45
4 S4 52.50 0
And I would like to create a figure such as :
Where I plot each bar coordinates within around the circular axis.
There is no preference about the order of the bars within the y axis.
Here is the dataframe in dput format if it can helps
structure(list(Seq = c("S1", "S2", "S3", "S4"), start = c(61.25,
30, 40, 52.5), end = c(5L, 35L, 45L, 0L)), class = "data.frame", row.names = c(NA,
-4L))
CodePudding user response:
This gets you very close, using geom_textpath
:
library(dplyr)
library(geomtextpath)
df %>%
group_by(Seq) %>%
summarize(start = if(start[1] > end[1]) c(start[1], 0) else start[1],
end = if(start[1] > end[1]) c(70, end[1]) else end[1]) %>%
ggplot(aes(start, as.numeric(factor(Seq))))
geom_segment(aes(xend = end, yend = as.numeric(factor(Seq))), size = 1.5)
geom_hline(yintercept = seq(0, 4) 0.5, col = 'gray80')
geom_hline(yintercept = 0.5)
geom_textpath(data = data.frame(x = unique(c(df$start, df$end, 17.5)), y = -1),
aes(x, y, label = x), size = 6)
scale_y_continuous(limits = c(-10, 5))
scale_x_continuous(limits = c(0, 70))
coord_polar()
theme_void()
CodePudding user response:
This gets you pretty close. You may want to tweak the angle values in geom_text()
to get it closer.
library(tidyr)
library(dplyr)
library(ggplot2)
text_labs <- df %>%
select(start, end) %>%
unlist() %>%
c(., 17.5) %>%
sort() %>%
unique() %>%
tibble(x = ., y = 70)
df %>%
mutate(flag = (end < start & end != 0) 1) %>%
uncount(flag, .remove = FALSE) %>%
group_by(Seq) %>%
mutate(start = case_when(row_number() == 2 ~ 0,
TRUE ~ start),
end = case_when(row_number() == 1 & n() == 2 ~ 70L,
end == 0 ~ 70L,
TRUE ~ end)) %>%
mutate(y = cur_group_id() * 5 77.5) %>%
ungroup() %>%
add_row(start = 0, end = 70, y = 77.5) %>%
ggplot(aes(y = y, x = start))
geom_segment(aes(yend = y, xend = end), size = 2)
geom_hline(yintercept = seq(80, 100, by = 5), colour = "grey90", size = 0.2)
geom_text(aes(x, y, label = x), data = text_labs)
scale_x_continuous(limits = c(0, 70), breaks = NULL)
scale_y_continuous(limits = c(0, 100), labels = NULL)
theme_minimal()
labs(x = NULL, y = NULL)
coord_polar()
theme(axis.line.x = element_blank(),
panel.grid = element_blank())