R语言 ggplot2中的分组、叠加和叠加百分比条形图

R语言 ggplot2中的分组、叠加和叠加百分比条形图

ggplot 是一个用于在R语言中生成图形的库。我们提供数据,并指定美学上的指定数据应如何映射。它是一个非常强大的库,广泛用于生成全面的图形和图表。它被用于创建基于 “图形语法 “的图形。

柱状图或条形图是一种数据可视化工具,广泛用于表示数字和分类变量之间的关系。数字变量通常绘制在Y轴上,分类变量绘制在水平X轴上。条形图的高度代表分类值的相应数字值。当我们有一个以上的分类变量和一个数字变量时,上述的方法就会很方便。

在这篇文章中,我们将看到如何绘制3种不同类型的条形图。这三种不同类型的柱状图是 :

  • 分组条形图
  • 叠加条形图
  • 堆积式条形图的百分比

这三种图的代码的唯一区别是在ggplot库的geom_bar()函数中 “position “参数的值。下面给出了同样的实现。

例子:

# importing the ggplot2 library
library(ggplot2)
  
# creating the cities column
# c() is used to combine vectors
# rep() is used for replication of values
cities <- c(rep("Delhi", 3), rep("Mumbai", 3),
            rep("Chennai", 3), rep("Bengaluru", 3))
  
# creating the humidity column
# contains 3 classes 
humidity <- rep(c("High", "Medium", "Low"), 4)
  
# creating the temperature column
# abs() is used for getting the absolute value
# rnorm() is used for generating random variates
# in a normal distribution
# rnorm(number of samples, mean, SD)
temperature <- abs(rnorm(12, 25, 10))
  
# dataframe consisting of the three columns
dataframe <- data.frame(cities, humidity,
                        temperature)
  
# calling the dataframe
dataframe
R

ggplot2中的分组、叠加和叠加百分比条形图

分组条形图

分组条形图或聚类条形图用于将单变量或单类别条形图的功能扩展到多变量条形图。在这些图中,条形图根据其类别被分组,颜色是代表其他分类变量的区别因素。条形图被定位为迎合一个组或主要组,颜色代表次要类别。对于分组条形图,位置参数的值被指定为 “dodge “。

方法

  • 导入模块
  • 创建数据框架
  • 用所需函数绘制图表
  • 在geom_bar( )函数中设置位置参数为dodge
  • 显示图表

语法:

geom_bar(position = “dodge” , ….)

例子

# importing the ggplot2 library
library(ggplot2)
  
# creating data frame
cities <- c(rep("Delhi", 3), rep("Mumbai", 3),
            rep("Chennai", 3), rep("Bengaluru", 3))
   
humidity <- rep(c("High", "Medium", "Low"), 4)
  
temperature <- abs(rnorm(12, 25, 10))
  
dataframe <- data.frame(cities, humidity,
                        temperature)
  
# calling the dataframe
dataframe
  
# plotting the graph
ggplot(dataframe, aes(fill = humidity,
                      y = temperature, x = cities))+
geom_bar(position = "dodge", stat = "identity")+
ggtitle("Weather Data of 4 Cities !")+
theme(plot.title = element_text(hjust = 0.5))
R

输出

ggplot2中的分组、叠加和叠加百分比条形图

天气数据集的分组条形图

叠加条形图

堆积条形图或堆积条形图是标准条形图的延伸,我们可以在一个条形图的帮助下表示两个分类的变量。在这些图中,主要分类变量的条形图决定了其位置,而次要分类变量的不同层次则根据其颜色进行区分,并相互叠加。对于堆叠条形图,位置参数的值被指定为 “堆叠”。

方法

  • 导入模块
  • 创建数据框架
  • 用必要的函数绘制图形
  • 在geom_bar( )函数中为堆栈设置位置参数
  • 显示图表

语法:

geom_bar(position = "stack" , ...)

例子

# importing the ggplot2 library
library(ggplot2)
  
# creating data frame
cities <- c(rep("Delhi", 3), rep("Mumbai", 3),
            rep("Chennai", 3), rep("Bengaluru", 3))
  
humidity <- rep(c("High", "Medium", "Low"), 4)
  
temperature <- abs(rnorm(12, 25, 10))
  
dataframe <- data.frame(cities, humidity,
                        temperature)
  
# calling the dataframe
dataframe
  
# plotting graph
ggplot(dataframe, aes(fill = humidity,
                      y = temperature, x = cities))+
geom_bar(position = "stack", stat = "identity")+
ggtitle("Weather Data of 4 Cities !")+
theme(plot.title = element_text(hjust = 0.5))
R

输出

ggplot2中的分组、叠加和叠加百分比条形图

天气数据集的叠加条形图

叠加条形图的 百分比

叠加百分比条形图用于显示每个分类变量的贡献或比例,同时累积主要分类变量。整个条形图被填充到顶部,不同组别占据了与其在条形图中的比例相应的高度。为了绘制百分比堆积条形图,位置参数的值被指定为 “fill “。

方法

  • 导入模块
  • 创建数据框架
  • 用所需函数绘制图表
  • 设置位置参数以填充geom_bar( )函数
  • 显示图表

语法:

geom_bar(position = "fill" , ....)

例子

# importing the ggplot2 library
library(ggplot2)
  
# creating data frame
cities <- c(rep("Delhi", 3), rep("Mumbai", 3),
            rep("Chennai", 3), rep("Bengaluru", 3))
  
humidity <- rep(c("High", "Medium", "Low"), 4)
  
temperature <- abs(rnorm(12, 25, 10))
  
dataframe <- data.frame(cities, humidity,
                        temperature)
  
# calling the dataframe
dataframe
  
# plotting graph
ggplot(dataframe, aes(fill = humidity,
                      y = temperature, x = cities))+
geom_bar(position = "fill", stat = "identity")+
ggtitle("Weather Data of 4 Cities !")+
theme(plot.title = element_text(hjust = 0.5))
R

输出:

ggplot2中的分组、叠加和叠加百分比条形图

天气数据集的百分比叠加条形图

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册