为什么注释会影响每个容器中的密度数字?

时间:2018-08-31 00:49:24

标签: r ggplot2

这里的问题是,由于某种原因,当我对ggplot使用注释时,放置在每个bin中的数字会更改。我不确定为什么,但我需要找出答案。我尚未发布数据,因为您看到此问题的唯一方法是,如果您拥有整个数据集,那么他的数据集太大了。

在After图形中,我圈出了一些变化的数字。

之前的代码:

ggplot(gb, aes(x = Y*100, y = Y1*100, fill = typeoft)) + 
  geom_bin2d(bins = 10, aes(alpha = ..count..)) +
  scale_fill_manual(values = c("black","red")) + 
  geom_text_repel(bins = 10,stat = "bin2d",
                  aes(label = round(100*..density..,1)),
                  size = 2,direction = "y") +
  facet_wrap(~type, nrow = 1) +
  geom_abline(slope=1, intercept=0) +
  scale_alpha_continuous(range = c(.05,1)) +
  theme(panel.background = element_rect(fill = "white")) + 

之后的代码:

ggplot(gb, aes(x = Y*100, y = Y1*100, fill = typeoft)) + 
  geom_bin2d(bins = 10, aes(alpha = ..count..)) +
  scale_fill_manual(values = c("black","red")) + 
  geom_text(bins = 10,stat = "bin2d",
            aes(label = round(100*..density..,1)),
            size = 2,check_overlap = TRUE) +
  facet_wrap(~type, nrow = 1) +
  geom_abline(slope=1, intercept=0) +
  scale_alpha_continuous(range = c(.05,1)) +
  theme(panel.background = element_rect(fill = "white")) + 
  theme(legend.position = "none") + 
  annotate(geom = "text",label = c(.1,0,.3),x = -.87,y=-.75,size = 2) +
  annotate(geom = "text",label = c(.2,.1,1),x = -.29,y=-.18,size = 2)

1 个答案:

答案 0 :(得分:1)

要使合并具有可复制性,您需要将每个绘图的轴范围显式设置为相同(请参见下面的scale_*_continuous元素)。为了使geom_text_repel的标签可重复,在生成每个图时,需要设置相同的种子(用于在标签位置产生随机移位)。可以通过seed中的geom_text_repel参数来完成。

library(tidyverse)
library(ggrepel)
library(grid.arrange)

# Read data
gb = "https://gist.githubusercontent.com/nwlezien/49ac446d3f924b2ab70e10b442883ade/raw/0769320b71c507884bfeb28a0639f3cfbacf31f1/data.csv"
gb = read_csv(dat)

# Function to generate plot components
pfnc = function(bins, title) {
  list(geom_bin2d(bins = bins, aes(alpha = ..count..)),
       scale_fill_manual(values = c("black","red")), 
       geom_text(bins = bins, stat = "bin2d", seed=2,
                       aes(label = round(100*..density..,1)),
                       size = 2, direction = "y"),
       facet_wrap(~type, nrow = 1),
       geom_abline(slope=1, intercept=0),
       scale_alpha_continuous(range = c(.5,1)),
       theme(panel.background = element_rect(fill = "white")), 
       theme(legend.position = "none"),
       labs(title=title),
       scale_x_continuous(limits=range(gb$Y*100) + c(-0.15,0.15)*diff(range(gb$Y*100)), expand=c(0,0)),
       scale_y_continuous(limits=range(gb$Y1*100) + c(-0.15,0.15)*diff(range(gb$Y1*100)), expand=c(0,0))
       )
}

p1 = ggplot(gb, aes(x = Y*100, y = Y1*100, fill = typeoft)) + 
  pfnc(10, "original plot")

p2 = ggplot(gb, aes(x = Y*100, y = Y1*100, fill = typeoft)) + 
  pfnc(10, "with annotations") +
  annotate(geom = "text",label = c(.1,0,.3),x = -.87,y=-.75,size = 4, colour="lightblue") +
  annotate(geom = "text",label = c(.2,.1,1),x = -.29,y=-.18,size = 4, colour="lightblue")

grid.arrange(p1, p2, ncol=1)

enter image description here

使用每个容器中心的标签可以更容易地解释这些图,如果将geom_text_repel更改为geom_text,则可以得到这些图:

enter image description here