繁体   English   中英

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)

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

xlim中的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))

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM