1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| library(RColorBrewer) library(ggplot2) blank_theme <- theme_minimal()+ theme( axis.title.x = element_blank(), axis.text.x=element_blank(), axis.title.y = element_blank(), axis.text.y=element_blank(), panel.border = element_blank(), panel.grid=element_blank(), axis.ticks = element_blank(), plot.title=element_text(size=14, face="bold",hjust = 0.5) ) f_pie <- function(lc_x, lc_main){ lc_cols <- brewer.pal(length(lc_x), "Paired") lc_v <- as.vector(100*lc_x) lc_percent = sprintf('%0.2f%%',lc_v) lc_df <- data.frame(type = names(lc_x), nums = lc_v) lc_df$pos <- with(lc_df, 100-cumsum(nums)+nums/2) # print(lc_df) lc_pie <- ggplot(data = lc_df, mapping = aes(x = 1, y = nums, fill = type)) + geom_bar(stat = 'identity') lc_pie <- lc_pie + coord_polar("y", start=0, direction = 1) + scale_fill_manual(values=lc_cols) + blank_theme lc_pie <- lc_pie + geom_text(aes(x = 1.3, y=pos,label= lc_percent)) lc_pie <- lc_pie + labs(title = lc_main) lc_pie } options(repr.plot.width=6, repr.plot.height=6) options(ggrepel.max.overlaps = Inf) f_pie(prop.table(table(Idents(scRNA))), "Proportion of each brain region")
|