如何将此堆叠条形图转换为R中的圆形图?

n8ghc7c1  于 2022-12-05  发布在  其他
关注(0)|答案(1)|浏览(117)

我想把这个堆积条形图转换成R的圆形图。

但脚本here仅用于分组条形图(如您在圆圈中心所见:A、B、C、D)。
如何将此堆叠条形图转换为圆形图?
以下是我的数据:

df.bar <- data.frame(Gene.name=c('Gene1','Gene1','Gene1','Gene2','Gene2','Gene2','Gene3','Gene3','Gene3',
                                 'Gene4','Gene4','Gene4'),
                     Alteration.frequency=c(0.0909, 2.1838, 0.6369, 0.1819, 1.0919, 0.3639, 0.4549,0.7279,
                                            0.7279, 0.000, 0.3639, 0.4549),
                     Alteration.Type=c("Deep deletion",  "Amplification",  "Point mutation", "Deep deletion",
                                       "Amplification",  "Point mutation", "Deep deletion",  "Amplification" ,
                                       "Point mutation", "Deep deletion",  "Amplification" , "Point mutation"))

谢谢你的帮助。

oknwwptz

oknwwptz1#

条形图中只有四个列,因此如果将这些列堆叠在一起,圆形图看起来会很奇怪(它会呈十字形)。我想如果我用数据制作圆形图,我会避开填充变量:

library(ggplot2)
library(geomtextpath)

ggplot(df.bar, aes(Gene.name, Alteration.frequency, fill = Alteration.Type)) +
  annotate('textsegment', label = c("0", "1", "2"), x = c(-Inf, -Inf, -Inf), 
           xend = c(Inf, Inf, Inf), y = c(0, 1, 2), yend = c(0, 1, 2),
           linewidth = 0.2, linecolour = 'gray50', hjust = 0.75) +
  geom_hline(yintercept = c(0.5, 1.5), size = 0.2, colour = 'gray75') +
  geom_col(width = 0.5, position = position_dodge(width = 0.7)) +
  coord_polar() +
  ylim(c(-2, 2.2)) +
  annotate('textsegment', label = levels(factor(df.bar$Gene.name)), 
           x = -0.3 + 1:4, xend = 0.3 + 1:4, y = rep(-0.1, 4), 
           yend = rep(-0.1, 4), vjust = 1.2) +
  theme_void() +
  scale_fill_brewer("Alteration Type", palette = 'Set2') +
  labs(y = 'Alteration Frequency') +
  theme(axis.title.y = element_text(angle = 90))

相关问题