making range of stat_function different than base plot

873 views
Skip to first unread message

John Goldin

unread,
Feb 2, 2013, 11:25:34 AM2/2/13
to ggp...@googlegroups.com
I am trying to add a function to a quantile regression line to a scatter plot.  The confidence interval for the line is wide at the extremes in part because of sparse data so I want to restrict the range of the line so that it does not cover the full range of the data on the x axis.  That's the motivation, but the example below is just a simple modification of an example from stat_function help.  I am unable to control the range displayed by the function in way I would have expected.

# plot f contains a red lline from 0 to 10
f <- ggplot(data.frame(x = c(0, 10)), aes(x)) + stat_function(fun = sin, colour = "red")

# now try to add a blue line on the range of 2 to 8
f + stat_function(fun = cos, colour = "blue", x = list(x = c(2, 8)))
# both lines cover from 0 to 10, which is not what I want.  I want the red to be 0 to 10 and the blue to be 2 to 8

# now I am going to flail about trying to do the same thing a different way
f + stat_function(fun = cos, colour = "blue", aes(x = list(x = c(2, 8))))
# both lines still cover from 0 to 10

# Next I'll try to replace the data in stat_function
f + stat_function(fun = cos, colour = "blue", data = data.frame(x = c(2, 8)))
# still doesn't work.

# And finally, here is a plot that shows that if I have only one stat_function call I can get it to override the data in ggplot.
ggplot(data.frame(x = c(0, 10)), aes(x)) + stat_function(fun = cos, colour = "blue", data = data.frame(x = c(2, 8)))
# in this last example, I do get a blue line from 2 to 8 after changing the data to the function, but only because the overall
# limit to the base plot gets changed from 2 to 8, which is not what I want.

It seems to me that this is a bug in stat_function, but generally I assume that ggplot2 is smarter than I am and that I am missing something.

Meanwhile I am going to try to work around this problem by changing the function I am passing to stat_function so that it returns NA's in the part of the range where I don't want it to appear.

John Goldin

unread,
Feb 2, 2013, 5:01:40 PM2/2/13
to ggp...@googlegroups.com
My work-around was a success, but it turned out to be quite ugly.  I changed the function that I passed to stat_function so that it returned NA where I didn't want the function to appear. But by trial and error I discovered that it had to return a value for the first value of x.  Otherwise I got the following error:
Error in unit(x, default.units) : 'x' and 'units' must have length > 0
So I put in NAs at the beginning of the list of return values, but changed y[1] to a numeric value rather than NA.

Dennis Murphy

unread,
Feb 2, 2013, 5:11:15 PM2/2/13
to John Goldin, ggp...@googlegroups.com
I had no such problem:

f <- ggplot(data.frame(x = c(0, 10)), aes(x)) + stat_function(fun =
sin, colour = "red")
f
g <- function(x) ifelse(x >= 2 & x <= 8, cos(x), NA)
f + stat_function(fun = g, color = "blue")

Warning message:
Removed 40 rows containing missing values (geom_path).

This 'works' because the function with the wider range was plotted
first. Was that the case in your work-around?

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Reply all
Reply to author
Forward
0 new messages