ggplot层与另一层干扰

时间:2021-04-12 23:44:58

I'm plotting a big plot, and for some reason some of the data disappears.

我正在策划一个大的情节,由于某种原因,一些数据消失了。

I played a little and found out that if I remove other layers, the data appears again. any idea way this is happening?

我玩了一点,发现如果我删除其他图层,数据会再次出现。这是怎么回事?

The plot, missing some of the data

情节,遗漏了一些数据

ggplot层与另一层干扰

The plot with the data after I removed the other layers (green and black lines)

删除其他图层(绿色和黑色线条)后的数据图

ggplot层与另一层干扰

This is the relevant code- (I would have put a reproducible example, if I knew how, I'm not sure what is causing it, and how to reproduce it)

这是相关的代码 - (如果我知道如何,我会提出一个可重现的例子,我不确定是什么导致它,以及如何重现它)

main_plot <- ggplot(seg) + facet_grid(id~chr, scales='free_x')

# KNN
# remove KNN rows that don't contain anything (after faceting)
seg <- seg[!(grepl('KNN', seg$id, fixed = TRUE) & seg$log2 == 0),]
 # check that there are KNN line before ploting
if(sum(grepl("KNN", seg$id,fixed=TRUE))) {
  main_plot <- main_plot + 
    geom_segment(data=seg[grepl("KNN",seg$id,fixed=TRUE),],
                 mapping=aes(x=start,xend=end,y=0,yend=0,size=3,color=((log2>0) - 0.5)*6))
}
# scatter
###main_plot <- main_plot + geom_segment(data=seg[grepl("hits",seg$id, fixed = TRUE),],
###                                    mapping=aes(x=start,xend=end,y=log2,yend=log2, size = 2),color='chartreuse3')
# median with smothing
###    if(sum(median_data_with_smoothing$median_color == 'red') > 0) {
###main_plot <- main_plot + geom_segment(data=median_data_with_smoothing[median_data_with_smoothing$median_color == 'red',], 
###                                     mapping=aes(x=start, xend=end, y=median, yend=median,size=2), color='red')
###}
###if(sum(median_data_with_smoothing$median_color == 'cyan') > 0) {
###main_plot <- main_plot + geom_segment(data=median_data_with_smoothing[median_data_with_smoothing$median_color == 'cyan',], 
###                                     mapping=aes(x=start, xend=end, y=median, yend=median,size=2), color='cyan')
###}
###if(sum(median_data_with_smoothing$median_color == 'black') > 0) {
###main_plot <- main_plot + geom_segment(data=median_data_with_smoothing[median_data_with_smoothing$median_color == 'black',], 
###                                        mapping=aes(x=start, xend=end, y=median, yend=median,size=2), color='black')
###}
# adding more layers
main_plot <- main_plot + 
  scale_color_gradient2(limits=c(-3,3),low="cyan",mid="gray60",high="red",na.value="deeppink",midpoint=0) +
  theme(panel.background=element_rect(fill="white")) +
  theme(legend.position = "none") +
  theme(strip.text.x = element_text(size = 12)) +
  theme(strip.text.y = element_text(size = 12)) +
  theme(panel.spacing=unit(2, "points")) +
  ylab("") + 
  ylim(c(-2,2))

The parts of the code that have ### are the layers I removed and the data appeared.

具有###的代码部分是我删除的层并且数据出现。

The data is from a 2 very big data.tables, (300K lines) so I can't upload it somehow.

数据来自2个非常大的data.tables,(300K行)所以我不能以某种方式上传它。

1 个解决方案

#1


1  

The problem is that I plot all the segments in the same Y coordinate (0), and ggplot treats the segments like discrete values, or something similar.

问题是我将所有段绘制在相同的Y坐标(0)中,而ggplot将段视为离散值或类似的东西。

I added some noise to the data with jitter() and then I could see all the data in the plot.

我用抖动()向数据添加了一些噪声,然后我可以看到图中的所有数据。

Here is my code-

这是我的代码 -

if(sum(grepl("KNN", seg$id,fixed=TRUE))) {
  main_plot <- main_plot + 
    geom_segment(data=seg[grepl("KNN",seg$id,fixed=TRUE),],
                 mapping=aes(x=start,xend=end,y=jitter(log2/100, amount=0.00001),yend=jitter(log2/100,amount=0.00001),size=3,color=((log2>0) - 0.5)*6))
}

The values of the log2 are 1 and -1, so I divided it by 100 to get close enough to 0, and added noise in much smaller scale so it will not be visible.

log2的值是1和-1,所以我将它除以100得到足够接近0,并且以更小的比例添加噪声,因此它将不可见。

The reason that when I removed some of the data (scatter and median) I saw all the KNN data is because the scale changed (the scales depends on the data that you plot)

当我删除一些数据(散射和中位数)时,我看到所有KNN数据的原因是因为比例发生了变化(比例取决于您绘制的数据)

#1


1  

The problem is that I plot all the segments in the same Y coordinate (0), and ggplot treats the segments like discrete values, or something similar.

问题是我将所有段绘制在相同的Y坐标(0)中,而ggplot将段视为离散值或类似的东西。

I added some noise to the data with jitter() and then I could see all the data in the plot.

我用抖动()向数据添加了一些噪声,然后我可以看到图中的所有数据。

Here is my code-

这是我的代码 -

if(sum(grepl("KNN", seg$id,fixed=TRUE))) {
  main_plot <- main_plot + 
    geom_segment(data=seg[grepl("KNN",seg$id,fixed=TRUE),],
                 mapping=aes(x=start,xend=end,y=jitter(log2/100, amount=0.00001),yend=jitter(log2/100,amount=0.00001),size=3,color=((log2>0) - 0.5)*6))
}

The values of the log2 are 1 and -1, so I divided it by 100 to get close enough to 0, and added noise in much smaller scale so it will not be visible.

log2的值是1和-1,所以我将它除以100得到足够接近0,并且以更小的比例添加噪声,因此它将不可见。

The reason that when I removed some of the data (scatter and median) I saw all the KNN data is because the scale changed (the scales depends on the data that you plot)

当我删除一些数据(散射和中位数)时,我看到所有KNN数据的原因是因为比例发生了变化(比例取决于您绘制的数据)