Home > Software engineering >  ggplot2 - show only lower and upper range of y axis
ggplot2 - show only lower and upper range of y axis

Time:02-25

y-axis in my geom_dotplot ranges from 0 to 1. Dots are only in the upper and lower range. I want to shrink the intermediate range with no dots (0.05 - 0.95) into the interval 0.05 and display together with the lower range between 0 - 0.05 and the upper range 0.95-1. Can anyone help, please? my code is:

ggplot(
  identical, aes(x=SNV, y=RAF, fill=Mutual_zygosity_of_parents))  
  geom_dotplot(
    binaxis = 'y', stackdir = 'center', stackratio = 0, dotsize = 0.3, show.legend = FALSE)  
  scale_fill_manual(values=c("cadetblue1"))   
  theme(legend.key=element_blank())  
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank()) 
  theme(axis.text.y = element_text(face="bold",size=16), 
        axis.title.y = element_text(face="bold",size=16))  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black"))  
  expand_limits(x= c(-1, 195))

plot

Thanks a lot Milos

CodePudding user response:

Your question is not reproducible since we don't have your data, but we can at least construct a similar data structure with the same names so we can use your plotting code to get a similar result:

set.seed(1)

identical <- data.frame(SNV = factor(sample(1:200, 400, TRUE)),
                        RAF = c(runif(200, 0, 0.02), runif(200, 0.97, 1)),
                        Mutual_zygosity_of_parents = "Yes")

p <- ggplot(
  identical, aes(x=SNV, y=RAF, fill=Mutual_zygosity_of_parents))  
  geom_dotplot(
    binaxis = 'y', stackdir = 'center', stackratio = 0, dotsize = 0.3, show.legend = FALSE)  
  scale_fill_manual(values=c("cadetblue1"))   
  theme(legend.key=element_blank())  
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank()) 
  theme(axis.text.y = element_text(face="bold",size=16), 
        axis.title.y = element_text(face="bold",size=16))  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black"))  
  expand_limits(x= c(-1, 195))

p

enter image description here

Now the problem here is that ggplot cannot do discontinuous axes, but you can come fairly close by faceting. All we need to do is set the faceting variable as RAF < 0.5, which will split the upper and lower dots into their own groups. If we also use scales = "free_y", then the y axis will "zoom in" to the range of the upper and lower dots:

p   facet_grid(RAF < 0.5~., scales = "free_y")
  theme(strip.background = element_blank(),
        strip.text = element_blank())

enter image description here

  • Related