Skip to Content
R 錦囊妙計
book

R 錦囊妙計

by Paul Teetor
January 2014
Beginner to intermediate
488 pages
8h 1m
Chinese
GoTop Information, Inc.
Content preview from R 錦囊妙計
276
|
第十章
在於,使用
plot
函數繪製第一條線之後,接著使用
lines
函數,繪製後續的其他直線。
如下所示,使用
lines
函數可添加直線至既有圖形上,而且允許設定不同的樣式、寬
度,和顏色:
> plot(x, y.democr, type="l", col="blue")
> lines(x, y.republ, col="red")
> lines(x, y.indeps, col="yellow")
其中,首先使用
plot
函數初始化圖形視窗,並將第一條線設為藍色。接著使用
lines
數繪製另外兩條直線,分別設定為紅色與黃色。
延伸資訊
錦囊 10.12 關於繪製基本直線圖。
10.14 繪製多個資料集
問題點
您想將多個資料集的繪圖結果呈現於一張圖上。
解決方案
使用高階圖形函數(如:
plot
curve
)初始化圖形,然後使用低階圖形函數(如:
lines
points
)來添加額外的資料集。
當初始化圖形時,需注意畫布(canvas)的正確尺寸。使用
xlim
ylim
兩個參數,設
定足夠的畫布空間來容納所有資料集,而不只是第一個資料集。以下程式碼片段顯示如
何使用
range
函數計算範圍,並設定
xlim
ylim
參數。它假設您有兩個資料集,一個
x1
y1
代表;另一個則由
x2
y2
代表:
> xlim <- range(c(x1,x2))
> ylim <- range(c(y1,y2))
> plot(x1, y1, type="l", xlim=xlim, ylim=ylim)
> lines(x2, y2, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python数据科学

Python数据科学

肯尼迪·贝曼, 诺亚·吉夫特
Java数据科学实战

Java数据科学实战

Michael R. Brzustowicz, PhD

Publisher Resources

ISBN: 9789862769829