Reputation: 10969
I'm using ggplot2
and attempting to create an empty plot with some basic dimensions, like I might do w/ the stock plot
function like so:
plot(x = c(0, 10), y=c(-7, 7))
Then I'd plot the points with geom_point()
(or, stock point()
function)
How can I set that basic plot up using ggplot? I'm only able to draw a plot using like:
ggplot() + layer(data=data, mapping = aes(x=side, y=height), geom = "point")
But this has max x/y values based on the data.
Upvotes: 2
Views: 1567
Reputation: 173677
You can set the overall plotting region limits using xlim
and ylim
:
ggplot(data = data) +
geom_point(aes(x = side, y = height) +
xlim(c(0,10)) +
ylim(c(-7,7))
Also see coord_cartesian
which zooms in and out rather than hard coding the axis limits.
Edit Since @Brian clarified the differences between his answer and mine well, I thought I should mention it as well in my answer, so no one misses it. Using xlim
and ylim
will set the limits of the plotting region no matter what data you add in subsequent layers. Brian's method using expand_limits
is a way to set the minimum ranges.
Upvotes: 1
Reputation: 58845
There are two ways to approach this:
Basically the same approach as with base graphics; the first layer put down has the limits you want, using geom_blank()
ggplot() +
geom_blank(data=data.frame(x=c(0,10),y=c(-7,7)), mapping=aes(x=x,y=y))
Using expand_limits()
ggplot() +
expand_limits(x=c(0,10), y=c(-7,7))
In both cases, if your data extends beyond this, the axes will be further expanded.
Upvotes: 3