R语言 使用ggplot2删除坐标轴标签

R语言 使用ggplot2删除坐标轴标签

在这篇文章中,我们将看到如何在R编程语言中删除ggplot2绘图的轴标签。

我们将使用ggplot2软件包中的theme()函数。在这种移除ggplot2绘图标签的方法中,用户首先必须在R控制台中导入并加载ggplot2包,这是这种方法的前提条件,然后用户必须调用 theme() 函数,这是ggplot2包的函数,并进一步需要传递 element_blank() 作为其参数,这将有助于在R编程语言中移除ggplot2绘图的空白标签。

theme()函数: 使用这个函数是一个强大的方式来定制你的绘图的非数据组件:即标题、标签、字体、背景、网格线和图例。这个函数也可以用来给绘图一个一致的自定义外观。

在这个例子中,我们将展示没有删除标签的图的外观。

library("ggplot2")   
gfg_data <- data.frame(x = c(1,2,3,4,5),
                       y = c(5,4,3,2,1))
gfg_plot <- ggplot(gfg_data, aes(x,y)) +  
  geom_point()
gfg_plot
R

输出

在R语言中使用ggplot2删除坐标轴标签

例子1: 在这个例子中,我们将使用R编程语言中ggplot2包的theme()函数去除5个数据点的ggplot2散点图的标签。

library("ggplot2")   
gfg_data<-data.frame(x = c(1,2,3,4,5),
                     y = c(5,4,3,2,1))
gfg_plot <- ggplot(gfg_data, aes(x,y)) +  
  geom_point()
gfg_plot +
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())
R

输出

在R语言中使用ggplot2删除坐标轴标签

例2: 在这个例子中,我们将使用R编程语言中ggplot2包中的theme()函数来删除ggplot2条形图的标签。

library("ggplot2")   
gfg_data<-data.frame(x = c(1,2,3,4,5),
                     y = c(5,4,3,2,1))
p<-ggplot(data=gfg_data, aes(x, y)) +
  geom_bar(stat="identity")
p+
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())
R

输出

在R语言中使用ggplot2删除坐标轴标签

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册