簡體   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