一、通過添加關鍵字參數來自定義圖例
在ggplot2中,可以直接通過添加關鍵字參數來自定義圖例,例如添加圖例標題、修改圖例位置等等。具體代碼如下:
library(ggplot2)
data(mpg)
ggplot(mpg, aes(x = displ, y = hwy, color = factor(cyl))) +
geom_point() +
labs(title = "Displacement vs Highway Miles per Gallon",
subtitle = "Grouped by Number of Cylinders",
x = "Engine Displacement (L)",
y = "Highway Miles per Gallon",
color = "Number of\nCylinders") + # 修改圖例名稱
theme(legend.position = "bottom") # 修改圖例位置
在這個例子中,我們使用了ggplot2自帶的mpg數據集,以車輛排量(displ)和公路里程數(hwy)為X軸和Y軸,按照車輛缸數(cyl)進行顏色分組。圖例名稱修改為「Number of Cylinders」,並且將圖例位置修改為「bottom」。
二、通過scale函數來給不同的圖層添加圖例
有時候我們需要通過不同的圖層來顯示數據的不同維度,這些圖層對應的圖例也需要分別顯示。可以通過ggplot2的scale函數來實現這一目的。例如,我們需要在一張圖中同時顯示氣缸數(cyl)和汽車類型(class)兩個維度。具體代碼如下:
library(ggplot2)
data(mpg)
ggplot(mpg, aes(x = displ, y = hwy, color = factor(cyl))) + # 第一層圖層
geom_point() +
geom_smooth(method = "lm", se = FALSE, aes(color = factor(class))) + # 第二層圖層
labs(title = "Displacement vs Highway Miles per Gallon",
subtitle = "Grouped by Number of Cylinders and Car Type",
x = "Engine Displacement (L)",
y = "Highway Miles per Gallon") +
scale_color_discrete(name = "Number of Cylinders") + # 添加第一層圖層的圖例
scale_color_manual(name = "Car Type", values = c("grey40", "#0072B2", "#E69F00", "#009E73", "#F0E442", "#D55E00", "#CC79A7")) # 添加第二層圖層的圖例
在這個例子中,我們除了按照車輛缸數(cyl)對點進行了顏色分組外,還添加了一層擬合直線,按照車輛類型(class)進行顏色分組,並且針對這兩個圖層分別添加了圖例。
三、通過不同的幾何對象來展示數據並添加相應的圖例
在ggplot2中,有不同的幾何對象可以用來展示數據。例如,我們可以通過geom_point繪製散點圖,通過geom_line繪製折線圖,通過geom_histogram繪製直方圖等等。不同的幾何對象需要添加不同類型的圖例。具體代碼如下:
library(ggplot2)
library(dplyr)
data(mpg)
mpg %>%
group_by(origin, cyl) %>%
summarise(avg_hwy = mean(hwy)) %>%
ggplot(aes(x = factor(cyl), y = avg_hwy, group = origin,
color = origin, fill = origin, linetype = factor(cyl))) +
geom_line(size = 1.2) + # 添加折線圖並調整線條粗細
geom_point(size = 3.5, shape = 21, stroke = 0.2, aes(fill = origin)) + # 添加散點圖並調整點的大小、形狀和填充顏色
scale_color_manual(values = c("red", "blue", "green")) + # 自定義線條顏色
scale_fill_manual(values = c("#FF0000FF", "#0000FFFF", "#008000FF")) + # 自定義填充顏色
scale_linetype_manual(values = c(1, 2, 3, 4, 5, 6)) + # 自定義線條類型
labs(title = "Average Highway Miles per Gallon by Origin and Cylinder Number",
x = "Number of Cylinders",
y = "Average Highway Miles per Gallon",
color = "Country of\nOrigin",
fill = "Country of\nOrigin",
linetype = "Number of\nCylinders") +
theme(legend.position = "bottom") # 修改圖例位置
在這個例子中,我們以車輛缸數(cyl)和公路里程數(hwy)為X軸和Y軸,按照車輛生產地(origin)進行線條顏色、填充顏色、線條類型的分組,並且添加散點圖進行數據的展示。我們需要按照不同的幾何對象分別添加不同類型的圖例。
四、通過內置函數來修改圖例
ggplot2還提供了一些方便的內置函數來修改圖例。例如,我們可以通過guides函數來修改圖例的標籤文字,添加圖例的標題等等。具體代碼如下:
library(ggplot2)
data(mpg)
ggplot(mpg, aes(x = displ, y = hwy, color = factor(cyl))) +
geom_point() +
labs(title = "Displacement vs Highway Miles per Gallon",
subtitle = "Grouped by Number of Cylinders",
x = "Engine Displacement (L)",
y = "Highway Miles per Gallon",
color = "Number of\nCylinders") +
guides(
color = guide_legend(title = "Cylinder Number") # 修改圖例標題
) +
theme(legend.position = "bottom") # 修改圖例位置
在這個例子中,我們在原始圖例的基礎上添加了一個標題,並且通過guides函數修改了圖例標題的文字。
原創文章,作者:SJBSP,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/370075.html