R语言 绘制对数直方图

R语言 绘制对数直方图

R是一种编程语言,主要用于统计计算和图形。它可以实现各种统计技术,包括线性建模、分类、时间序列分析和聚类。直方图通常用于描述变量的频率分布。它也经常被用来总结连续和离散的数据。与条形图不同的是,在条形图中,频率由条形的高度来表示,在这里,频率由条形的面积来表示。

在这篇文章中,我们将看到如何在R编程语言中用对数尺度绘制直方图。

方法1:使用基础R

hist()函数来创建直方图。

语法

语法: hist( v, main, xlab, xlim, ylim, breaks, col, border)

如果我们想把直方图的值转换成对数尺度,我们可以在 hist() 函数中使用 log() 函数,如下所示。

hist(log(v))

  • v : 包含直方图中使用的数字值的向量。

例1:创建一个正态直方图

取一个向量 ‘A ‘,其中包含直方图要使用的数值。使用 hist() 函数绘制向量 ‘A ‘,使用主参数将图的标题设置为 “直方图”。

# Code for Plotting A Normal Histogram
# Create data for the Histogram
A <- c(10, 10, 10, 20, 15,
       15, 20, 20, 20, 20)
  
# Plot the histogram
hist(A, main = "Histogram")

输出

用R语言绘制对数直方图

例2:在R中创建一个对数直方图

取一个向量 ‘A ‘,它包含直方图要使用的数值,向量 ‘A ‘使用 hist( )函数中的 log() 函数来绘制。

绘图的标题被设置为 “直方图”,条形图的颜色被设置为 “绿色”,条形图的边框颜色被设置为 “红色”,xlabel被设置为 “对数刻度”,分别使用main, col, border, xlab参数。

代码

# Code for Plotting A Histogram in
# the Logarithmic Scale
  
# Create data for the Histogram
A <- c(10, 10, 10, 20, 20,
       20, 20, 20, 30, 30)
  
# Plot the histogram
hist(log(A), main = "Histogram",
     col = "green", border = "red",
     xlab = "Logarithmic Scale")

输出

用R语言绘制对数直方图

方法2:使用ggplot2包

使用 install.packages() 安装 ggplot2 包,使用 library( )加载 ggplot2 包。

# Install & load ggplot2 package
install.packages("ggplot2")
library("ggplot2")

ggplot2是一个专门用于数据可视化的R包。使用ggplot2包,我们可以绘制几乎所有类型的图表。

语法: ggplot(data.frame(x), aes(x))+ geom_histogram()

  • x 是包含直方图所使用的数值的向量。
  • data.fram(x )从给定的数据中创建一个数据框。
  • aes(x) 经常被用于其他图形元素中,以指定所需的美学效果。
  • geom_histogram() 用于绘制直方图。

创建一个正态直方图

  1. 取一个向量 “A” ,包含直方图要使用的数值。
  2. 使用 ggplot() 函数和 geom_histogram() 函数绘制直方图。

例子

library("ggplot2")
  
# Code for Plotting A Histogram in 
# the Logarithmic Scale
  
# Create data for the Histogram
A <- c(10, 10, 10, 20, 15,
       15, 20, 20, 20, 20)
  
# Plot the histogram
# The bins parameter divides the plot
# into 5 parts/bars
ggplot(data.frame(A), aes(A)) + geom_histogram(bins = 5)

输出

用R语言绘制对数直方图

方法3:使用ggplot2包和log()函数

如果我们想把直方图的数值转换成对数尺度,我们可以在 ggplot()geom_histogram( )函数中使用 log() 函数,如下所示。

语法: ggplot(data.frame(log(x)), aes(log(x)))+ geom_histogram()

  • x 是包含直方图要使用的数值的向量。
  • data.frame(log(x ))从 log(x )创建一个数据框。
  • aes(log(x)) 经常被用于其他图形元素中,以指定所需的美学效果。
  • geom_histogram() 用于绘制柱状图。

取一个向量 ‘A ‘,其中包含直方图要使用的数值。使用 log() 函数将X轴转换成对数尺度,使用 ggplot() 函数、 log() 函数和 geom_histogram() 函数绘制向量 ‘A ‘。使用 fillcolor 分别将条形图的颜色设置为绿色,边界颜色设置为红色。

library("ggplot2")
  
  
# Code for Plotting A Histogram in 
# the Logarithmic Scale
  
# Create data for the Histogram
A <- c(10, 10, 10, 20, 15,
       15, 20, 20, 20, 20)
  
# Plot the histogram
# The bins parameter divides the plot 
# into 5 parts/bars
ggplot(data.frame(log(A)),
       aes(log(A))) + geom_histogram(bins = 5)

输出

用R语言绘制对数直方图

方法4:使用ggplot2包和scale_x_log10()函数。

如果我们想把直方图的值转换成对数尺度,我们可以使用scale_x_log10()函数和ggplot()以及geom_histogram()函数,如下所示。

语法: ggplot(data.frame(x), aes(x))+ geom_histogram() + scale_x_log10()

参数

  • x 是包含直方图所使用的数值的向量。
  • data.fram(x )从 x 创建一个数据框
  • aes(x) 经常被用于其他图形元素中,以指定所需的美学效果。
  • geom_histogram() 用于绘制直方图。
  • scale_x_log10() 用于将x轴转换为对数尺度。

取一个向量 ‘A ‘,它包含直方图要使用的数值。使用 ggplot() 函数、 scale_x_log10() 函数和 geom_histogram() 函数绘制向量 ‘A ‘,条形图的颜色被设置为绿色,边界颜色被设置为红色,分别使用 fillcolor

library("ggplot2")
  
  
# Code for Plotting A Histogram in 
# the Logarithmic Scale
  
# Create data for the Histogram
A <- c(10, 10, 10, 20, 15,
       15, 20, 20, 20, 20)
  
# Plot the histogram
# The bins parameter divides the
# plot into 6 parts/bars
ggplot(data.frame(A), aes(A)) +
geom_histogram(color = "red", fill = "green", bins = 6) +
scale_x_log10()

输出

用R语言绘制对数直方图

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程