ggplot2如何添加图例

发布时间:2023-05-23

一、通过添加关键字参数来自定义图例

在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函数修改了图例标题的文字。