簡體   English   中英

如何將此堆疊條形圖轉換為 R 中的圓形 plot?

[英]How can I convert this stacked barplot to a circular plot in R?

我想將此堆疊條形圖轉換為 R 中的圓形 plot。

在此處輸入圖像描述

此處的腳本僅適用於分組條形圖(如您在圓心處所見:A、B、C、D)。

如何將此堆疊條形圖轉換為圓形 plot?

這是我的數據:

   dput(df.bar)
structure(list(`Gene name` = c("Gene1", "Gene1", "Gene1", "Gene1", 
"Gene2", "Gene2", "Gene2", "Gene2", "Gene3", "Gene3", "Gene3", 
"Gene3", "Gene4", "Gene4", "Gene4", "Gene4", "Gene5", "Gene5", 
"Gene5", "Gene5", "Gene6", "Gene6", "Gene6", "Gene6", "Gene7", 
"Gene7", "Gene7", "Gene7", "Gene8", "Gene8", "Gene8", "Gene8", 
"Gene9", "Gene9", "Gene9", "Gene9", "Gene10", "Gene10", "Gene10", 
"Gene10", "Gene11", "Gene11", "Gene11", "Gene11", "Gene12", "Gene12", 
"Gene12", "Gene12", "Gene13", "Gene13", "Gene13", "Gene13", "Gene14", 
"Gene14", "Gene14", "Gene14", "Gene15", "Gene15", "Gene15", "Gene15", 
"Gene16", "Gene16", "Gene16", "Gene16"), `Alteration Frequency` = c(0, 
0.090991811, 2.183803458, 0.636942675, 0, 0.181983621, 1.091901729, 
0.363967243, 0, 0.454959054, 0.727934486, 0.727934486, 0, 0, 
0.363967243, 0.454959054, 0, 0, 1.364877161, 1.000909918, 0, 
0.272975432, 1.910828025, 0, 0, 0, 1.000909918, 0.727934486, 
0.090991811, 0.454959054, 14.83166515, 0.727934486, 0, 0, 1.18289354, 
0.363967243, 0, 0, 0.818926297, 0.363967243, 0, 0, 4.458598726, 
1.000909918, 0, 0, 0.636942675, 0.181983621, 0, 0, 0.636942675, 
0.181983621, 0.090991811, 0, 9.099181074, 0.363967243, 0, 0.181983621, 
0.363967243, 0.272975432, 0, 0.272975432, 0.181983621, 0.363967243
), `Alteration Type` = c("Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation", "Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation", "Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation", "Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation", "Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation", "Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation", "Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation", "Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation", "Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation", "Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation", "Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation", "Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation", "Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation", "Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation", "Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation", "Multiple Alteration", "Deep deletion", 
"Amplification", "Point mutation")), class = "data.frame", row.names = c(NA, 
-64L))

謝謝你的幫助。

你可以這樣做:

library(ggplot2)
library(geomtextpath)

ggplot(df.bar, aes(x = factor(`Gene name`, unique(`Gene name`)), 
                   y = `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.015) +
  geom_hline(yintercept = c(0.5, 1.5), size = 0.2, colour = 'gray75') +
  geom_col(width = 0.5, position = position_stack(), alpha = 0.8) +
  coord_polar(start = -pi/30) +
  ylim(c(-2, 2.2)) +
  annotate('segment', x = 1:16, xend = 1:16, y = rep(-1, 16),
           yend = rep(-0.6, 16), color = 'gray75') +
  annotate('text', x = 0, y = -2, label = 'Gene', size = 8,
           color = 'gray50') +
  annotate('textsegment', label = 1:16, 
           x = -0.3 + 1:16, xend = 0.3 + 1:16, y = rep(-0.1, 16), 
           yend = rep(-0.1, 16), vjust = 1.2, color = 'gray50') +
  theme_void() +
  scale_fill_brewer("Alteration Type", palette = 'Set2') +
  labs(title = 'Alteration Frequency') +
  theme(plot.title.position = 'panel',
        plot.title = element_text(hjust = 0.5, vjust = -10))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM