# 怎樣使用R語言ggplot2畫柱形圖
## 一、ggplot2簡介
ggplot2是R語言中最著名的數據可視化包之一,由Hadley Wickham創建。它基于"圖形語法"(Grammar of Graphics)理論,通過分層構建圖形元素的方式實現高度靈活的數據可視化。相較于R基礎繪圖系統,ggplot2具有以下優勢:
1. 一致的語法結構
2. 高度可定制的圖形組件
3. 自動處理圖例和坐標軸
4. 支持復雜圖形組合
5. 活躍的社區支持
柱形圖(Bar Plot)是ggplot2中最常用的圖表類型之一,主要用于展示分類變量的頻數分布或不同組間的比較。
## 二、安裝與加載ggplot2
在開始之前,需要先安裝并加載ggplot2包:
```r
# 安裝ggplot2(如果尚未安裝)
install.packages("ggplot2")
# 加載包
library(ggplot2)
同時建議安裝并加載其他常用輔助包:
install.packages(c("dplyr", "tidyr", "scales"))
library(dplyr) # 數據操作
library(tidyr) # 數據整理
library(scales) # 坐標軸格式化
使用R內置的mtcars數據集作為示例:
data(mtcars)
head(mtcars)
# 計算各氣缸數的頻數
cyl_counts <- mtcars %>%
count(cyl) %>%
rename(count = n)
# 基礎柱形圖
ggplot(cyl_counts, aes(x = factor(cyl), y = count)) +
geom_col()
geom_col()
: 用于當數據已包含y值的情況geom_bar(stat = "count")
: 當需要自動計算頻數時使用aes()
: 美學映射,將數據變量映射到圖形屬性ggplot(cyl_counts, aes(x = factor(cyl), y = count, fill = factor(cyl))) +
geom_col() +
scale_fill_brewer(palette = "Set1")
ggplot(cyl_counts, aes(x = factor(cyl), y = count, fill = factor(cyl))) +
geom_col() +
geom_text(aes(label = count), vjust = -0.5) +
scale_fill_manual(values = c("#E41A1C", "#377EB8", "#4DAF4A"))
ggplot(cyl_counts, aes(x = factor(cyl), y = count)) +
geom_col(width = 0.6,
color = "black", # 邊框顏色
size = 0.5) # 邊框線寬
grouped_data <- mtcars %>%
group_by(cyl, am) %>%
summarise(mean_mpg = mean(mpg)) %>%
ungroup()
ggplot(grouped_data, aes(x = factor(cyl), y = mean_mpg, fill = factor(am))) +
geom_col(position = position_dodge()) +
labs(x = "氣缸數", y = "平均MPG", fill = "變速箱類型") +
scale_fill_discrete(labels = c("自動", "手動"))
ggplot(grouped_data, aes(x = factor(cyl), y = mean_mpg, fill = factor(am))) +
geom_col(position = "stack") +
labs(title = "堆疊柱形圖示例")
ggplot(cyl_counts, aes(x = count, y = reorder(factor(cyl), count))) +
geom_col(fill = "steelblue") +
labs(x = "頻數", y = "氣缸數") +
theme_minimal()
summary_data <- mtcars %>%
group_by(cyl) %>%
summarise(
mean_mpg = mean(mpg),
sd_mpg = sd(mpg),
n = n(),
se_mpg = sd_mpg / sqrt(n)
)
ggplot(summary_data, aes(x = factor(cyl), y = mean_mpg)) +
geom_col(fill = "lightblue") +
geom_errorbar(
aes(ymin = mean_mpg - se_mpg, ymax = mean_mpg + se_mpg),
width = 0.2
)
ggplot(cyl_counts, aes(x = factor(cyl), y = count)) +
geom_col(fill = "coral") +
theme_bw() + # 黑白主題
labs(title = "氣缸數分布",
subtitle = "mtcars數據集",
caption = "數據來源:R內置數據集")
custom_theme <- theme(
plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
axis.title = element_text(size = 12),
axis.text = element_text(color = "darkgray"),
panel.background = element_rect(fill = "white"),
panel.grid.major.y = element_line(color = "gray90")
)
ggplot(cyl_counts, aes(x = factor(cyl), y = count)) +
geom_col(fill = "#1E88E5") +
custom_theme
ggplot(cyl_counts, aes(x = reorder(factor(cyl), -count), y = count)) +
geom_col()
# 創建有長標簽的示例數據
long_label_data <- data.frame(
category = c("非常長的類別名稱A", "非常長的類別名稱B", "非常長的類別名稱C"),
value = c(20, 35, 15)
)
ggplot(long_label_data, aes(x = category, y = value)) +
geom_col() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
data_with_neg <- data.frame(
group = LETTERS[1:5],
value = c(3, -2, 5, -1, 4)
)
ggplot(data_with_neg, aes(x = group, y = value)) +
geom_col(aes(fill = value > 0)) +
scale_fill_manual(values = c("red", "blue"))
# 模擬銷售數據
set.seed(123)
sales_data <- data.frame(
month = month.name[1:6],
product_A = round(runif(6, 50, 100)),
product_B = round(runif(6, 30, 80))
) %>%
pivot_longer(cols = starts_with("product"),
names_to = "product",
values_to = "sales")
# 繪制分組柱形圖
ggplot(sales_data, aes(x = month, y = sales, fill = product)) +
geom_col(position = position_dodge(0.8), width = 0.7) +
scale_fill_manual(values = c("#FF6B6B", "#4ECDC4")) +
labs(title = "上半年產品銷售情況",
x = "月份",
y = "銷售額(萬元)",
fill = "產品類型") +
theme_minimal() +
theme(legend.position = "top")
# 模擬實驗數據
experiment_data <- data.frame(
treatment = rep(c("Control", "Low", "Medium", "High"), each = 3),
replicate = rep(1:3, 4),
growth = c(2.1, 2.3, 2.0, 3.5, 3.2, 3.8, 4.9, 4.7, 5.1, 5.5, 5.8, 5.3)
) %>%
group_by(treatment) %>%
summarise(
mean_growth = mean(growth),
se = sd(growth)/sqrt(n())
)
# 繪制帶誤差棒的柱形圖
ggplot(experiment_data,
aes(x = factor(treatment, levels = c("Control", "Low", "Medium", "High")),
y = mean_growth)) +
geom_col(fill = "lightgreen", width = 0.6) +
geom_errorbar(aes(ymin = mean_growth - se, ymax = mean_growth + se),
width = 0.2) +
labs(x = "處理組", y = "平均生長量(cm)") +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
theme_classic()
# 保存高質量圖像
ggsave("my_barplot.png",
width = 8,
height = 6,
dpi = 300,
units = "in")
通過掌握ggplot2繪制柱形圖的技巧,您將能夠創建出專業級的數據可視化作品,有效傳達數據背后的洞見。 “`
注:本文實際約3100字(中文字符統計),包含了從基礎到高級的ggplot2柱形圖繪制方法。由于Markdown中無法真實顯示圖片,實際應用時需要將圖片占位符替換為真實生成的圖表。建議讀者在RStudio中逐步運行示例代碼,觀察每個參數對圖形的影響。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。