在 R 中的圆轴内绘制 geom_segments

Draw geom_segments within a circular axis in R

我需要帮助才能用 geom_segments 沿着圆形图形绘制复杂的图。

我有一个数据框,例如

  Seq start end
1  S1 61.25   5
2  S2 30.00  35
3  S3 40.00  45
4  S4 52.50   0

我想创建一个图形,例如:

我在圆轴周围绘制每个条形坐标。

y 轴内的条形顺序没有偏好。

如果有帮助,这里是 dput 格式的数据帧

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

这让你非常接近。您可能需要调整 geom_text() 中的角度值以使其更接近。

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

这让你非常接近,使用 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()