嗨,我正在策划像这样的函数:
eq <-function(x) ifelse(x<(-1)|x>1|(x>(-1)& x<1),(x/((x^2)-1)),NA)
eq2 <-function(x) ifelse(x<(-sqrt(2))|x>sqrt(2)|x>(-sqrt(2))&x<sqrt(2),(x/((x^2)-2)), NA)
eq3 <-function(x) ifelse(x<(-sqrt(3))|x>sqrt(3)|x>(-sqrt(3))&x<sqrt(3),(x/((x^2)-3)), NA)
library(ggplot2)
ggplot(data.frame(x=c(-3,3)), aes(x))+
stat_function(fun=eq,geom="line", aes(colour="x/(x^2-1)"))+
stat_function(fun=eq2,geom='line',aes(color='x/(x^2-2)')) +
stat_function(fun=eq3,geom='line', aes(color ='x/(x^2-3)'))+
scale_x_continuous(breaks = (seq(-3, 3, by = 0.5)))+ ylim(-3,3)这创造了一个情节,但线条似乎是随机停止的。我希望他们继续到情节的边缘。我相当肯定,我的代码相当于这个问题的答案,Plotting Noncontinuous Function ggplot2。有什么建议吗?
发布于 2016-09-26 17:33:52
ylim(-3,3)正在使超出该范围的数据值从绘图中移除(scale_y_continuous也会发生这种情况)。使用coord_cartesian设置限制,而不删除这些限制之外的数据:
ylim(-10,10) +
coord_cartesian(ylim=c(-3,3))在上面的代码中,我还设置了一个ylim范围,但是一个比图的范围限制大得多的范围。这是为了避免将函数值吹到Inf或-Inf的垂直行。
https://stackoverflow.com/questions/39708877
复制相似问题