简体   繁体   中英

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

I want to convert this stacked barplot to a circular plot in R.

在此处输入图像描述

but the script here is just for grouped bar plots (as you can see at the center of the circle: A,B,C,D).

How can I convert this stacked barplot to a circular plot?

Here is my data:

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

Thanks for any help.

You could do:

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM