2014-09-29 154 views
39
函數曲線

什麼是繪製簡單的曲線像如何繪製R中

eq = function(x){x*x} 
R中

功能選擇呢?

這聽起來這樣一個明顯的問題,但我只找到計算器這些相關的問題,但他們都更具體

我希望我沒有寫出重複的問題。

回答

5

您的意思是這樣的?

> eq = function(x){x*x} 
> plot(eq(1:1000), type='l') 

Plot of eq over range 1:1000

(或任何值的範圍是有關您的功能)

20

plotplot.function方法

plot(eq, 1, 1000) 

或者

curve(eq, 1, 1000) 
+1

有趣的是,我沒有看到你的例子'plot(eq,1,1000)'在其他地方。我還看到了'curve(eq,1,100)'例子。有區別嗎? – sjdh 2014-09-29 01:33:52

+3

@sjdh不多。在做一些參數檢查後,'plot.function'實際上調用'curve'。另外,'curve'可以將一個表達式作爲輸入,但'plot'需要一個函數作爲輸入來分派到'plot.function' – GSee 2014-09-29 01:41:53

41

我做了一些搜索網站上,這有一些方法,我發現:

使用曲線的最簡單方法,而不預先定義的功能

curve(x^2, from=1, to=50, , xlab="x", ylab="y") 

enter image description here

您也可以使用曲線時你有一個predfined功能

eq = function(x){x*x} 
curve(eq, from=1, to=50, xlab="x", ylab="y") 

enter image description here

如果你想使用ggplot,你有qplot之間的choise

library("ggplot2") 
eq = function(x){x*x} 
qplot(c(1,50), fun=eq, stat="function", geom="line", xlab="x", ylab="y") 

enter image description here

和ggplot

library("ggplot2") 
eq = function(x){x*x} 
ggplot(data.frame(x=c(1, 50)), aes(x=x)) + stat_function(fun=eq, geom="line") + xlab("x") + ylab("y") 

enter image description here

1

這裏是一個格子版本:

library(lattice) 
eq<-function(x) {x*x} 
X<-1:1000 
xyplot(eq(X)~X,type="l") 

Lattice output

1

與我需要額外的設置萊迪思的解決方案:

library(lattice) 
distribution<-function(x) {2^(-x*2)} 
X<-seq(0,10,0.00001) 
xyplot(distribution(X)~X,type="l", col = rgb(red = 255, green = 90, blue = 0, maxColorValue = 255), cex.lab = 3.5, cex.axis = 3.5, lwd=2) 
  1. 如果您需要的x值的範圍繪製在1,例如不同的增量0.00001您可以使用:

X < -seq(0,10,0。00001)

  • 您可以通過定義一個RGB值變更線的顏色:
  • COL = RGB(紅= 255,綠色= 90 ,藍色= 0,maxColorValue = 255)

  • 可以通過設置改變繪製線條的寬度:
  • LWD = 2

  • 可以通過縮放他們改變標籤的大小:
  • cex.lab = 3.5,CEX。軸= 3.5

    Example plot