R語言編程繪圖箱線圖基本實(shí)例
基本箱線圖繪制
使用ggplot2繪制箱線圖的核心函數(shù)是geom_boxplot()。以下是一個(gè)基礎(chǔ)示例,展示如何用iris數(shù)據(jù)集繪制不同物種(Species)的萼片長(zhǎng)度(Sepal.Length)分布:
library(ggplot2) ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_boxplot()
顏色與填充控制
通過fill和color參數(shù)可分別控制箱線圖內(nèi)部填充色和邊框顏色:
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) + geom_boxplot(color = "black", alpha = 0.7)
alpha參數(shù)調(diào)整透明度(0-1)- 顏色支持Hex格式(如
#FF5733)或R顏色名稱
異常值樣式調(diào)整
箱線圖的異常值(outliers)可通過以下參數(shù)定制:
geom_boxplot( outlier.color = "red", # 異常點(diǎn)顏色 outlier.shape = 19, # 點(diǎn)形狀編號(hào) outlier.size = 3, # 點(diǎn)大小 outlier.alpha = 0.6 # 透明度 )
寬度與位置調(diào)整
width參數(shù)控制箱體寬度,position調(diào)整分組位置:
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) + geom_boxplot(width = 0.5, position = position_dodge(0.8))
分組箱線圖
當(dāng)需要按兩個(gè)分類變量分組時(shí),使用交互變量或分面:
# 方法1:dodge分組 ggplot(mpg, aes(x = class, y = hwy, fill = factor(cyl))) + geom_boxplot(position = position_dodge(preserve = "single")) # 方法2:分面 ggplot(mpg, aes(x = class, y = hwy)) + geom_boxplot() + facet_wrap(~cyl)
統(tǒng)計(jì)信息顯示
可通過stat_summary()疊加顯示均值等統(tǒng)計(jì)量:
ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_boxplot() + stat_summary(fun = mean, geom = "point", shape = 18, size = 3, color = "red")
水平箱線圖
交換x/y映射即可創(chuàng)建水平箱線圖:
ggplot(iris, aes(y = Species, x = Sepal.Length)) + geom_boxplot()

完整參數(shù)列表
geom_boxplot()支持的完整美學(xué)參數(shù)(aesthetics)包括:
x:分類變量(必需)y:連續(xù)變量(必需)lower/upper:自定義箱體范圍middle:自定義中位數(shù)線ymin/ymax:自定義須線范圍group:強(qiáng)制分組變量weight:加權(quán)箱線圖
主題定制
通過theme()函數(shù)可精細(xì)調(diào)整標(biāo)題、坐標(biāo)軸等元素:
ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_boxplot() + labs(title = "鳶尾花萼片長(zhǎng)度分布") + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1))
# 加載必要的庫(kù)
library(ggplot2)
# 創(chuàng)建示例數(shù)據(jù)
df <- data.frame(
group = rep(c("A", "B", "C"), each = 100),
value = c(rnorm(150, mean = 0), rnorm(60, mean = 1), rnorm(400, mean = 2))
)
# 繪制箱線圖
p <- ggplot(df, aes(x = group, y = value)) +
geom_boxplot(width = 0.6, fill = "white", color = "black") + # 使用白色填充,黑色邊框
labs(title = "Boxplot of Values by Group", # 標(biāo)題
x = "Group", # X軸標(biāo)簽
y = "Value") + # Y軸標(biāo)簽
theme_minimal() + # 使用簡(jiǎn)潔主題
theme(plot.title = element_text(size = 16, face = "bold", hjust = 0.5), # 標(biāo)題樣式
axis.title = element_text(size = 14, face = "bold"), # 軸標(biāo)題樣式
axis.text = element_text(size = 12), # 軸刻度標(biāo)簽樣式
legend.position = "none",
axis.line = element_line(color = "black")
)
# 顯示圖像
print(p)
# 保存為高分辨率圖像
ggsave("boxplot.png", plot = p, width = 8, height = 6, dpi = 300)
到此這篇關(guān)于R語言編程繪圖-箱線圖的文章就介紹到這了,更多相關(guān)R語言箱線圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
R包c(diǎn)lusterProfiler如何安裝成功(新手必看!)
最近在我以為ClusterProfiler已經(jīng)安裝好的時(shí)候,又遇到了一些問題,所以這篇文章主要給大家介紹了關(guān)于R包c(diǎn)lusterProfiler如何安裝成功的相關(guān)資料,需要的朋友可以參考下2023-02-02
R語言數(shù)據(jù)可視化ggplot添加左右y軸繪制天貓雙十一銷售圖
本篇文章主要介紹如何在R中,使用ggplot2包在一個(gè)圖像上添加左右兩個(gè) y 軸刻度,并在同一個(gè)圖像上繪制兩個(gè)完全不一樣的統(tǒng)計(jì)圖,有需要的朋友可以借鑒參考下2021-11-11

