John Goldin
unread,Feb 2, 2013, 11:25:34 AM2/2/13Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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.