R语言 使用ggplot2修改坐标轴、图例和绘图标签

版本前瞻 2025-12-03 23:18:34

R语言 使用ggplot2修改坐标轴、图例和绘图标签

在这篇文章中,我们将看到如何在R编程语言中使用ggplot2条形图修改轴标签、图例和绘图标签。

为了创建一个简单的柱状图,我们将使用函数 geom_bar( )

语法: geom_bar(stat, fill, color, width)

参数:

stat: 设置stat参数以确定模式。

fill: 代表条形图内部的颜色。

color: 代表条形图轮廓的颜色。

width: 代表条形图的宽度。

使用中的数据集

让我们先把图表的原貌可视化,这样就可以看出变化。

例子

# Inserting data

ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),

runs=c(67,37,74,10))

head(ODI)

library(ggplot2)

# Default axis labels in ggplot2 bar plot

perf <-ggplot(data=ODI, aes(x=match, y=runs,fill=match))+

geom_bar(stat="identity")

perf

输出

在图中添加轴标签和主标题

默认情况下,R将使用数据框架中提供的变量作为轴的标签。我们可以很容易地修改它们并改变其外观。用来改变坐标轴标签的函数是 。

xlab( ): 用于横轴。

ylab( ) :用于垂直轴。

labs() : 同时用于两个轴。

element_text( ): 这个函数的参数是 。

语法

element_text( family, face, color, size, hjust, vjust, angle, margin)

element_blank( ): 使标签为空,并将其从图中删除。

参数 hjust (水平调整)或 vjust (垂直调整)用于移动轴的标签。它们的取值范围是[0,1],其中 。

hjust = 0 // 描绘轴的最左角

hjust = 0.5 // 描绘轴的中间部分

hjust = 1 // 描绘轴的最右角

使用的 关键字 是:

title : 添加绘图标签。

subtitle : 在图中添加副标题。

caption : 在图中添加标题。

axis.title.x : 用于水平轴。

axis.title.y 。用于垂直轴。

例子

library(ggplot2)

# Inserting data

ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),

runs=c(67,37,74,10))

# Default axis labels in ggplot2

# bar plot

perf <-ggplot(data=ODI, aes(x=match, y=runs,fill=match))+

geom_bar(stat="identity")

perf

# Manually adding axis labels and

# Plot Label

ggp<-perf+labs(x="Matches",y="Runs Scored",

title="Runs scored by Virat Kohli in ODI matches")

ggp

输出

使用labs()函数并通过subtitle参数和所需的subtitle,也可以将subtitle与主标题一起包含。

例子

library(ggplot2)

# Inserting data

ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),

runs=c(67,37,74,10))

# Default axis labels in ggplot2 bar plot

perf <-ggplot(data=ODI, aes(x=match, y=runs,fill=match))+

geom_bar(stat="identity")

perf

# Manually adding axis labels and Plot Label

ggp<-perf+labs(x="Matches",y="Runs Scored",

title="Runs scored by Virat Kohli in ODI matches")

ggp

# Subtitle and Caption

ggp+labs(subtitle="Performance",caption="GeeksforGeeks Trophy")

输出

要移动轴的标签,hjust参数是根据要求设置的。

例子

library(ggplot2)

# Inserting data

ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),

runs=c(67,37,74,10))

# Default axis labels in ggplot2 bar plot

perf <-ggplot(data=ODI, aes(x=match, y=runs,fill=match))+

geom_bar(stat="identity")

perf

# Manually adding axis labels and Plot Label

ggp<-perf+labs(x="Matches",y="Runs Scored",

title="Runs scored by Virat Kohli in ODI matches")

ggp

# Moving axis label to left

ggp + theme(

axis.title.x = element_text(hjust=0),

axis.title.y = element_text(hjust=0)

)

# Moving axis label in middle

ggp + theme(

axis.title.x = element_text(hjust=0.5),

axis.title.y = element_text(hjust=0.5)

)

# Moving axis label to right

ggp + theme(

axis.title.x = element_text(hjust=1),

axis.title.y = element_text(hjust=1)

)

输出

绘图的轴标签和主标题的外观格式化

轴标签和主标题可以被改变以反映所需的外观。为此,element_text()函数要传递所需的属性。

例子

library(ggplot2)

# Inserting data

ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),

runs=c(67,37,74,10))

# Default axis labels in ggplot2 bar plot

perf <-ggplot(data=ODI, aes(x=match, y=runs,fill=match))+

geom_bar(stat="identity")

perf

# Manually adding axis labels and Plot Label

ggp<-perf+labs(x="Matches",y="Runs Scored",

title="Runs scored by Virat Kohli in ODI matches")

# Plot title, axis labels format

ggp + theme(plot.title = element_text(

colour="#006000", size=14,face="bold"),

axis.title.x = element_text(

colour="Purple",size=10,face="bold.italic"),

axis.title.y = element_text(

colour="DarkBlue",size=10,face="bold.italic")

)

输出

移除坐标轴标签和绘图标题

对于这个theme()函数的调用,要参考图中的哪一部分要被修改。对于这些引用,请传递 element_blank(),不要有任何参数。

例子

library(ggplot2)

# Inserting data

ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),

runs=c(67,37,74,10))

# Default axis labels in ggplot2 bar plot

perf <-ggplot(data=ODI, aes(x=match, y=runs,fill=match))+

geom_bar(stat="identity")

perf

# Manually adding axis labels and Plot Label

ggp<-perf+labs(x="Matches",y="Runs Scored",

title="Runs scored by Virat Kohli in ODI matches")

# Remove chart title and axis label

ggp + theme(plot.title = element_blank(),

axis.title.x = element_blank(),

axis.title.y = element_blank()

)

输出

改变图例的位置

要改变图例的位置,需要以 legend.position 为参数调用 theme() 函数,并向该参数传递一个必要的位置。

语法

theme( legend.position = “Pos”)

参数

Pos :左、右、顶、底。

例子

library(ggplot2)

# Inserting data

ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),

runs=c(67,37,74,10))

# Default axis labels in ggplot2 bar plot

perf <-ggplot(data=ODI, aes(x=match, y=runs,fill=match))+

geom_bar(stat="identity")

perf

# Manually adding axis labels and Plot Label

ggp<-perf+labs(x="Matches",y="Runs Scored",

title="Runs scored by Virat Kohli in ODI matches")

# Move legend to top position of the plot

ggp + theme(legend.position="top")

输出

例子

library(ggplot2)

# Inserting data

ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),

runs=c(67,37,74,10))

# Default axis labels in ggplot2 bar plot

perf <-ggplot(data=ODI, aes(x=match, y=runs,fill=match))+

geom_bar(stat="identity")

perf

# Manually adding axis labels and Plot Label

ggp<-perf+labs(x="Matches",y="Runs Scored",

title="Runs scored by Virat Kohli in ODI matches")

# Move legend to bottom position of the plot

ggp + theme(legend.position="bottom")

输出