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

问题描述 投票:0回答:1

这里的问题是,当我使用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)
r ggplot2
1个回答
1
投票

要使binning可重现,您需要明确地为每个绘图设置轴范围相同(请参阅下面的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

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

enter image description here


推荐问答