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

R 为什么注释会影响每个箱子中的密度数?,r,ggplot2,R,Ggplot2,这里的问题是,当我使用带有ggplot的annotate时,由于某种原因,我放在每个箱子中的数字都会发生变化。我不知道为什么,但我需要弄清楚。我没有发布数据,因为你看到这个问题的唯一方式是如果你有整个数据集,这太大了 在后面的图表中,我圈出了几个发生变化的数字 代码之前: ggplot(gb, aes(x = Y*100, y = Y1*100, fill = typeoft)) + geom_bin2d(bins = 10, aes(alpha = ..count..)) + sca

这里的问题是,当我使用带有ggplot的annotate时,由于某种原因,我放在每个箱子中的数字都会发生变化。我不知道为什么,但我需要弄清楚。我没有发布数据,因为你看到这个问题的唯一方式是如果你有整个数据集,这太大了

在后面的图表中,我圈出了几个发生变化的数字

代码之前:

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)

要使装箱可复制,您需要为每个绘图明确地设置相同的轴范围(请参见下面的
比例*.\u连续
元素)。要使带有
geom_text_repel
的标签可复制,您需要在生成每个绘图时设置相同的种子(用于在标签位置产生随机位移)。这可以通过
geom\u text\u repel
中的
seed
参数完成

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)

如果将
geom_text_repel
更改为
geom_text
,则在每个箱子的中心放置标签,这些图将更容易解释:


我查看了您的链接,没有找到代码中使用的变量名。你确定这是正确的文件吗?我已经更改了问题中的名称。谢谢你让我知道。有什么想法吗@Z.LinIt似乎
注释
更改了x轴的范围。虽然垃圾箱的数量相同,但垃圾箱的边界发生了变化。有没有办法解决这个问题?范围没有改变吗?我没有任何解决办法。此外,我也无法复制您显示的情节。很抱歉,我再次编辑了代码。我意识到我忘了包括*100,所以Y和Y1的每个值都应该乘以100。现在检查代码。