R语言 分类气泡图

R语言 分类气泡图

散点图是在直角坐标系的前提下,用图形表示两个数字变量之间的关系,在X轴和Y轴的数值分别延伸出来的假想垂直线和水平线的交点上绘制一个点或圆点。其中一个变量沿X轴(通常是水平轴)表示,另一个变量沿垂直方向(通常是Y轴)表示。散点图的一个加强版是气泡图,其中散点图的传统点或点被圆形或气泡所取代。这允许同时引入另一个数字变量来表示,其值与气泡的大小相对应。因此,气泡图允许我们在同一图中映射三个数字变量。

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

  • 使用 ggplot2 库
  • 使用 plotly 库

方法1:使用GGPLOT2

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

语法: ggplot(数据框架名称, aes(x = 第一个数字列的列名, y = 第二个数字变量的列名, size = 指定尺寸的变量列名, color = 分类变量的列名))

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

scale_size(range = 泡沫的尺寸范围, name = 显示在尺寸图例上方的名称)

方法

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

程序:

# 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

输出

R中的分类气泡图

使用ggplot绘制分类泡沫图

方法2:使用Plotly

Plotly是一个R包/库,用于设计和渲染交互式图表。它是建立在开源的Javascript库之上的,用于生成可出版的图形和图表。如果你的R环境中没有包含plotly,那么你可以使用它。

使用 plotly 渲染分类气泡图所需的语法和参数见下文。

语法:

我们使用 plot_ly () 函数来生成 plotly 的图。

参数

  • 数据集(这里是数据框)
  • x = ~ X轴上要显示的变量的列名
  • y = ~要在Y轴上显示的变量的列名
  • text = ~当光标悬停在气泡上方时要显示的列的名称
  • color = ~列名,颜色是根据它分配的(分类变量)。
  • size = ~列名,决定气泡的大小
  • sizes = c(),指定气泡大小的范围
  • marker = list() 用于指定不透明度和测量单位(如直径)。
  • 我们还可以使用layout()函数指定布局,包括标题、网格结构等。

方法

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

程序:

# 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
R

输出:

R中的分类气泡图

使用Plotly生成的分类泡沫图

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册