reduce space between X-axis ticks
1
0
Entering edit mode
18 months ago

Hi I am trying to make a bubble plot for enriched pathway in R using the code given below but I wish to reduce the space between the x-axis ticks. When I use facet_grid with space = "free_y" the theme(aspect.ratio = 0.4) gives me the following error:

Error in `f()`:
! Free scales cannot be mixed with a fixed aspect ratio
Run `rlang::last_error()` to see where the error occurred.

From discussion posted here and here I understand that it is difficult to achieve but is there a way that I can edit the ggplot2 object to reduce the space between x -axis ticks

enter image description here

ggplot(df, aes(yend=Term,y = Term, xend = Category, x = Category,colour = -log10(Adjusted.P.value), fill= -log10(Adjusted.P.value), size=count))+
 geom_point()+
scale_color_viridis_c(option = "C") + 
scale_fill_viridis_c(option="C") +
ggthemes::theme_few() + 
scale_y_discrete(labels=function(x) stringr::str_wrap(x,width = 55))+
labs(x=NULL, y = NULL)+
theme(aspect.ratio = 0.4)+
facet_grid(firstannotation ~ ., scales = "free",switch = "y", space = "free")+
theme(axis.text.x=element_text(color = "black", size=10, face="bold", angle=90, hjust = 1),axis.text.y=element_text(color = "black", size=9, face="bold"),
strip.placement = "outside",strip.text.y.left = element_text(angle = 0),strip.background.y=element_rect(color = NA,  fill="grey"))

Data look like:

Term Category Overlap P.value Adjusted.P.value Old.P.value Combined.Score Genes Gene.Ratio label firstannotation
Protein metabolism Exp_20h 54/442 1.30E-07 2.38E-05 2.348932 37.24151 EIF4A1;TRAM1;RPL32;RPL31;TUSC3;GALNT15;GALNT18;RPL6;RPS4X;STS;RPL18A;RPL36;RPL35;RPLP2;RPL38;RPL37;EIF5B;ALG9;RPL21;RPS8;RPL23;RPL22;RPS3A;ALG11;B3GNT5;RPL37A;RPL24;RPL27;RPL26;GAS6;RPL28;PIGF;PFDN5;RPL12;RPL11;RPS4Y1;MAN2A1;GRPEL2;RPL13;GALNT7;SEC24B;GALNT1;C1GALT1;RPL35A;RPL23A;TBCA;RPS26;RPS25;RPS27;EIF3K;RPL27A;TOMM7;RPS20;RPS24 6 Metabolism,Protein folding, sorting and degradation,Protein modification,Translation Metabolism
Cytoplasmic ribosomal proteins Exp_20h 34/108 8.75E-17 9.62E-14 7.74482 286.36 RPL32;RPL31;RPL12;RPL11;RPS4Y1;RPL6;RPS4X;RPS6KA3;RPL18A;RPL36;RPL35;RPLP2;RPL13;RPL38;RPL37;RPL21;RPS8;RPL23;RPL22;RPL35A;RPS3A;RPL23A;RPS26;RPS25;RPS27;EIF3K;RPL27A;RPL37A;RPL24;RPL27;RPS20;RPL26;RPL28;RPS24 1 Genetic Information Processing:Translation,Genetic disease,Nucleic acid metabolism,Translation,Transport Genetic Information Processing
Translation Exp_20h 38/151 5.24E-15 1.92E-12 5.677127 186.6828 EIF4A1;TRAM1;RPL32;RPL31;RPL12;RPL11;RPS27L;RPS4Y1;RPL6;RPS4X;RPL18A;RPL36;RPL35;RPLP2;RPL13;RPL38;RPL37;EIF5B;RPL21;RPS8;RPL23;RPL22;RPL35A;RPS3A;RPL23A;RPS26;RPS25;RPS27;EIF3K;RPL27A;RPL37A;RPL24;RPL27;RPS20;RPL22L1;RPL26;RPL28;RPS24 3 Genetic Information Processing:Translation,Translation Genetic Information Processing
TGF-beta regulation of extracellular matrix Exp_2h 34/565 1.37E-10 1.67E-08 4.056584 92.13191 CXCL6;SYNM;CSF1;C1S;ELN;SLC40A1;CXCL1;PTGS2;FBLN5;ADAMTS1;CTSK;HAS2;CD36;JUNB;MAP3K5;KLF10;POSTN;VCAM1;PTGIS;PLCL1;PLA2G4C;BTBD11;INHBA;DKK1;ARL4A;COL1A2;ID2;STK17B;DDIT4;FGF18;CYP1A1;TRIB3;PRKD1;STEAP2 6 Genetic disease,Signal transduction Genetic disease
TGF-beta regulation of extracellular matrix Exp_2h 54/565 0.000168 0.020536 1.771653 15.3966 RB1;CNTNAP1;ELN;GOLIM4;SPINT2;PTPRK;SLC7A11;RPL18A;CTSK;LEPR;NRCAM;CD36;NCK1;KLF10;POSTN;IL1R1;PLA2G4C;NSMCE1;RBL2;RBP1;ANG;PDE5A;IDI1;CUL5;CEBPD;PIK3R1;PTGS2;PPL;NID2;SELENBP1;SDCBP;PDGFD;APOE;S100A10;SVIL;CDKN2B;JAG1;PLCL1;PCDHGC3;AKR1C3;PDP1;BMP4;TF;COL1A2;TSPAN13;NFIB;TNFSF4;ID2;QPCT;RAB13;CYP1A1;PIR;ACKR4;EVI2B 9 Genetic disease,Signal transduction Genetic disease
TGF-beta regulation of extracellular matrix Exp_2h 31/565 8.64E-13 1.08E-10 5.499825 152.7713 CXCL6;SYNM;CSF1;SLC40A1;STC1;TNFAIP3;CXCL1;PTGS2;CYR61;MYC;CTSK;CD36;JUNB;MAP3K5;KLF10;EDN1;POSTN;VCAM1;PLA2G4C;BTBD11;APLN;BMP4;ARL4A;COL1A2;STK17B;CYP1A1;ACKR3;TRIB3;PRKD1;STEAP2;ABCG1 5 Genetic disease,Signal transduction Genetic disease
R GSEA • 3.1k views
ADD COMMENT
0
Entering edit mode

Can you please show a head() of the data you are trying to plot?

This is really a wild assortment of mappings

aes(yend=Term,y = Term, xend = Category, x = Category,colour = -log10(Adjusted.P.value), fill= -log10(Adjusted.P.value), size=count))

and I think with a bit of preprocessing of your input data, you can simplify a lot in the actual plot.

ADD REPLY
0
Entering edit mode

Hi Matthias Zepper I have added the data above for your reference.

ADD REPLY
0
Entering edit mode
library("ggplot2")

df <- data.frame(metacategory=sample(c("A","B"),20,replace=TRUE),
                 category_a=rep(LETTERS[1:5],4),
                 category_b=rep(LETTERS[1:5],each=4),
                 value=rnbinom(20,0.4,0.2))

ggplot(df,aes(x=category_a,y=category_b,size=value)) + geom_point() + facet_grid(metacategory ~ . )

is a minimal example for the type of plot you are trying to create. Make sure, that your df has three discrete columns (as factors) that you would like to map to x,y and for the grid. Map a numeric value to size and if you like, you can also map the p-value to color or maybe to alpha (1 - p.value then of course, such that the significant ones are shown).

ADD REPLY
0
Entering edit mode

I understand that and maybe if you give a closer look to my code I used somtething similar. The problem is when I do

ggplot(df,aes(x=category_a,y=category_b,size=value)) + geom_point() + facet_grid(metacategory ~ . , scales = "free",switch = "y", space = "free")+coord_fixed()

or

ggplot(df,aes(x=category_a,y=category_b,size=value)) + geom_point() + facet_grid(metacategory ~ . , scales = "free",switch = "y", space = "free")+theme(aspect.ratio = 0.4)

I get the following errors:

error in `f()`:
! Free scales cannot be mixed with a fixed aspect ratio

or

Error in `f()`:
! coord_fixed doesn't support free scales
Run `rlang::last_error()` to see where the error occurred.

I want something like this where the x-axis ticks are equidistant and closer to each other

enter image description here

ADD REPLY
2
Entering edit mode
18 months ago

Just alter the dimensions of the output device. If you are exporting to PDF, for example, alter the values of width and height.

Kevin

ADD COMMENT
0
Entering edit mode

Yes eventually I took that path. I thought if there was a more controlled way of do it before saving the file rather than altering the width and height.

ADD REPLY

Login before adding your answer.

Traffic: 1855 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6