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)
热门推荐
经常吃木瓜的好处 木瓜吃多了会怎样
故宫博物院镇馆之宝欣赏
苹果手机画中画功能开启方法详解
木瓜好吃吗?甜香多汁还是苦涩硬邦?你的口味说了算!
甜蜜惩罚:增进情侣感情的调皮游戏
经济适用房申请条件及优缺点详解
如何查询房产性质信息?这些信息对房产交易有何影响?
零碳排放绿色制氢技术研究取得新突破
肱骨外科颈骨折治疗
软卧和硬卧到底有啥区别?看完明白原因,难怪价格贵这么多
Unity 2D物理系统:刚体组件详解
脑卒中后,大脑真的无法恢复吗?科学告诉你答案!
滴定管使用指南:为什么要润洗?碱式与酸式有何区别?
哮喘夜间发作怎么办
哈森股份跨界“果链”后股民跑步入局,“牛散”提前一年押注
马卡:安东尼在贝蒂斯2周就已赢得信任 佩工告诉他无需做花哨动作
儿童脑部受伤有何症状
太公分猪肉:从祭祖习俗到文化符号
重载铁路运输技术详解:从编组方式到同步控制
牛奶的功效与作用:从补充蛋白质到滋养皮肤
在 Mac 上使用多点触控手势
银行的操作风险如何防范?
哈森股份拟调整重大资产重组方案停牌,股价提前收获“两连板”
AI来写高考作文,能写出什么花来?
如何写一封无人回复后的跟进邮件(内含模板)
战略市场细分:揭秘赢在起跑线的竞争策略
什么是发动机机油滤芯
从冲突到共赢:客服应对难缠客户的策略与技巧
美国主要银行开户条件全面解析:如何在美国顺利开设银行账户
赶紧跑,有以下5种特征,十有八九是串串房!