R语言 如何为ggplot添加标题

R语言 如何为ggplot添加标题

在这篇文章中,我们将看到如何在R编程语言中为一个图添加标题。标题在数据可视化中非常重要,可以显示与图表相关的一些细节。

准备数据

为了绘制我们将使用的散点图,我们将使用geom_point()函数。以下是关于ggplot函数geom_point()的简要信息。

语法 : geom_point(size, color, fill, shape, stroke)

参数 :

  • size : 点的大小

  • color : 点的颜色/边界

  • fill : 点的颜色

  • shape : 点的形状,范围为0-25

  • stroke。点的边界厚度

    返回:它创建了散点图。

# import lib
library(ggplot2)
  
# plot datapoint using iris
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point()

输出

如何在R语言中为ggplot添加标题?

在图中添加标题

为了添加标题,我们将使用 labs() 函数中的标题属性。

语法: labs(caption)

参数。

  • caption。字符串标题
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point()+
  
    # adding caption and subtitle
    labs(subtitle="Scatter plot",
       caption="Geeksforgeeks"
       )

输出

如何在R语言中为ggplot添加标题?

自定义标题文本

element_text() 方法可以用来定制图形中的标题。

library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
    geom_point()+
  
    # adding caption and subtitle
    labs(subtitle="Scatter plot - Examples",
       caption="Geeksforgeeks")+ 
  
    # size of the caption
    theme(plot.caption= element_text(size=15,
                                   color="Green"))

输出

如何在R语言中为ggplot添加标题?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程