📜  R中ggplot2中的旋转和间隔轴标签

📅  最后修改于: 2022-05-13 01:54:45.451000             🧑  作者: Mango

R中ggplot2中的旋转和间隔轴标签

在本文中,我们将讨论如何在 R 编程语言中的 ggplot2 中旋转和空间轴标签。

间隔轴标签:

我们可以使用主题函数增加或减少轴标签和轴之间的空间。 theme()函数的axis.txt.x /axis.text.y 参数用于使用element_text()函数的hjust 和vjust 参数调整间距。

句法:

plot + theme( axis.text.x / axis.text.y = element_text( hjust, vjust )

在哪里,

  • hjust:确定水平对齐方式
  • vjust:确定垂直对齐

例子:



在这个例子中,我们在 R 语言的 ggplot2 图中使用主题函数的 vjust 命令添加了 10 个点的垂直空间。

R
# Create sample data
set.seed(5642)                             
sample_data <- data.frame(name = c("Geek1","Geek2",
                                   "Geek3","Geek4",
                                   "Geeek5") ,
                          value = c(31,12,15,28,45)) 
  
# Load ggplot2 pckage
library("ggplot2") 
  
# Create bar plot using ggplot() function
ggplot(sample_data,
             aes(name,value,, color=name)) + 
  
# geom_bar function is used to plot bars 
# of barplot
geom_bar(stat = "identity", fill="white")+
  
# vjust is used to justify the label vertically
theme(axis.text.x = element_text(vjust=-10))


R
# Create sample data
set.seed(5642)                             
sample_data <- data.frame(name = c("Geek1","Geek2",
                                   "Geek3","Geek4",
                                   "Geeek5") ,
                          value = c(31,12,15,28,45)) 
  
# Load ggplot2 pckage
library("ggplot2") 
  
# Create bar plot using ggplot() function
ggplot(sample_data,
             aes(name,value,, color=name)) + 
  
# geom_bar function is used to plot bars 
# of barplot
geom_bar(stat = "identity", fill="white")+
  
# rotate axis label using axis.text.x parameter
# of theme() 90 degree rotation makes label 
# vertical
theme(axis.text.x = element_text(angle = 90))


R
# Create sample data
set.seed(5642)                             
sample_data <- data.frame(name = c("Geek1","Geek2",
                                   "Geek3","Geek4",
                                   "Geeek5") ,
                          value = c(31,12,15,28,45)) 
  
# Load ggplot2 pckage
library("ggplot2") 
  
# Create bar plot using ggplot() function
ggplot(sample_data,
             aes(name,value,, color=name)) + 
  
# geom_bar function is used to plot bars 
# of barplot
geom_bar(stat = "identity", fill="white")+
  
# rotate axis label using axis.text.x parameter of theme()
# vjust is used to justify the label to avoid 
# overlapping with plot
theme(axis.text.x = element_text(angle = 45, vjust=0.5))


输出:

旋转轴标签

我们可以使用主题函数旋转轴标签和轴。 theme()函数的axis.txt.x /axis.text.y 参数用于使用element_text()函数的angle 参数调整标签的旋转。

句法:

plot + theme( axis.text.x / axis.text.y = element_text( angle )

在哪里,

角度:确定旋转角度



例子:

在本例中,我们使用 R 语言中 ggplot2 绘图中主题函数的角度命令将旋转角度设为 90 度。这使轴标签垂直。

电阻

# Create sample data
set.seed(5642)                             
sample_data <- data.frame(name = c("Geek1","Geek2",
                                   "Geek3","Geek4",
                                   "Geeek5") ,
                          value = c(31,12,15,28,45)) 
  
# Load ggplot2 pckage
library("ggplot2") 
  
# Create bar plot using ggplot() function
ggplot(sample_data,
             aes(name,value,, color=name)) + 
  
# geom_bar function is used to plot bars 
# of barplot
geom_bar(stat = "identity", fill="white")+
  
# rotate axis label using axis.text.x parameter
# of theme() 90 degree rotation makes label 
# vertical
theme(axis.text.x = element_text(angle = 90)) 

输出:

例子:

在此示例中,我们将轴标签旋转了 45 度,使其与绘图重叠。所以我们使用 vjust 参数将其向下移动以避免重叠。

电阻

# Create sample data
set.seed(5642)                             
sample_data <- data.frame(name = c("Geek1","Geek2",
                                   "Geek3","Geek4",
                                   "Geeek5") ,
                          value = c(31,12,15,28,45)) 
  
# Load ggplot2 pckage
library("ggplot2") 
  
# Create bar plot using ggplot() function
ggplot(sample_data,
             aes(name,value,, color=name)) + 
  
# geom_bar function is used to plot bars 
# of barplot
geom_bar(stat = "identity", fill="white")+
  
# rotate axis label using axis.text.x parameter of theme()
# vjust is used to justify the label to avoid 
# overlapping with plot
theme(axis.text.x = element_text(angle = 45, vjust=0.5)) 

输出: