r - ggplot2 pie plot with geom_text_repel -
i plotting multiple pie plots ggplot2 , succeeded in having labels plotted in right positions, as:
df <- data.frame(annotation=rep(c("promoter", "intergenic", "intragene", "5prime", "3prime"), 3), value=c(69.5, 16, 10.7, 2.5, 1.3, 57.2, 18.8, 20.2, 2.1, 1.7, 50.2, 32.2, 15.3, 1.2, 1.1), treatment=rep(c("treated1", "treated2", "untreated"), c(5, 5, 5))) library(ggplot2) ggplot(data = df, aes(x = "", y = value, fill = annotation)) + geom_bar(stat = "identity") + geom_text(aes(label = value), position = position_stack(vjust = 0.5)) + coord_polar(theta = "y") + facet_grid(.~treatment) i wanted make use of ggrepel small slices numbers not overlap:
library(ggrepel) ggplot(data = df, aes(x = "", y = value, fill = annotation)) + geom_bar(stat = "identity") + geom_text_repel(aes(label = value), position = position_stack(vjust = 0.5)) + coord_polar(theta = "y") + facet_grid(.~treatment) but following warning "warning: ignoring unknown parameters: position"
and messed labels.
anyone knows how combine right positioning of labels geom_text_repel, or alternative?
thank you!
sessioninfo() r version 3.3.2 (2016-10-31) platform: x86_64-redhat-linux-gnu (64-bit) running under: scientific linux 7.2 (nitrogen) locale: [1] lc_ctype=en_us.utf-8 lc_numeric=c [3] lc_time=en_us.utf-8 lc_collate=en_us.utf-8 [5] lc_monetary=en_us.utf-8 lc_messages=en_us.utf-8 [7] lc_paper=en_us.utf-8 lc_name=c [9] lc_address=c lc_telephone=c [11] lc_measurement=en_us.utf-8 lc_identification=c attached base packages: [1] stats graphics grdevices utils datasets methods base other attached packages: [1] ggrepel_0.6.5 ggplot2_2.2.1 limma_3.26.9 loaded via namespace (and not attached): [1] rcpp_0.12.8 digest_0.6.12 grid_3.3.2 plyr_1.8.4 [5] gtable_0.2.0 magrittr_1.5 scales_0.4.1 stringi_1.1.5 [9] reshape2_1.4.2 lazyeval_0.2.0 labeling_0.3 tools_3.3.2 [13] stringr_1.2.0 munsell_0.4.3 colorspace_1.3-2 tibble_1.3.0
first, need make correct column position of labels before coord_polar(). 'dplyr' can use whatever comfortable with:
library('dplyr') df <- df %>% arrange(treatment, desc(annotation)) %>% group_by(treatment) %>% mutate(text_y = cumsum(value) - value/2) df # tibble: 15 x 4 # groups: treatment [3] annotation value treatment text_y <chr> <dbl> <chr> <dbl> 1 promoter 69.5 treated1 34.75 2 intragene 10.7 treated1 74.85 3 intergenic 16.0 treated1 88.20 4 5prime 2.5 treated1 97.45 5 3prime 1.3 treated1 99.35 6 promoter 57.2 treated2 28.60 7 intragene 20.2 treated2 67.30 8 intergenic 18.8 treated2 86.80 9 5prime 2.1 treated2 97.25 10 3prime 1.7 treated2 99.15 11 promoter 50.2 untreated 25.10 12 intragene 15.3 untreated 57.85 13 intergenic 32.2 untreated 81.60 14 5prime 1.2 untreated 98.30 15 3prime 1.1 untreated 99.45
now text , labels in middle of column when use text_y y aesthetic geom_text().
plot prior coord_polar():
ggplot(data = df, aes(x = "", y = value, fill = annotation)) + geom_bar(stat = "identity") + geom_label(aes(label = value, y = text_y)) + facet_grid(.~treatment) and adding label_repel , transforming coordinates:
ggplot(data = df, aes(x = "", y = value, fill = annotation)) + geom_bar(stat = "identity") + geom_label_repel(aes(label = value, y = text_y)) + facet_grid(. ~ treatment) + coord_polar(theta = "y") 

Comments
Post a Comment