ggplot2饼图与geom_text_repel [英] ggplot2 pie plot with geom_text_repel

查看:1340
本文介绍了ggplot2饼图与geom_text_repel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用ggplot2绘制多个饼图,并成功将标签绘制在正确的位置,如下所示:

I am plotting multiple pie plots with ggplot2 and succeeded in having the labels plotted in the 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)

然后我想利用ggrepel,以使小片的数字不重叠:

I then wanted to make use of ggrepel so that the small slices numbers do 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 I get the following warning "Warning: Ignoring unknown parameters: position"

并弄乱了标签.

任何人都知道如何将标签的正确位置与geom_text_repel或其他方法结合起来?

Anyone knows how to combine the right positioning of the labels with geom_text_repel, or any alternative?

谢谢!

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

推荐答案

首先,您需要创建一个正确的列,并在coord_polar()之前加上标签的位置.我会用'dplyr'做到的,但是您可以使用任何您喜欢的方式:

First, you need to make a correct column with position of your labels before coord_polar(). I would do it with 'dplyr' but you can use whatever you comfortable with:

library('dplyr')

df <- df %>% 
  arrange(treatment, desc(annotation)) %>% 
  group_by(treatment) %>% 
  mutate(text_y = cumsum(value) - value/2)

df

# A 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

现在,当我们将text_y用作geom_text()y美感时,文本和标签将位于列的中间.

Now text and labels will be in the middle of a column when we will use text_y as a y aesthetic for geom_text().

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)

现在添加label_repel并转换坐标:

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

这篇关于ggplot2饼图与geom_text_repel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆