R语言 ggplot中移动坐标轴标签

R语言 ggplot中移动坐标轴标签

在这篇文章中,我们将看到如何在R编程语言中使用ggplot2条形图移动轴的标签。

首先,如果之前没有在R Studio中安装ggplot2软件包,你需要安装它。为了创建一个简单的柱状图,我们将使用函数 geom_bar()

语法

geom_bar(stat, fill, color, width)

参数 :

  • stat : 设置stat参数以确定模式。
  • fill : 代表条形图内部的颜色。
  • color : 代表条形图轮廓的颜色。
  • width : 代表条形图的宽度。

使用中的数据

在R语言的ggplot中移动坐标轴标签

让我们看看没有任何修改的默认绘图是什么样子的。

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语言的ggplot中移动坐标轴标签

默认情况下,R将在数据框中分配的矢量名称作为轴的标题。要手动添加轴标题,请使用以下命令。

// 要修改X轴的标签
xlab(“X_axis_Labelname”)
// 要修改Y轴的标签
ylab(“Y_axis_Labelname”)
// 同时修改x轴和y轴的标题
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

输出

在R语言的ggplot中移动坐标轴标签

为了对轴标签进行任何修改,我们使用函数 element_text( ) 这个函数的参数是 :

  • Color
    • Size
    • Face
    • Family
    • lineheight
    • hjust and vjust

取值范围是 [ 0 ,1] ,其中 :

//描述轴的最左角
hjust = 0
//描述轴的中间部分
hjust = 0.5
//描述轴的最右角
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)
  )

输出

在R语言的ggplot中移动坐标轴标签

现在让我们把它们移到中心。

例子

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语言的ggplot中移动坐标轴标签

类似的做法也可以用于向右移动它们。

例子

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语言的ggplot中移动坐标轴标签

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程