R ggplot2 - 在具有不同 x 轴范围的一个图中绘制多个函数

R ggplot2 - Plot multiple functions in one plot with different x-axis ranges

我想在一个图中绘制不同的数学函数,将其中一些函数限制在不同的 x 值范围内。代码是:

# Different functions
stereographic <- function(theta,f=1) {
  2*f*tan(theta/2.0)
}

equidistant <- function(theta,f=1) {
  f*theta
}

equisolidangle <- function(theta,f=1) {
  2*f*sin(theta/2.0)
}

orthographic <- function(theta,f=1) {
  f*sin(theta)
}

# Plot
p <- ggplot(data = data.frame(x = c(0,pi)), mapping = aes(x = x))

p <- p +
  stat_function(fun=stereographic,aes(colour="Stereografisch")) +
  stat_function(fun=equidistant,aes(colour="Äquidistant")) +
  stat_function(fun=equisolidangle,aes(colour="Flächentreu")) +
  ylim(0,3)
print(p)

现在我想在[0,pi/2]的x轴范围内添加另一个函数,但我找不到工作方法。我总是得到类似 ggplot2 doesn't know how to deal with data of class uneval 的东西。是否可以将 stat_function 限制在新的 x 轴范围内或者是否有其他好的方法?

xlim in stat_function 允许您设置绘制的 x 值范围:

new.func = function(theta,f=1) {
  f/sin(theta)
}

p + stat_function(fun=new.func, aes(colour="1/sin"), xlim=c(0,pi/2))