您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# R语言怎么画气泡图
## 一、气泡图简介
气泡图(Bubble Chart)是散点图的变种,它在二维坐标系中通过**圆点位置**表示两个变量的关系,同时通过**圆点大小**展示第三个变量的数值。这种图表常用于展示三个维度的数据关系,在金融分析、生物统计、社会科学等领域应用广泛。
### 气泡图的核心要素:
1. X轴变量(数值型)
2. Y轴变量(数值型)
3. 气泡大小变量(数值型)
4. 可选的颜色变量(分类或数值)
## 二、基础气泡图绘制
### 1. 使用基础绘图系统
```r
# 创建示例数据
set.seed(123)
data <- data.frame(
x = rnorm(30),
y = rnorm(30),
size = runif(30, 1, 20)
)
# 基础气泡图
plot(data$x, data$y,
cex = data$size/5, # 控制点大小
pch = 16, # 实心圆点
col = "steelblue",
xlab = "X变量",
ylab = "Y变量",
main = "基础气泡图")
library(ggplot2)
ggplot(data, aes(x = x, y = y, size = size)) +
geom_point(alpha = 0.7, color = "blue") +
scale_size(range = c(1, 15)) + # 调整大小范围
labs(title = "ggplot2气泡图",
x = "X变量",
y = "Y变量",
size = "大小指标") +
theme_minimal()
# 添加分类颜色变量
data$category <- factor(sample(letters[1:3], 30, replace = TRUE))
ggplot(data, aes(x, y, size = size, color = category)) +
geom_point(alpha = 0.7)) +
scale_size(range = c(3, 15)) +
scale_color_brewer(palette = "Set1")) +
labs(color = "分类") +
theme_bw()
# 添加文本标签
data$label <- paste0("ID", 1:30)
ggplot(data, aes(x, y, size = size, label = label)) +
geom_point(color = "orange", alpha = 0.6)) +
geom_text(size = 3, vjust = -1.5)) + # 文本位置调整
scale_size(range = c(2, 20))
library(gganimate)
library(gapminder)
# 使用gapminder数据集
ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent)) +
geom_point(alpha = 0.7)) +
scale_size(range = c(2, 20)) +
scale_x_log10())) +
labs(title = 'Year: {frame_time}', x = '人均GDP', y = '预期寿命') +
transition_time(year)) +
ease_aes('linear')
library(tidyverse)
covid_data <- read_csv("https://covid.ourworldindata.org/data/owid-covid-data.csv") %>%
filter(date == as.Date("2021-12-01")) %>%
select(location, total_cases, total_deaths, population) %>%
drop_na()
ggplot(covid_data, aes(x = total_cases/population*100,
y = total_deaths/total_cases*100,
size = population,
color = location)) +
geom_point(alpha = 0.6)) +
scale_size(range = c(1, 20), labels = scales::comma)) +
labs(x = "感染率(%)",
y = "病死率(%)",
title = "2021年全球COVID-19情况",
size = "人口数量",
color = "国家/地区") +
theme(legend.position = "right",
legend.box = "vertical") +
guides(color = guide_legend(override.aes = list(size = 5)))
# 使用position_jitter防止重叠
ggplot(data, aes(x, y, size = size)) +
geom_point(position = position_jitter(width = 0.1, height = 0.1)))
ggplot(data, aes(x, y, size = size)) +
geom_point(alpha = 0.3)) # 透明度设置为0.3
# 使用scale_size_area保持面积比例
ggplot(data, aes(x, y, size = size)) +
geom_point()) +
scale_size_area(max_size = 20))
问题原因:未正确指定size参数或数据格式错误
解决方案:
# 确保size变量是数值型
data$size <- as.numeric(data$size)
问题原因:图例元素过多或比例不当
解决方案:
ggplot(...) +
guides(size = guide_legend(override.aes = list(alpha = 1)))
解决方案:
# 使用geom_point的shape参数替代pch
ggplot(large_data, aes(x, y, size = size)) +
geom_point(shape = 21)) # 空心圆点渲染更快
library(maps)
world <- map_data("world")
ggplot() +
geom_polygon(data = world, aes(long, lat, group = group), fill = "gray90")) +
geom_point(data = covid_data,
aes(x = longitude, y = latitude, size = total_cases),
color = "red", alpha = 0.5)) +
coord_fixed(1.3))
library(plotly)
p <- ggplot(data, aes(x, y, size = size, color = category, text = label)) +
geom_point(alpha = 0.7))
ggplotly(p, tooltip = c("text", "x", "y", "size"))
R语言提供了多种绘制气泡图的方法,从基础绘图系统到ggplot2,再到交互式可视化。关键要点包括:
最佳实践建议:当数据点超过500个时,考虑使用抽样或热力图替代气泡图,避免视觉混乱。
附录:常用参数速查表
参数 | 作用 | 示例值 |
---|---|---|
size | 控制气泡大小 | 1-20 |
alpha | 控制透明度 | 0-1 |
color | 边框颜色 | “red” |
fill | 填充颜色 | ”#FF0000” |
shape | 点形状 | 16(实心圆) |
stroke | 边框宽度 | 0.5 |
”`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。