R语言绘制气泡图

气泡图是一种数据可视化图表。它通常在二维或三维空间中展示数据。两个变量决定气泡在平面或空间中的位置,第三个变量则以气泡大小呈现。能直观反映三个变量间关系,帮助用户快速理解数据特征和趋势,在数据分析和展示中广泛应用。

0x01 使用symbols()函数

一、函数用法及参数说明

symbols(x, y = NULL, circles, squares, rectangles, stars, thermometers, boxplots, inches = TRUE, add = FALSE, fg = par("col"), bg = NA, xlab = NULL, ylab = NULL, main = NULL, xlim = NULL, ylim = NULL,...)

1.x:表示横坐标位置。

2.y:表示纵坐标位置。

3.circles:圆形的半径。

4.squares:正方形的边长。

5.rectangles:绘制矩形,需搭配两列矩阵指定宽高,第一列表示宽度,第二列表示长度。

6.stars:绘制星形,搭配多列矩阵定义射线长度。

7.thermometers:绘制温度计,搭配三或四列矩阵定义宽高及填充比例。

8.boxplots:绘制箱线图,搭配五列矩阵定义宽高、须长及中位数位置比例。

9.inches:尺寸大小,取值为FLASE、TRUE或者一个给定的正数。

10.add:如果为TRUE,符号将添加到现有图形上,否则将创建一个新图形。

11.fg:表示符号的边框颜色。

12.bg:表示符号的填充颜色。

13.xlab:x轴标签。

14.ylab:y轴标签。

15.main:图形的主标题。

16.xlim:指定x轴范围。

17.ylim:指定y轴范围。

二、数据准备

这里作为方便演示,使用ggplot2包的diamonds数据集,它包含了超过 50,000 颗圆形切割钻石的价格和其他属性信息,可用于对钻石数据进行分析和可视化。

r 复制代码
#加载ggplot2包
library(ggplot2)
#由于数据中存在5w多条数据,这里提取十五条作为演示。
diamond <- diamonds[0:15,]

三、绘制基本的气泡图

r 复制代码
symbols(diamond$depth,diamond$price,circle = diamond$carat)

四、美化和调整

r 复制代码
# 绘制以钻石深度为横坐标、价格为纵坐标、克拉重量决定圆形大小的图形
symbols(diamond$depth,diamond$price,circle = diamond$carat,inches = 0.5,fg = "white",bg = "lightblue3",main = "part of diamonds",ylab = "price of diamond",xlab = "depth of diamond")
# 在对应的坐标位置添加钻石的切割等级文本
text(diamond$depth,diamond$price,diamond$cut,cex = 0.5)

0x02 使用ggplot2包

一、准备工作

r 复制代码
#加载ggplot2包
library(ggplot2)
#sample(sequence, n)函数从给定的序列中随机抽取n个元素。在这里,从 1 到 500000 的序列中随机抽取 30 个整数。
diamond <- diamonds[sample(1:50000,30),]

二、绘制基本的气泡图

使用geom_point()函数绘制气泡图。

r 复制代码
ggplot(diamond,aes(x = depth,y = price)) +
  geom_point(size = diamond$price/400)

三、美化与调整

r 复制代码
ggplot(diamond,aes(x = depth,y = price)) +
  geom_point(size = diamond$price/400,shape = 19,color = 7) +
  labs(title = "气泡图",x = "depth of diamond",y = "price of diamond") +
  theme(plot.title = element_text(hjust = 0.5))

也可以将某些参数设定为因子,然后对颜色进行区分。

r 复制代码
ggplot(diamond,aes(x = depth,y = price)) +
  geom_point(size = diamond$price/400,shape = 19,color = factor(diamond$depth)) +
  labs(title = "气泡图",x = "depth of diamond",y = "price of diamond") +
  theme(plot.title = element_text(hjust = 0.5))

也可以对颜色进行手动的设置。

r 复制代码
ggplot(diamond,aes(x = depth,y = price)) +
  geom_point(size = diamond$price/400,shape = 19,color = "grey") +
  annotate("text",x = diamond$depth,y = diamond$price,label = diamond$cut,fontface = "italic",colour = "darkred",size = 3)
#fontface = "italic"设置文本的字体样式为斜体。
相关推荐
Rocky4015 分钟前
JAVAEE->多线程:锁策略
java·开发语言·jvm
生信学术纵览20 分钟前
中科院1区顶刊|IF14+:多组学MR联合单细胞时空分析,锁定心血管代谢疾病的免疫治疗新靶点
数据挖掘·数据分析
JSUITDLWXL30 分钟前
ideal2022.3.1版本编译项目报java: OutOfMemoryError: insufficient memory
java·开发语言
magic 24534 分钟前
Java建造者模式(Builder Pattern)详解与实践
java·开发语言·建造者模式
前端小崔36 分钟前
前端面试题之ES6保姆级教程
开发语言·前端·javascript·面试·职场和发展·ecmascript·es6
Love__Tay1 小时前
【学习笔记】Python金融基础
开发语言·笔记·python·学习·金融
Lilith的AI学习日记1 小时前
什么是预训练?深入解读大模型AI的“高考集训”
开发语言·人工智能·深度学习·神经网络·机器学习·ai编程
我命由我123451 小时前
VSCode - VSCode 放大与缩小代码
前端·ide·windows·vscode·前端框架·编辑器·软件工具
捷码小编1 小时前
数据可视化大屏案例落地实战指南:捷码平台7天交付方法论
低代码·数字孪生·数据可视化
PT_silver2 小时前
tryhackme——Abusing Windows Internals(进程注入)
windows·microsoft