社交網路分析的 R 基礎:(六)繪圖操作

張高興發表於2022-02-12

R 語言強大的視覺化功能在科學研究中非常受歡迎,豐富的類庫使得 R 語言可以繪製各種各樣的圖表。當然這些與本章內容毫無關係?,因為筆者對繪製圖表瞭解有限,僅限於能用的程度。接下來的內容無需額外安裝任何包,僅使用 R 語言自帶的繪圖工具完成柱狀圖與折線圖的繪製。如果對繪製的圖表定製性要求較高,請搜尋 ggplot2 包的相關教程。

柱狀圖

R 語言中使用 barplot() 函式來建立柱狀圖,下面繪製一個最簡單的柱狀圖:

> data1 <- c(0.7795875, 0.8686484, 0.8901365, 0.905844, 0.9201746, 0.9227028)
> barplot(data1)

image

為了使圖表的可讀性更高,還需要新增標題、x 與 y 軸的副標題:

> xArgs <- c("1%", "2%", "3%", "4%", "5%", "6%")
> barplot(data1, main = "Plot Title", names.arg = xArgs, xlab = "X-axis Subtitle", ylab = "Y-axis Subtitle")

image

現在需要為現有的柱狀圖新增一組對比資料,並且灰色有些單調,還需要為柱狀圖新增一些鮮豔的顏色:

> data2 <- c(0.7921935, 0.8775983, 0.8999872, 0.9109487, 0.9250147, 0.9274716)  # 第二組資料
> colors <- rainbow(2, 0.6, 0.9)  # 生成2個彩虹中的顏色並設定一下飽和度
> barplot(rbind(data1, data2), main = "Plot Title", names.arg = xArgs, xlab = "X-axis Subtitle", ylab = "Y-axis Subtitle", beside = TRUE, col = colors)

image

現在的柱狀圖變得稍微美觀了一些,但兩組資料的對比似乎還缺少圖例:

> legends <- c("Data1", "Data2")
> b <- barplot(rbind(data1, data2), main = "Plot Title", names.arg = xArgs, xlab = "X-axis Subtitle", ylab = "Y-axis Subtitle", beside = TRUE, col = colors)
> legend("bottom", legend = legends, fill = colors, horiz = TRUE, bg = "white")  # 圖例

image

這個柱狀圖仍然有些不太滿意,需要限制一下 y 軸的範圍在 [0.75, 1],還要為柱狀圖新增數字顯示,如果能加上網格就更好了:

> b <- barplot(rbind(data1, data2), main = "Plot Title", names.arg = xArgs, xlab = "X-axis Subtitle", ylab = "Y-axis Subtitle", beside = TRUE, col = colors, ylim = c(0.75, 1), xpd = FALSE)
> legend("bottom", legend = legends, fill = colors, horiz = TRUE, bg = "white")  # 圖例
> text(b, y = rbind(data1, data2) + 0.01, labels = as.character(round(rbind(data1, data2), 3)), cex = 0.75)  # 柱狀圖文字描述
> grid(nx = 0, ny = 5, col = "lightgray")  # 網格

image

“你是來找茬的吧!”,將柱狀圖改成虛線填充:

> angles <- c(60, 120)  # 虛線填充的角度
> b <- barplot(rbind(data1, data2), main = "Plot Title", names.arg = xArgs, xlab = "X-axis Subtitle", ylab = "Y-axis Subtitle", beside = TRUE, col = colors, ylim = c(0.75, 1), xpd = FALSE, angle = angles, density = 15)
> # 上文出現過的程式碼此處省略...

image

這樣一個不能說是最好看的,但一定是一個功能最全的柱狀圖就完成了,請根據需求自由組合柱狀圖的外觀。

折線圖

此處折線圖就不像柱狀圖一樣一步步畫了,圖表的外觀是通用的,柱狀圖中提到的外觀都可以直接拿來給折線圖用。下面繪製一個兩條線的折線圖:

> data1 <- c(0.8457699, 0.9294758, 0.9550087, 0.9640443, 0.969838, 0.9750423)
> data2 <- c(0.7892351, 0.8938469, 0.9202865, 0.9603516, 0.9590848, 0.9745516)  # 兩組資料
> xArgs <- c("1%", "2%", "3%", "4%", "5%", "6%")  # x 軸
> legends <- c("Data1", "Data2")  # 圖例
> colors <- rainbow(2, 0.6, 0.9)  # 顏色
> ltys <- c(1, 2)  # 線條型別
> pchs <- c(12, 13)  # 點的符號
> plot(data1, main = "Plot Title", xlab = "X-axis Subtitle", ylab = "Y-axis Subtitle", 
col = colors[1], lty = ltys[1], pch = pchs[1], cex = 1.2, lwd = 2, type = "o", ylim = c(0.75, 1), xaxt = "n")
> axis(1, at = 1:6, labels = xArgs)  # x 軸
> lines(data2, type="b", cex = 1.2, lwd = 2, lty = ltys[2], col = colors[2], pch = pchs[2])  # 新增第二條線
> legend("bottomright", legend = legends, col =  colors, pch = pchs, horiz = FALSE)  # 圖例

image

線條型別和點的符號見下圖:

image

下面繪製一個雙 y 軸的折線圖:

> data1 <- c(1, 0.93, 0.87, 0.82, 0.87, 0.98)
> data2 <- c(14.3736, 14.5011, 12.9268, 11.4347, 10.0557, 8.6953)
> xArgs <- c("1%", "2%", "3%", "4%", "5%", "6%")
> par(mar = c(5, 5, 4, 5))  # 設定圖表的外邊距,以便有足夠的空間顯示副標題
> plot(data1, main = "Plot Title", xlab = "X-axis Subtitle", ylab = "Y1-axis Subtitle", type = "o", col = "blue", pch = 15, axes = FALSE)  # 繪製第一條線
> axis(side = 2)  # 繪製第一條 y 軸
> axis(1, at = 1:6, labels = xArgs, cex.axis = 0.9)  # 繪製 x 軸
> par(new = TRUE)
> plot(data2, type = "o", xaxt = "n", yaxt = "n", ylab = "", xlab = "", col = "red", lty = 2, pch = 16, axes = FALSE)  # 繪製第二條線
> axis(side = 4)
> mtext("Y2-axis Subtitle", side = 4, line = 3)  # 繪製第二條 y 軸
> box()  # 將圖表封閉

image

儲存繪製的圖表

儲存為 pdf 檔案:

pdf(file = "plot.pdf")
plot()  # 此處繪圖
dev.off()

儲存為 png 檔案:

png(file = "plot.png")
plot()  # 此處繪圖
dev.off()

相關文章