5

When doing a conventional plot I could use xlim and ylim to show ranges of what I wanted plotted.

How can I achieve this in ggplot?

EDIT: Example of a dataset I plot:

real <- read.table("http://pelinfamily.ca/bio/GDR-18_conc.ld", header=F)
dd <- data.frame(Distance=real[,2]-real[,1], r.2=real[,13])

ggplot(dd, aes(x=Distance, y=r.2)) +
   stat_summary(fun.data="mean_sdl", mult=1, geom="ribbon", alpha=.4) + 
   stat_summary(fun.data="mean_sdl", mult=1, geom="line")
tonytonov
  • 25,060
  • 16
  • 82
  • 98
AdrianP.
  • 435
  • 2
  • 4
  • 14

2 Answers2

9

Since you are doing stat_summary you might consider..

+ coord_cartesian(xlim=c(-10, 10), ylim=c(-10, 10))

Which will merely "zoom in" without changing the underlying data.

ggplot2 docs

jenswirf
  • 7,087
  • 11
  • 45
  • 65
6

This post actually answers it pretty well: How to set limits for axes in ggplot2 R plots?

I found:

library(scales); 
... + scale_x_continuous(limits = c(-5000, 5000)

to work.

... + xlim(-5000, 5000)

Did not work for some reason.

Community
  • 1
  • 1
AdrianP.
  • 435
  • 2
  • 4
  • 14