# Changing the axis limits of ggplot objects

By EnhancedVolcano R package I have this plot with this code

    > head(res1)
baseMean log2FoldChange     lfcSE        stat     pvalue      padj
SAMD11   237.7975     0.03584334 1.3244737  0.02706233 0.97841002 0.9971691
NOC2L   4751.0381    -0.62090622 0.6500115 -0.95522347 0.33946470 0.9239759
KLHL17   735.0849    -0.95126291 0.8417444 -1.13010892 0.25843033 0.8926461
PLEKHN1 1117.5598    -2.11321359 1.0411108 -2.02976816 0.04238011 0.6350143
PERM1    409.0061    -2.32444927 1.1255813 -2.06511000 0.03891259 0.6285916
HES4    1088.7549     0.28395931 0.9091259  0.31234320 0.75477971 0.9866881
>
p= EnhancedVolcano(res1,
lab = rownames(res1),
x = 'log2FoldChange',
y = 'pvalue',
xlim = c(-5, 8))


I have stored my plot in an object

> class(p)
[1] "gg"     "ggplot"


I want to limit the axis length in this plot in the other word shrining my plot more but I don't know how to do that. For example y axis be from 0 to 10 instead of 20

• Could you clarify what do you man by "limit the axis length"? Do you want it for instance to reach just from -4 to 4 or do you want a wider plot? What kind of searches did you make? Also if you could include a reproducible example, see reprex, it would be easier for people to understand and work with your problem
– llrs
Sep 19, 2019 at 12:11

Axes for ggplots can be controlled via xlim() and ylim() functions. You can try:

your_ggplot_object +
ylim(-5,20) # assuming that you want the gene labels to be readabe, not clear from your post

• > p1 + + xlim() + + ylim(-5,20) Error in UseMethod("limits") : no applicable method for 'limits' applied to an object of class "NULL" > Sep 19, 2019 at 12:31
• I have realized that the function EnhancedVolcano has a "ylim" argument and its lower limit is 0, please try "ylim = c(-5, max(-log10(toptable[,y]), na.rm=TRUE) + 5)" within EnhancedVolcano.
– haci
Sep 19, 2019 at 12:40
• Sorry what is the y here? when I am placing pvalue, says that Error in [.data.frame(res_responders, , pvalue) : object 'pvalue' not found Sep 19, 2019 at 12:52
• Please check the documentation of the function EnhancedVolcano() and play a little bit different arguments. For you case ssetting the ylim argument "ylim = (-5,20)" might help.
– haci
Sep 19, 2019 at 13:24