Letin
Letin

Reputation: 1275

how to make the curve/points of the ggplot start from 0 line?

I have a small question regarding the ggplot, where in I want to know how to get the data points start with a 0 line without leaving that small gap on the left, right and the bottom.

This is my code:

hov.dat <- structure(list(x = c(3L, 3L, 9L, 25L, 25L, 27L, 30L, 39L, 49L, 
56L, 60L, 65L), y = c(55, 54, 34.33, 34, 75.66, 44, 56.55, 54, 
27.34, 30.75, 19.04, 25.29)), .Names = c("x", "y"), class = "data.frame", row.names = c(NA, 
-12L))

with(hov.dat, plot(x, y))

qplot(x, y, data=hov.dat, geom=c('point', 'smooth'), method='lm', formula=y ~ ns(x, 3))

enter image description here

can anyone help me with what am I supposed to code to remove the left, right, and bottom gaps in the plot (marked with arrows in the picture)

Upvotes: 2

Views: 2734

Answers (1)

ziggystar
ziggystar

Reputation: 28680

You have to supply the expand argument to scale_x_continuous (same for y-axis):

qplot(data=d,x,y,geom=c("point","smooth"),method="lm") + 
  scale_y_continuous(expand=c(0,0)) + 
  scale_x_continuous(expand=c(0,0))

I could not execute function ns, so I didn't use your formula.

enter image description here

expand: a numeric vector of length two giving multiplicative and additive expansion constants. These constants ensure that the data is placed some distance away from the axes.

Note that you can also extend the interpolation of your smoothing function, like in the following plot, which IMHO looks nicer. See this question on CV:

gplot(hov.dat,aes(x=x,y=y)) + geom_point() + stat_smooth(method="lm",fullrange=T) + scale_x_continuous(limits=c(-5,70),expand=c(0,0))

enter image description here

Upvotes: 1

Related Questions