R语言入门 | 使用 ggplot2 进行数据可视化
创作时间:
作者:
@小白创作中心
R语言入门 | 使用 ggplot2 进行数据可视化
引用
CSDN
1.
https://blog.csdn.net/mooridy/article/details/137018894
本文将介绍如何使用R语言中的ggplot2包进行数据可视化。文章将从数据准备、基本绘图、图形属性映射、分面、几何对象、统计变换、位置调整、坐标系等多个方面进行讲解,并通过具体的代码示例和图形展示帮助读者理解。
准备工作
首先需要安装并加载tidyverse包,其中包含了ggplot2以及其他一些数据处理和可视化的工具包。
install.packages("tidyverse")
library(tidyverse)
数据框
数据框是变量(列)和观测(行)的矩形集合。本文将使用mpg数据集,该数据集包含了由美国环境保护协会收集的38种车型的观测数据。
创建ggplot图形
使用ggplot创建一个基本的散点图:
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg):创建一张空白图geom_point():添加一个点层,创建散点图aes():定义数据变量到图形属性的映射
绘图模板
ggplot(data = <DATA>) +
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))
图形属性映射
可以将点的颜色映射为变量class:
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))
也可以手动设置图形属性:
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), color = "blue", shape=21, fill="red")
分面
facet_wrap()
按class分组,排成2行:
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2)
facet_grid()
按drv和cyl分组:
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ cyl)
几何对象
常见几何对象
geom_point():散点图geom_smooth():平滑曲线图geom_bar():条形图
叠加使用
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(x = displ, y = hwy))
统计变换
位置调整
dodge
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
identity
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "identity")
jitter
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), position = "jitter")
盒图
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot(aes(fill=class))
坐标系
图形分层语法
条形图
geom_bar
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
stat_count
ggplot(data = diamonds) +
stat_count(mapping = aes(x = cut))
显示比例
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
stat_summary
ggplot(data = diamonds) +
stat_summary(mapping = aes(x = cut, y = depth), fun.ymin = min, fun.ymax = max, fun.y = median)
位置调整
dodge
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
jitter
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), position = "jitter")
盒图
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot(aes(fill=class))
旋转坐标系
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()
绘制空间数据
nz <- map_data("nz")
ggplot(nz, aes(long, lat, group = group)) +
geom_polygon(fill = "white", color = "black") +
coord_quickmap
鸡冠图
coord_polar(theta="x")
p <- ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = cut)) +
coord_polar()
coord_polar(theta="y")
p <- ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = cut, width=1)) +
coord_polar(theta="y")
频率分布图
ggplot(data = diamonds, mapping = aes(x = price)) +
geom_freqpoly(binwidth = 10)
热门推荐
八段锦详细动作解析,每天十分钟打通全身经络
2025低空物流配送行业发展咨询报告(附数据分析图表)
橛橛梗梗:意志坚定不动摇的启示
地方债"发飞",怎么看?
驱蚊花露水成分
翡翠夜光杯的用途及特点:探索这种神奇杯子的独特价值和美丽光彩
长孙皇后:李世民心中的永恒挚爱
SPI通信协议详解:原理、时序与应用
减肥状态不佳应如何处理
驱寒祛湿的养生茶配方
讨论炮制对中药活性成分及功效的影响论文
【鼻炎】等于鼻敏感?与鼻窦炎一样吗?症状、原因、治疗及预防方法一览
如果拔完牙两三天后仍感剧烈疼痛,应及时就医处理!(内附拔牙术后注意事项)
生态设计:特点、优势和实例
肺不张的病因、诊断与治疗丨临床必备
JWT 续签方案解析与实战
激光祛斑后应怎样饮食以促进恢复
鲜牛奶和纯牛奶哪个更有营养
五大银行是哪五个?中国五大银行介绍
甲醛检测方法:科学评估室内环境安全
大股东和小股东发生矛盾怎么解决
怎样辨别银行客服电话的真假?
解析梦境中的人流,深度的心理探索
京AG6车牌代表什么含义
中医经络学说的历史背景与传承发展
什么是主力资金在股市中的作用?这种资金流动如何影响市场趋势和投资者策略?
制定有效的事件响应计划的实用指南
降三高、皮肤透亮!戒糖的好处、戒糖可以吃什么及不能吃什么
H.265 与 H.264 的主要区别
高强度运动后小腿抽筋怎么办?原因分析与解决方案