在 R 中的 ggplot 中移动轴标签
在本文中,我们将了解如何使用 R 编程语言中的 ggplot2 条形图移动轴标签。
首先,如果之前未在 R Studio 中安装 ggplot2 包,则需要安装它。为了创建一个简单的条形图,我们将使用函数geom_bar() 。
Syntax:
geom_bar(stat, fill, color, width)
Parameters :
- stat : Set the stat parameter to identify the mode.
- fill : Represents color inside the bars.
- color : Represents color of outlines of the bars.
- width : Represents width of the bars.
使用中的数据:
让我们看看没有任何修改的默认图会是什么样子。
R
library(ggplot2)
# Inserting data
ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),
runs=c(67,37,74,10))
head(ODI)
# Default axis labels in ggplot2 bar plot
perf <-ggplot(data=ODI, aes(x=match, y=runs,fill=match))+
geom_bar(stat="identity")
perf
R
library(ggplot2)
# Inserting data
ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),
runs=c(67,37,74,10))
head(ODI)
# 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
ggp <- perf+labs(x="Matches",y="Runs Scored")
ggp
R
library(ggplot2)
# Inserting data
ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),
runs=c(67,37,74,10))
head(ODI)
# 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
ggp <- perf+labs(x="Matches",y="Runs Scored")
ggp
# Moving axis label to left
ggp + theme(
axis.title.x = element_text(hjust=0),
axis.title.y = element_text(hjust=0)
)
R
library(ggplot2)
# Inserting data
ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),
runs=c(67,37,74,10))
head(ODI)
# 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
ggp <- perf+labs(x="Matches",y="Runs Scored")
ggp
# Moving axis label in middle
ggp + theme(
axis.title.x = element_text(hjust=0.5),
axis.title.y = element_text(hjust=0.5)
)
R
library(ggplot2)
# Inserting data
ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),
runs=c(67,37,74,10))
head(ODI)
# 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
ggp <- perf+labs(x="Matches",y="Runs Scored")
ggp
# Moving axis label to right
ggp + theme(
axis.title.x = element_text(hjust=1),
axis.title.y = element_text(hjust=1)
)
输出:
默认情况下,R 添加在数据框中分配的矢量名称作为轴标题。要手动添加轴标题,请使用以下命令:
// To modify the x axis label
xlab(“X_axis_Labelname”)
// To modify the y axis label
ylab(“Y_axis_Labelname”)
// Simultaneously modify both x and y axes title
labs(x=”X_axis_Labelname”,y=”Y_axis_Labelname”)
例子:
电阻
library(ggplot2)
# Inserting data
ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),
runs=c(67,37,74,10))
head(ODI)
# 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
ggp <- perf+labs(x="Matches",y="Runs Scored")
ggp
输出:
要对轴标签进行任何修改,我们使用函数element_text()。这个函数的参数是:
- 颜色
- 尺寸
- 脸
- 家庭
- 行高
- hjust 和 vjust
参数 hjust(水平调整)或 vjust(垂直调整)用于移动轴标签。它们采用范围[0,1]中的数字,其中:
// Depicts left most corner of the axis
hjust = 0
// Depicts middle of the axis
hjust = 0.5
// Depicts right most corner of the axis
hjust = 1
让我们首先创建一个带有向左轴标签的图。
例子:
电阻
library(ggplot2)
# Inserting data
ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),
runs=c(67,37,74,10))
head(ODI)
# 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
ggp <- perf+labs(x="Matches",y="Runs Scored")
ggp
# Moving axis label to left
ggp + theme(
axis.title.x = element_text(hjust=0),
axis.title.y = element_text(hjust=0)
)
输出:
现在让我们将它们移动到中心。
例子:
电阻
library(ggplot2)
# Inserting data
ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),
runs=c(67,37,74,10))
head(ODI)
# 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
ggp <- perf+labs(x="Matches",y="Runs Scored")
ggp
# Moving axis label in middle
ggp + theme(
axis.title.x = element_text(hjust=0.5),
axis.title.y = element_text(hjust=0.5)
)
输出:
类似的方法可以将它们向右移动。
例子:
电阻
library(ggplot2)
# Inserting data
ODI <- data.frame(match=c("M-1","M-2","M-3","M-4"),
runs=c(67,37,74,10))
head(ODI)
# 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
ggp <- perf+labs(x="Matches",y="Runs Scored")
ggp
# Moving axis label to right
ggp + theme(
axis.title.x = element_text(hjust=1),
axis.title.y = element_text(hjust=1)
)
输出: