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)
热门推荐
股票趋势判断:从技术分析到量化分析的全面指南
车辆定损的基本步骤
司马懿的祖先与司马氏的崛起之路
头痛头晕、情绪低落,中医推荐多按压头顶这个穴位
奥美拉唑什么时候吃
闽北、闽西“战”洪灾 中国安能二局厦门救援基地抗洪抢险勇担当
为什么股票会横盘震荡?它对市场有什么影响?
音乐剧《战争与和平》的中国化表达和时代启示
游戏王灵摆召唤入门指南:机制详解与实战应用
明朝募兵制:特点、优点与缺点的深度解析
千里援疆山海情|泰州援疆花式助力马产业,让昭苏文旅“天马腾飞”
显卡电源不接的后果和解决方法
何为痉挛症状
科学实在论与反实在论的核心争论
兰州交通大学:百年交大,铁路摇篮
车船税怎么退
等离子喷涂多孔镍网为何能提升工业制氢效率?
烟酰胺美白的原理是什么
视觉训练不能“治愈”近视,但是可以“防控”近视!!!
高压反应釜的安全标准与规范
信号上升沿与系统带宽的关系
心包胸腺瘤治疗方法
9 种最容易学习的乐器
宝宝拒奶真相:识别、解读与应对策略
什么是贫血?如何通过血液检验来判断贫血?
饮食指南:6种富含凝集素的食物,看看您吃过没?
C语言猜数字小游戏完整教程
想吃蛋糕自己在家做,教你6种做法,简单易学,比外面卖的还好吃
吃什么食物预防干眼症
有人在纳粹毒气室中幸存下来吗?