R語言數(shù)據(jù)可視化ggplot繪制置信區(qū)間與分組繪圖技巧
1. 單組情況
1)構(gòu)造數(shù)據(jù)集
x <- 1:10 y <- x^2 ci_l <- x^2 - 0.5 * x ci_r <- x^2 + 0.5 * x dat_plot <- data.frame(x, y, ci_l, ci_r)
數(shù)據(jù)集長下面這樣:
x y ci_l ci_r 1 1 1 0.5 1.5 2 2 4 3.0 5.0 3 3 9 7.5 10.5 4 4 16 14.0 18.0 5 5 25 22.5 27.5 6 6 36 33.0 39.0 7 7 49 45.5 52.5 8 8 64 60.0 68.0 9 9 81 76.5 85.5 10 10 100 95.0 105.0
2)繪制置信區(qū)間
添加置信區(qū)間的核心函數(shù)是:geom_ribbon()
,并且要注意,先畫置信區(qū)間,再繪制線條,才能保證線在置信區(qū)間的上方。
ggplot(dat_plot, aes(x = x)) + # x軸在此處添加,目的為了置信區(qū)間與擬合線共享同一個x geom_ribbon(aes(ymin = ci_l, ymax = ci_r)) + # 添加置信區(qū)間 geom_line(aes(y = y)) # 添加擬合線
(文末會對上面“丑丑”的置信區(qū)間進(jìn)行美化。)
通常情況,所需的圖片都是需要分組的,下面我們會進(jìn)行分組繪制置信區(qū)間。
2. 多組情況
我們將會演示兩種方式繪制分組情況繪制置信區(qū)間:
方法1
1)構(gòu)造數(shù)據(jù)集
通常情況下,ggplot
需要的向量化構(gòu)造:
x <- 1:10 y1 <- x^2 ci_l1 <- x^2 - 0.5 * x ci_r1 <- x^2 + 0.5 * x y2 <- 20 * log(x) ci_l2 <- 20 * log(x) - 0.5 * x ci_r2 <- 20 * log(x) + 0.5 * x dat_plot <- data.frame(rbind(cbind(x, y1, ci_l1, ci_r1), cbind(x, y2, ci_l2, ci_r2))) names(dat_plot) <- c("x", "y", "ci_l", "ci_r") dat_plot$group <- rep(c("G1", "G2"), each = 10)
數(shù)據(jù)樣式:
x y ci_l ci_r group 1 1 1.00000 0.50000 1.50000 G1 2 2 4.00000 3.00000 5.00000 G1 3 3 9.00000 7.50000 10.50000 G1 4 4 16.00000 14.00000 18.00000 G1 5 5 25.00000 22.50000 27.50000 G1 6 6 36.00000 33.00000 39.00000 G1 7 7 49.00000 45.50000 52.50000 G1 8 8 64.00000 60.00000 68.00000 G1 9 9 81.00000 76.50000 85.50000 G1 10 10 100.00000 95.00000 105.00000 G1 11 1 0.00000 -0.50000 0.50000 G2 12 2 13.86294 12.86294 14.86294 G2 13 3 21.97225 20.47225 23.47225 G2 14 4 27.72589 25.72589 29.72589 G2 15 5 32.18876 29.68876 34.68876 G2 16 6 35.83519 32.83519 38.83519 G2 17 7 38.91820 35.41820 42.41820 G2 18 8 41.58883 37.58883 45.58883 G2 19 9 43.94449 39.44449 48.44449 G2 20 10 46.05170 41.05170 51.05170 G2
2)繪制置信區(qū)間
注意,這里分組的關(guān)鍵就是使用 group =
參數(shù)。
ggplot(dat_plot, aes(x = x, group = group)) + geom_ribbon(aes(ymin = ci_l, ymax = ci_r)) + geom_line(aes(y = y))
但是這里的顏色比較吃藕,所以我們改變一下線條的顏色與置信區(qū)間的顏色。
非常簡單,我們將參數(shù) group =
用 color =
與 fill =
替換即可。值得一提的是,這里的color =
如果加在ggplot()
中,添加的就會是擬合線與置信區(qū)間外邊線兩條曲線。若不想要置信區(qū)間的外邊線, color =
寫在geom_line()
中即可。
此外,還需要注意,繪制置信區(qū)間,若線條與區(qū)間是相同顏色,一定要修改置信區(qū)間的透明度,利用alpha =
進(jìn)行修改,其范圍在0-1之間,并且值越小越透明。
代碼如下:
ggplot(dat_plot, aes(x = x, color = group, fill = group)) + geom_ribbon(aes(ymin = ci_l, ymax = ci_r), alpha = 0.3) + # alpha 修改透明度 geom_line(aes(y = y))
在大多數(shù)情況下,我們遇到的多組數(shù)據(jù)集長下面 方法2 這樣,我們需要怎么進(jìn)行繪制呢?下面繼續(xù)進(jìn)行講解:
方法2
1)構(gòu)造數(shù)據(jù)集
dat_plot <- data.frame(x, y1, ci_l1, ci_r1, y2, ci_l2, ci_r2) # 基于前文的數(shù)據(jù)
x y1 ci_l1 ci_r1 y2 ci_l2 ci_r2 1 1 1 0.5 1.5 0.00000 -0.50000 0.50000 2 2 4 3.0 5.0 13.86294 12.86294 14.86294 3 3 9 7.5 10.5 21.97225 20.47225 23.47225 4 4 16 14.0 18.0 27.72589 25.72589 29.72589 5 5 25 22.5 27.5 32.18876 29.68876 34.68876 6 6 36 33.0 39.0 35.83519 32.83519 38.83519 7 7 49 45.5 52.5 38.91820 35.41820 42.41820 8 8 64 60.0 68.0 41.58883 37.58883 45.58883 9 9 81 76.5 85.5 43.94449 39.44449 48.44449 10 10 100 95.0 105.0 46.05170 41.05170 51.05170
2)繪制置信區(qū)間
面對上述這種數(shù)據(jù)格式,我們處理起來也十分簡單,我們只需要在對應(yīng)的aes()
函數(shù)中,寫清楚對應(yīng)的分組名稱即可。
color =
與 fill =
一定要寫在 aes()
里面?。。?br />
color =
與 fill =
一定要寫在 aes()
里面?。。?br />
color =
與 fill =
一定要寫在 aes()
里面?。?!
重要的事情說三遍,具體代碼如下所示:
ggplot(dat_plot, aes(x = x)) + geom_ribbon(aes(ymin = ci_l1, ymax = ci_r1, fill = "G1"), alpha = 0.3) + geom_ribbon(aes(ymin = ci_l2, ymax = ci_r2, fill = "G2"), alpha = 0.3) + geom_line(aes(y = y1, color = "G1")) + geom_line(aes(y = y2, color = "G2"))
但這樣的置信區(qū)間還比較丑,下面我們給出一個略微美化的版本,并在代碼中進(jìn)行注釋,說明每個函數(shù)的用意。
3)美化
ggplot(dat_plot, aes(x = x)) + geom_ribbon(aes(ymin = ci_l1, ymax = ci_r1, fill = "G1", color = "G1"), alpha = 0.3, linetype = 2) + # linetype = 2 表示置信區(qū)間描邊線為虛線 geom_ribbon(aes(ymin = ci_l2, ymax = ci_r2, fill = "G2", color = "G2"), alpha = 0.3, linetype = 2) + geom_line(aes(y = y1, color = "G1")) + geom_line(aes(y = y2, color = "G2")) + theme_bw(base_family = "Times") + theme(panel.grid = element_blank(), legend.position = "top", # legend 置頂 panel.border = element_blank(), text = element_text(family = "STHeiti"), # Mac 系統(tǒng)中中文繪圖 plot.title = element_text(hjust = 0.5)) + # 標(biāo)題居中 labs(x = "y", y = "x", title = "分組置信區(qū)間", color = "", fill = "") # 將置信區(qū)間與擬合線的 legend 合并,并且不要 legend 的小標(biāo)題
想學(xué)習(xí)更多 ggplot 美化繪圖的技巧,可以參考下述鏈接:
R語言學(xué)習(xí)ggplot2繪制統(tǒng)計圖形包全面詳解
以上就是R語言數(shù)據(jù)可視化ggplot繪制置信區(qū)間與分組繪圖技巧的詳細(xì)內(nèi)容,更多關(guān)于ggplot繪制置信區(qū)間與分組繪圖的資料請關(guān)注腳本之家其它相關(guān)文章!
- R語言ggplot2圖例修改超詳細(xì)介紹
- R語言中g(shù)gplot2繪制雙坐標(biāo)軸圖
- R語言ggplot2設(shè)置圖例(legend)的操作大全
- R語言ggplot2圖例標(biāo)簽、標(biāo)題、順序修改和刪除操作實例
- R語言ggplot2拼圖包patchwork安裝使用
- R語言包ggplot實現(xiàn)分面去掉小標(biāo)題的灰色底色小技巧
- R語言學(xué)習(xí)ggplot2繪制統(tǒng)計圖形包全面詳解
- R語言使用ggplot繪制畫中畫細(xì)節(jié)放大的方法
- R語言ggplot2之圖例的設(shè)置
- R語言ggplot2包之坐標(biāo)軸詳解
- R語言ggplot在熱圖上標(biāo)注相關(guān)系數(shù)的操作方法