Cut Y axis in ggplot2

Dear All,
I'm trying to cut unnecessary part of y axis from my ggplot2 please see code below:
snp_rs9303277<-as.factor(age16_RV_SNP_Rawdata$rs9303277_C)
levels(snp_rs9303277)<-c("TT","CT","CC")
p1<-ggplot(aes(x=as.factor(rs9303277_C),y=IFN_beta_RV1B),data = age16_RV_SNP_Rawdata)+theme(axis.text.y=element_text(size = 12),axis.text.x = element_text(size = 12),axis.title = element_text(size = 16))+geom_boxplot(outlier.size = -1)+xlab("rs9303277_C")+ylab("pg/ml")+ ggtitle("RV1B IFN-beta age16")+
scale_x_discrete(labels=c("0" = "TT", "1" = "CT", "2" = "CC"))
A1<-p1+stat_compare_means(comparisons = my_comperisons)+ scale_y_continuous(breaks=c(0,10,20,35,40), labels=c(0,10,20,35,40))+
geom_jitter(position = position_jitter(0.15),aes(color=snp_rs9303277))
A1

I'm trying to remove y scale from 20 to 35 pg/ml, but instead I achieved to just put the breaks were I needed them. please see attached. any ideas how can I remove unwanted part of Y axis?

Rather than cutting out part of the y axis, which would make the plot hard to interpret, could you move the mean comparisons. Compare the two plots below.

library(ggplot2)
library(ggpubr)
#> Warning: package 'ggpubr' was built under R version 3.5.3
#> Loading required package: magrittr

Rawdat <- data.frame(C = rep(c("X", "Y", "Z"), each = 31),
                     beta = c(c(rnorm(30, 1,1), 15), rnorm(31, 2,1.5), rnorm(31, 2.5, 1)))
p1<-ggplot(data = Rawdat, aes(x = C, y= beta)) + geom_boxplot()  

p1 + stat_compare_means(comparisons = list(c("X", "Y"), c("Y", "Z"), c("X", "Z"))) 

  

p1 + stat_compare_means(comparisons = list(c("X", "Y"), c("Y", "Z"), c("X", "Z")),
                        label.y = c(7.5, 10, 12.5)) 

Created on 2019-10-12 by the reprex package (v0.3.0.9000)

2 Likes

Dear FJCC,

Thank you for your reply, I really like the idea!

I applied it to my code, but unfortunately it doesn't work.

my_comperisons<-list(c("0","1"),c("1","2"),c("0","2"))

snp_rs9303277<-as.factor(age16_RV_SNP_Rawdata$rs9303277_C)
levels(snp_rs9303277)<-c("TT","CT","CC")

p1<ggplot(aes(x=as.factor(rs9303277_C),y=IFN_beta_RV1B),data=age16_RV_SNP_Rawdata)+theme(axis.text.y=element_text(size = 12),axis.text.x = element_text(size = 12),axis.title = element_text(size = 16))+geom_boxplot(outlier.size = -1)+xlab("rs9303277_C")+ylab("pg/ml")+ ggtitle("RV1B IFN-beta age16")+
scale_x_discrete(labels=c("0" = "TT", "1" = "CT", "2" = "CC"))

A1<-p1+stat_compare_means(comparisons = my_comperisons,lebel.y = c(22, 25, 35))+
geom_jitter(position = position_jitter(0.15),aes(color=snp_rs9303277))
A1

It doesn't give me an error message either. I'm sorry but I don't know how to use reprex packed to make my codes sharable.

Thank you for your time and I will keep trying to sort it out.
Kind Regards,
Eteri

I probably need to understand your data better to figure out what is going on.

Before that, I see a typo in your code. Notice that you wrote "lebel" instead of "label" in the following:

A1 <- p1 + stat_compare_means(comparisons = my_comperisons, lebel.y = c(22, 25, 35)) + 
geom_jitter(position = position_jitter(0.15), aes(color = snp_rs9303277))

I would expect that to cause an error so I assume your actual code did not have that problem. If the problem continues after fixing the typo, please show the result of these two commands.

ggplot(aes(x = rs9303277_C, y = IFN_beta_RV1B), data=age16_RV_SNP_Rawdata) +
  geom_boxplot()
str(age16_RV_SNP_Rawdata)
1 Like

Yes, It was a problem with the typo!

It's working now!

Thank you very much!

For future reference, here is one way of creating some common grounds for finding a solution:

# Load libraries
# ------------------------------------------------------------------------------
library('tidyverse')

# Create example data
# ------------------------------------------------------------------------------
set.seed(28816)
n = 1000
d = tibble(snp_rs9303277 = factor(sample(x = c('TT', 'CT', 'CC'),
                                         size = n, replace = TRUE),
                                  levels = c('TT', 'CT', 'CC')),
           dosis = rnorm(n = n, mean = 0, sd = 16)^2/100)

# Visualise data
# ------------------------------------------------------------------------------
d %>%
  ggplot(aes(x = snp_rs9303277,
             y = dosis)) +
  geom_boxplot(outlier.size = -1) +
  geom_jitter(aes(colour = snp_rs9303277)) +
  ggtitle(label = expression("RV1B IFN-" * beta * " age16"))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.