新聞中心
散點(diǎn)圖顯示在笛卡爾平面中繪制的許多點(diǎn)。 每個(gè)點(diǎn)表示兩個(gè)變量的值。 在水平軸上選擇一個(gè)變量,在垂直軸上選擇另一個(gè)變量。
使用plot()函數(shù)創(chuàng)建簡(jiǎn)單散點(diǎn)圖。
語(yǔ)法
在r語(yǔ)言中創(chuàng)建散點(diǎn)圖的基本語(yǔ)法是 -
plot(x, y, main, xlab, ylab, xlim, ylim, axes)
以下是所使用的參數(shù)的描述 -
-
x是其值為水平坐標(biāo)的數(shù)據(jù)集。
-
y是其值是垂直坐標(biāo)的數(shù)據(jù)集。
-
main要是圖形的圖塊。
-
xlab是水平軸上的標(biāo)簽。
-
ylab是垂直軸上的標(biāo)簽。
-
xlim是用于繪圖的x的值的極限。
-
ylim是用于繪圖的y的值的極限。
-
axes指示是否應(yīng)在繪圖上繪制兩個(gè)軸。
例
我們使用r語(yǔ)言環(huán)境中可用的數(shù)據(jù)集“mtcars”來(lái)創(chuàng)建基本散點(diǎn)圖。 讓我們使用mtcars中的“wt”和“mpg”列。
input <- mtcars[,c('wt','mpg')]
print(head(input))
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
wt mpg Mazda RX4 2.620 21.0 Mazda RX4 Wag 2.875 21.0 Datsun 710 2.320 22.8 Hornet 4 Drive 3.215 21.4 Hornet Sportabout 3.440 18.7 Valiant 3.460 18.1
創(chuàng)建散點(diǎn)圖
以下腳本將為wt(重量)和mpg(英里/加侖)之間的關(guān)系創(chuàng)建一個(gè)散點(diǎn)圖。
# Get the input values.
input <- mtcars[,c('wt','mpg')]
# Give the chart file a name.
png(file = "scatterplot.png")
# Plot the chart for cars with weight between 2.5 to 5 and mileage between 15 and 30.
plot(x = input$wt,y = input$mpg,
xlab = "Weight",
ylab = "Milage",
xlim = c(2.5,5),
ylim = c(15,30),
main = "Weight vs Milage"
)
# Save the file.
dev.off()
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
散點(diǎn)圖矩陣
當(dāng)我們有兩個(gè)以上的變量,我們想找到一個(gè)變量和其余變量之間的相關(guān)性,我們使用散點(diǎn)圖矩陣。 我們使用pairs()函數(shù)創(chuàng)建散點(diǎn)圖的矩陣。
語(yǔ)法
在R中創(chuàng)建散點(diǎn)圖矩陣的基本語(yǔ)法是 -
pairs(formula, data)
以下是所使用的參數(shù)的描述 -
-
formula表示成對(duì)使用的一系列變量。
-
data表示將從其獲取變量的數(shù)據(jù)集。
例
每個(gè)變量與每個(gè)剩余變量配對(duì)。 為每對(duì)繪制散點(diǎn)圖。
# Give the chart file a name. png(file = "scatterplot_matrices.png") # Plot the matrices between 4 variables giving 12 plots. # One variable with 3 others and total 4 variables. pairs(~wt+mpg+disp+cyl,data = mtcars, main = "Scatterplot Matrix") # Save the file. dev.off()
當(dāng)執(zhí)行上面的代碼中,我們得到以下輸出。
文章題目:創(chuàng)新互聯(lián)R語(yǔ)言教程:R語(yǔ)言散點(diǎn)圖
轉(zhuǎn)載來(lái)于:http://www.dlmjj.cn/article/coshpii.html


咨詢(xún)
建站咨詢(xún)
