Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw geom_segments within a circular axis in R

Tags:

plot

r

ggplot2

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 :

enter image description here

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))
like image 892
chippycentra Avatar asked Oct 28 '25 08:10

chippycentra


1 Answers

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()

enter image description here

like image 149
Allan Cameron Avatar answered Oct 29 '25 23:10

Allan Cameron