📜  R中的分类气泡图

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

R中的分类气泡图

散点图是基于笛卡尔坐标系前提的相互关联的两个数值变量的图形表示,其中点或点绘制在从 X 轴和 Y 轴的值延伸的假想垂直线和水平线的交点处分别。一个变量沿 X 轴表示,通常为水平轴,另一个沿垂直方向表示,通常为 Y 轴。散点图的增强是气泡图,其中散点图的传统点或点被圆圈或气泡取代。这允许同时表示另一个数值变量的引入,其值被映射为对应于气泡的大小。因此,气泡图允许我们在同一图中映射三个数值变量。

气泡图的进一步扩展或增强是分类气泡图,除了已经在气泡图中映射的三个数值变量之外,我们还可以表示分类变量。分类气泡图的工作机制是气泡的位置由沿 X 轴和 Y 轴映射的两个数值变量的值确定。气泡的大小由第三个数值变量的值决定。除此之外,气泡的颜色指定了它所属的类别,这就是类别变量在同一图中的表示方式。在本文中,我们将探索在 R 中绘制气泡图的 2 种方法。这些方法如下所述:

  • 使用 ggplot2 库
  • 使用 plotly 库

方法一:使用GGPLOT2

在这种方法中,我们使用 ggplot2() 库,这是一个非常全面的库,用于渲染所有类型的图表和图形。在这种方法下,我们在 aes()函数添加了两个额外的参数,名称为“size”,其值为数值变量,“color”用于表示分类变量。下面提到了使用 ggplot 绘制气泡图所需的语法和参数。

要指定气泡的大小范围,我们使用:

方法

  • 创建数据集
  • 导入模块
  • 绘制数据框
  • 显示图

程序 :

R
# creating data set columns
height <- abs(rnorm(25, 175, 10))
  
weight <- abs(rnorm(25, 72, 10))
  
population <- floor(abs(rnorm(25, 1500, 500)))
  
cities <- c(rep("Delhi", 7), rep("Mumbai", 6),
            rep("Chennai", 6), rep("Bengaluru", 6))
  
# creating the dataframe from the above columns
dataframe <- data.frame(height, weight,
                        population, cities)
  
# importing the ggplot2 library
library(ggplot2)
  
# calling the ggplot function
# the value of the first parameter is the
# name of the dataframe
# the size of the bubbles is proportional to
# the population
# each of the 4 cities is identified by separate
# colors
ggplot(dataframe, aes(x = weight, y = height,
                      size = population,
                      color = cities))+
  
# specifying the transparence of the bubbles
# where value closer to 1 is fully opaque and
# value closer to 0 is completely transparent
geom_point(alpha = 0.7)+
  
# setting the scale of sizes of the bubbles using
# range parameter where the smallest size is 0.1
# and the largest one is 10
# name of the size legend is Population
scale_size(range = c(0.1, 10), name = "Population")+
  
# specifying the title for the plot
ggtitle("Height and Weight Data of 4 Cities")+
  
# code to center the title which is left aligned
# by default
theme(plot.title = element_text(hjust = 0.5))


R
# create data set column
height <- abs(rnorm(25, 175, 10))
  
weight <- abs(rnorm(25, 72, 10))
  
population <- floor(abs(rnorm(25, 1500, 500)))
  
cities <- c(rep("Delhi", 7), rep("Mumbai", 6),
            rep("Chennai", 6), rep("Bengaluru", 6))
  
# creating the dataframe 
dataframe <- data.frame(height, weight,
                        population, cities)
  
# calling the plotly library
library(plotly)
  
# declaring the bubbleplot variable
# to render the plot
# the first parameter is the dataframe
# x and y have the column name values of the 
# corresponding variables to plotted on the axes
# size holds the column name value to determine the 
# size of the bubbles
# color holds the value of the categorical cities
# variable
# sizes specify the range of sizes from 10 to 50
# marker parameter specifies the opacity and the 
# sizemode
bubbleplot <- plot_ly(dataframe, x = ~weight, y = ~height,
                      text = ~cities, size = ~population,
                      color = ~cities, sizes = c(10, 50),
                      marker =
                      list(opacity = 0.7,
                           sizemode = "diameter"))
  
# code to generate the layout
# specifying the title
# the grids are displayed
bubbleplot <- bubbleplot%>%layout
(title = "Height and Weight Data of 4 Cities",
 xaxis = list(showgrid = TRUE),
 yaxis = list(showgrid = TRUE))
  
# calling the bubbleplot variable to render
# the plot
bubbleplot


输出:

使用 ggplot 的分类气泡图

方法 2:使用 Plotly

Plotly 是一个 R 包/库,用于设计和渲染交互式图形。它建立在开源 Javascript 库之上,用于生成可供发布的图形和图表。如果您的 R 环境中不包含 plotly。



下面提到了使用 plotly 渲染分类气泡图所需的语法和参数:

方法

  • 创建数据框
  • 导入模块
  • 创建情节
  • 显示图

程序 :

电阻

# create data set column
height <- abs(rnorm(25, 175, 10))
  
weight <- abs(rnorm(25, 72, 10))
  
population <- floor(abs(rnorm(25, 1500, 500)))
  
cities <- c(rep("Delhi", 7), rep("Mumbai", 6),
            rep("Chennai", 6), rep("Bengaluru", 6))
  
# creating the dataframe 
dataframe <- data.frame(height, weight,
                        population, cities)
  
# calling the plotly library
library(plotly)
  
# declaring the bubbleplot variable
# to render the plot
# the first parameter is the dataframe
# x and y have the column name values of the 
# corresponding variables to plotted on the axes
# size holds the column name value to determine the 
# size of the bubbles
# color holds the value of the categorical cities
# variable
# sizes specify the range of sizes from 10 to 50
# marker parameter specifies the opacity and the 
# sizemode
bubbleplot <- plot_ly(dataframe, x = ~weight, y = ~height,
                      text = ~cities, size = ~population,
                      color = ~cities, sizes = c(10, 50),
                      marker =
                      list(opacity = 0.7,
                           sizemode = "diameter"))
  
# code to generate the layout
# specifying the title
# the grids are displayed
bubbleplot <- bubbleplot%>%layout
(title = "Height and Weight Data of 4 Cities",
 xaxis = list(showgrid = TRUE),
 yaxis = list(showgrid = TRUE))
  
# calling the bubbleplot variable to render
# the plot
bubbleplot

输出 :

使用 Plotly 生成的分类气泡图