R语言入门 | 使用 ggplot2 进行数据可视化

devtools/2024/10/19 2:20:56/

   

1.0准备工作

先下好tidyverse包,并进行加载。
install.packages ( "tidyverse" )
library(tidyverse)
R 包只需安装一次,但每次开始新会话时都要重新加载。

1.1 数据框 

数据框是变量(列)和观测(行)的矩形集合。

下文经常使用mpg 包含了由美国环境保护协会收集的 38 种车型的观测数据。

当你想了解mpg数据框的信息时,可使用 ?<数据框名> 来查阅。

1.2 创建 ggplot 图形

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))

 

ggplot(data = mpg): 会创建一张空白图
函数 geom_point() 向图中添加一 个点层,可以创建一张散点图。
 mapping 参数:定义了如何将数据集中的变量映射为图形属性。
aes() 函数:aes() 函数的 x 参数和 y 参数分别指定了映射到 x 轴的变量与映射到 y 轴的变量。

1.3 绘图模板 

ggplot(data = <DATA>) + 
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))

#<GEOM_FUNCTION>
geom_point散点图

#<MAPPINGS>
x=<变量名>,y=<变量名>,color=<变量名>,shape,size,alpha(透明度)

eg.ggplot(data = diamonds) + geom_point(mapping = aes(x=carat,y=price))

1.4 图形属性映射 

eg.ggplot(data = mpg) + 
geom_point(mapping = aes(x = displ, y = hwy,
color = class))
可以将点的颜色映射为变量class               ^^^^^^^^^^^^^

区分:
#以手动为几何对象设置图形属性(常量)
ggplot(data = mpg) + 
geom_point(mapping = aes(x = displ, y = hwy), color = "blue",shape=21,fill="red")
                                              ^^^^^^^^^^^^^^^^^^^^^^^写在aes外面

1.5 分面 

1.5.1 facet_wrap()
 

eg. ggplot(data = mpg) + 
geom_point(mapping = aes(x = displ, y = hwy)) + 
facet_wrap(~ class, nrow = 2)

          以class来分组,排成2行


1.5.2 facet_grid()


(多一个分类)
eg.ggplot(data = mpg) + 
geom_point(mapping = aes(x = displ, y = hwy)) + 
facet_grid(drv ~ cyl)

1.6 几何对象 

几何对象是图中用来表示数据的几何图形对象。我们经常根据图中使用的几何对象类型来
描述相应的图。例如,条形图使用了条形几何对象,折线图使用了直线几何对象,箱线图
使用了矩形和直线几何对象。
#geom_point散点图
#geom_smooth平滑曲线图
#geom_bar条形图


#可以叠加使用
eg.ggplot(data = mpg) + 
+     geom_smooth(mapping = aes(x = displ, y = hwy)) + 
+     geom_point(mapping = aes(x = displ, y = hwy))

#在geom_smooth平滑曲线图中,可以按照不同的线型绘制出不同的曲线,每条曲线对应映射到线型的
变量的一个唯一值:
ggplot(data = mpg) + 
geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv))
linetype线性
group
color
#不想要示例图
show.legend=FALSE  位置:和mapping并列
#不想要质性区间
se=FALSE  位置:和mapping并列

1.7 统计变换 

1.8 位置调整 

ggplot(data = diamonds) + 
geom_bar( 
    mapping = aes(x = cut, fill = clarity), 

    position = "dodge" 
  )


position参数

dodge分开排


identity叠着排,实际高度


默认 堆叠着排


jitter(适用范围:散点图)
position = "jitter"为每个数据点添加一个很小的随机扰动,这样就可以将重叠的点分散开:
ggplot(data = mpg) + 
geom_point( 
    mapping = aes(x = displ, y = hwy), 
    position = "jitter" 
  )

  

对比没有使用jitter的:

画盒图


ggplot(data = mpg,mapping = aes(x = class, y = hwy)) + 
geom_boxplot( aes(fill=class))

1.9 坐标系 

1.10 图形分层语法 

#图形属性映射

#分面

#几何对象  


几何对象是图中用来表示数据的几何图形对象。我们经常根据图中使用的几何对象类型来
描述相应的图。例如,条形图使用了条形几何对象,折线图使用了直线几何对象,箱线图
使用了矩形和直线几何对象。


#geom_point散点图
#geom_smooth平滑曲线图
#geom_bar条形图


#可以叠加使用
eg.ggplot(data = mpg) + 
+     geom_smooth(mapping = aes(x = displ, y = hwy)) + 
+     geom_point(mapping = aes(x = displ, y = hwy))

#在geom_smooth平滑曲线图中,可以按照不同的线型绘制出不同的曲线,每条曲线对应映射到线型的
变量的一个唯一值:
ggplot(data = mpg) + 
geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv))
linetype线性
group
color
#不想要示例图
show.legend=FALSE  位置:和mapping并列
#不想要质性区间
se=FALSE  位置:和mapping并列

#简化
全局映射
ggplot是全局函数
而geom_point等是局部函数
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
geom_point(mapping = aes(color = class)) + 
geom_smooth()

#筛选
data = filter(数据集, class == 变量名)
eg.
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
geom_point(mapping = aes(color = class)) + 
geom_smooth( 
    data = filter(mpg, class == "subcompact"), 
    se 
= FALSE 
  )

#条形图
geom_bar
ggplot(data = diamonds) + 
geom_bar(mapping = aes(x = cut))   不用写y轴  

#统计变换函数


stat_count(可替换geom_bar)
ggplot(data = diamonds) + 
stat_count(mapping = aes(x = cut))

#如果是统计过的数据
ggplot(data = demo) + 
geom_bar( 
    mapping = aes(x = a, y = b), stat = "identity" 
  )
  
  
#显示一张表示比例(而不是计数)的条形图:
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 >>>>>中位数,这里也可以改成mean,看均值
  )
  
#为条形图上色
 ggplot(data = diamonds) + 
geom_bar(mapping = aes(x = cut, color = cut)) 
ggplot(data = diamonds) + 
geom_bar(mapping = aes(x = cut, fill = cut)) //fill明显更常用

#映射
ggplot(data = diamonds) + 
+     geom_bar(mapping = aes(x = cut, fill = color)) 

#位置调整


ggplot(data = diamonds) + 
geom_bar( 
    mapping = aes(x = cut, fill = clarity), 
    position = "dodge" 
  )
position参数
dodge分开排
identity叠着排,实际高度
默认 堆叠着排
jitter(散点图用)
position = "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))
    

#旋转坐标系  coord_flip()


ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + 
geom_boxplot() + 
coord_flip()

#绘制空间数据  geom_polygon()


nz <- map_data("nz") //取出新西兰地图
ggplot(nz, aes(long, lat, group = group)) + 
geom_polygon(fill = "white", color = "black") + 
coord_quickmap     coord_quickmap函数可以为地图设置合适的纵横比

#画鸡冠图

coord_polar()画极坐标

1.coord_polar(theta="x")

p<-ggplot(data = diamonds) +   geom_bar(mapping = aes(x = cut, fill = cut))+coord_polar()

2.coord_polar(theta="y") 

p<-ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = cut,width=1))+coord_polar(theta="y")

#分布进行,把命令储存到变量,可进行叠加

eg.

bar <- ggplot(data = diamonds) +
 geom_bar(
 mapping = aes(x = cut, fill = cut),
 show.legend = FALSE,
 width = 1
 ) +
 theme(aspect.ratio = 1) +
 labs(x = NULL, y = NULL)

bar + coord_flip()

bar + coord_polar()//鸡冠图

 show.legend = FALSE:删除图例

width=1:width越大,图挨得越近,等于1时,挨在一起

 theme(aspect.ratio = 1):宽高比为1,更圆

 labs(x = NULL, y = NULL):去除标签注释

频率分布图geom_freqpoly()

 ggplot(data = diamonds, mapping = aes(x = price)) +
+     geom_freqpoly(binwidth = 10)


http://www.ppmy.cn/devtools/45227.html

相关文章

oracle 12c DB卸载流程

1.运行卸载程序 [rootprimary1 ~]# su - oracle [oracleprimary1 ~]$ cd $ORACLE_HOME/deinstall [oracleprimary1 deinstall]$ ./deinstall Checking for required files and bootstrapping ... Please wait ... 这里选择3 、回车、y、y、回车、ASM 这里输入y 2.删除相关目录…

Kotlin 数据类

文章目录 定义编译器自动重写、生成方法 定义 Kotlin 数据类可以用于存储数据&#xff08;当然&#xff0c;不是说普通类不行&#xff09;。数据类使用data class定义&#xff1a; data class Book(val name: String, val author: String)数据类必须有主构造方法&#xff0c;…

Jupyter Notebook快速搭建

Jupyter Notebook why Jupyter Notebook Jupyter Notebook 是一个开源的 Web 应用程序&#xff0c;允许你创建和分享包含实时代码、方程、可视化和解释性文本的文档。其应用包括&#xff1a;数据清洗和转换、数值模拟、统计建模、数据可视化、机器学习等等。 Jupyter Notebo…

【C++ ——— 继承】

文章目录 继承的概念即定义继承概念继承定义定义格式继承关系和访问限定符继承基类成员访问方式的变化 基类对象和派生类对象的赋值转换继承中的作用域派生类中的默认成员函数继承与友元继承与静态成员菱形继承虚继承解决数据冗余和二义性的原理继承的总结继承常见笔试面试题 继…

基于springboot实现青年公寓服务平台系统项目【项目源码+论文说明】计算机毕业设计

基于springboot实现青年公寓服务平台系统演示 摘要 传统信息的管理大部分依赖于管理人员的手工登记与管理&#xff0c;然而&#xff0c;随着近些年信息技术的迅猛发展&#xff0c;让许多比较老套的信息管理模式进行了更新迭代&#xff0c;房屋信息因为其管理内容繁杂&#xff…

算法(十一)贪婪算法

文章目录 算法简介算法概念算法举例 经典问题 -背包问题 算法简介 算法概念 贪婪算法&#xff08;Greedy&#xff09;是一种在每一步都采取当前状态下最好的或者最优的选择&#xff0c;从而希望导致结果也是全局最好或者最优的算法。贪婪算法是当下局部的最优判断&#xff0c…

ES 生命周期管理

一 .概念 ILM定义了四个生命周期阶段&#xff1a;Hot&#xff1a;正在积极地更新和查询索引。Warm&#xff1a;不再更新索引&#xff0c;但仍在查询。cold&#xff1a;不再更新索引&#xff0c;很少查询。信息仍然需要可搜索&#xff0c;但是如果这些查询速度较慢也可以。Dele…

The book

Deep Learning for Coders with Fastai and PyTorch: AI Applications Without a PhD is the book that forms the basis for this course. We recommend reading the book as you complete the course. There’s a few ways to read the book – you can buy it as a paper bo…