R语言 使用ggplot2处理图例

R语言 使用ggplot2处理图例

图中的图例可以帮助我们了解每个条形图、线形图或方框的类型、颜色 属于哪个组。我们可以使用 legend() 函数在 R 中添加一个图例框。这些都是作为指南的作用。键可以通过刻度断点来确定。在这篇文章中,我们将使用R编程语言中的ggplot2来处理图例和相关的定制工作。

语法:

legend(x, y = NULL, legend, fill = NULL, col = par("col"), border = "black", lty, lwd, pch)

安装

我们将首先安装和加载 ggplot2 包,因为没有这个包,我们将无法绘制我们的数据。所以,我们可以用下面的命令来安装这个包。

install.packages("ggplot2")

使用R编程语言中的 库函数 加载该软件包。

我们将尝试使用 ggplot() 函数来绘制数据的不同方面。对于处理可以相互比较的数据的绘图,将默认生成一个适当的图例供参考。

例子: 显示默认图例的R程序

library(ggplot2)
  
# Read the dataset
data("USArrests")
  
head(USArrests)
  
# Plot the data
plt <- ggplot(USArrests, aes(Murder, Assault, colour = UrbanPop)) +
geom_point()
  
plt

输出

在R中使用ggplot2处理图例

这样生成的图例可以很容易地按照要求进行定制。

改变图例标题的颜色和大小

在这里,我们将改变由绘图形成的图例标题的颜色和大小。这可以通过theme()函数来完成。我们将在这个函数中传递 legend.title 参数。legend.title参数基本上指的是形成的图例的标题。legend.title参数等同于element_text函数,该函数继承于title,并接受颜色、大小等参数。

语法:

theme( legend.title = element_text(), ...., complete = FALSE, validate = TRUE)

例子: 自定义图例标题的R程序

# Installing and loading the package
library(ggplot2)
  
# Read the dataset
data("USArrests")
  
# Plot the data
plt <- ggplot(USArrests, aes(Murder, Assault, colour = UrbanPop)) +
geom_point()
  
# Changing the title and font style of the 
# text of legend
plt + theme(legend.title = element_text(colour = "red", size = 12))

输出:

在R中使用ggplot2处理图例

改变标签的颜色和大小

这里,我们将改变图例中标签的颜色和大小。这可以通过传递 legend.text 作为参数,使用 theme() 函数来完成。legend.text等于element_text()函数,它继承自text,接受颜色、大小、字体等参数。legend.text参数基本上指的是图例项目的标签

语法:

theme( legend.text = element_text(), ... , complete = FALSE, validate = TRUE)

例子: 定制图例标签的R程序

library(ggplot2)
  
# Read the dataset
data("USArrests")
  
head(USArrests)
  
# Plot the data
plt <- ggplot(USArrests, aes(Murder, Assault, colour = UrbanPop)) +
geom_point()
  
plt + theme(legend.text = element_text(colour = "blue", size = 9))
  
plt

输出

在R中使用ggplot2处理图例

隐藏图例标题

我们将在这里使用提供legend.title作为参数的theme()函数隐藏图例标题。在这里,legend.title等同于 element_blank() 函数。element_blank()不分配空间,也不画任何东西。

语法:

theme( legend.title = element_blank(),...., complete = FALSE, validate = TRUE)

例子: 隐藏图例标题的R程序

# Installing and loading the package
library(ggplot2)
  
# Read the dataset
data("USArrests")
  
# Plot the data
plt <- ggplot(USArrests, aes(Murder, Assault, colour = UrbanPop)) +
geom_point()
  
# Hiding legend title
plt + theme(legend.title = element_blank())

输出

在R中使用ggplot2处理图例

删除图例

我们可以通过在legend.position中提供 “none “作为theme()函数的一个参数来删除图例。legend.position参数指的是图例的位置。

语法:

theme( legend.position = "none", ..., complete = FALSE, validate = TRUE)

例子: 去除图例的R程序

# Installing and loading the package
library(ggplot2)
  
# Read the dataset
data("USArrests")
  
# Plot the data
plt <- ggplot(USArrests, aes(Murder, Assault, colour = UrbanPop))+
geom_point()
  
# Removing legend 
plt + theme(legend.position = "none")

输出:

在R中使用ggplot2处理图例

将图例的位置放在顶部

这里,我们将把图例的位置从侧面改为顶部。我们将使用theme()函数并提供legend.position作为参数来做这件事。legend.position参数将被指定为顶部。

语法:

theme( legend.position = "top", ..., complete = FALSE, validate = TRUE)

例子: 将图例放在顶部的R程序

# Installing and loading the package
library(ggplot2)
  
# Read the dataset
data("USArrests")
  
# Plot the data
plt <- ggplot(USArrests, aes(Murder, Assault, colour = UrbanPop)) + 
geom_point()
  
# For placing the legend position as top
plt + theme(legend.position = "top")

输出:

在R中使用ggplot2处理图例

将图例的位置放在底部

在这里,我们将改变图例的位置,把它放在底部。我们将使用theme()函数,我们将传递legend.position参数。legend.position参数将被分配为底部。

语法:

theme( legend.position = "bottom", ..., complete = FALSE, validate = TRUE)

例子: 将图例放在底部的R程序

# Installing and loading the package
library(ggplot2)
  
# Read the dataset
data("USArrests")
  
# Plot the data
plt <- ggplot(USArrests, aes(Murder, Assault, colour = UrbanPop)) + 
geom_point()
  
# For placing the legend position as bottom
plt + theme(legend.position = "bottom")

输出

在R中使用ggplot2处理图例

在图例周围绘制一个方框并为其添加背景颜色

我们将使用 theme()函数并提供legend.background作为参数,在我们的图例周围添加一个方框并为其添加颜色。legend.background等于element_rect()函数,该函数在我们的图例周围创建一个矩形框,我们可以使用fill参数为其添加背景颜色。

语法:

theme( legend.box.background = element_rect(), ...., complete = FALSE, validate = TRUE)

例子: R程序在图例周围画一个盒子,并为其添加背景色

library(ggplot2)
  
# Read the dataset
data("USArrests")
  
head(USArrests)
  
# Plot the data
plt <- ggplot(USArrests, aes(Murder, Assault, colour = UrbanPop)) +
geom_point()
  
# Drawing a rectangular box around legend 
# and adding background color to it
plt + theme( legend.background = element_rect( fill = "lightblue", 
                size = 0.5, linetype = "solid", colour = "black"))

输出

在R中使用ggplot2处理图例

改变关键风格和改变图例颜色

我们可以使用theme函数并提供legend.key作为参数来改变图例的关键风格和图例颜色。legend.key参数等于element_rect函数,该函数的参数包含用于图例键的轮廓颜色的 “color “和用于键的内部颜色的 “fill”。 as.factor() 函数将用于颜色参数,将矢量或列从数字转换成因子。

例子: R程序改变按键风格和图例颜色

library(ggplot2)
  
data("USArrests")
  
head(USArrests)
  
# Plot the data
plt <- ggplot(USArrests, aes(Murder, Assault, colour = 
                    as.factor(UrbanPop))) + geom_point()+
      theme(legend.key = element_rect( color = "black", 
                                      fill = "light blue"))
  
plt

输出 :

在R中使用ggplot2处理图例

例2: R程序改变钥匙样式和图例颜色

library(ggplot2)
  
data("USArrests")
   
head(USArrests)
  
# Plot the data
plt <- ggplot(USArrests, aes(Murder, Assault, colour = as.factor(UrbanPop))) +
geom_point()+ theme(legend.key = element_rect( color = "white", fill = "black"))
  
plt

输出:

在R中使用ggplot2处理图例

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程