T.G.
T.G.

Reputation: 793

ggplot layers interfere one with the other

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

The plot, missing some of the data

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

The plot with the data

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.

Upvotes: 0

Views: 91

Answers (1)

T.G.
T.G.

Reputation: 793

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.

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.

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)

Upvotes: 1

Related Questions