library(tidyverse)
基础作图
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
分别设好 x y轴及数据集即可
加颜色
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))
大小
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, size = class))
透明度和形状
# 将车型class映射给透明度
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, alpha = class))
# 将车型class映射给形状
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, shape = class))
(4)手动设置图形属性
# 例:所有点设为蓝色
# (注意:color="blue"在aes()外)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
(5)stroke-轮廓
# 适用于散点图,21-24号形状
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, stroke = 3),shape=21)
5.分面
# (1)依据单个变量分面 facet_wrap() #分子图
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2) #分两行展示
# nrow指定分面后显示几行
# ncol指定分面后显示几列
# 注意~分面依据必须是离散型变量。
(2)依据两个变量分面 facet_grid()
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ cyl)
# 不需要指定nrow和ncol。
(3)不想在行或列维度中分面,用.代替变量名
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(. ~ cyl)
6.几何对象
# 也就是图的不同类型,如点图、折线图、直方图等。
# (1)理解分组
#
# 将一个图形属性映射为一个离散型变量,ggplot2就会自动对数据进行分组来绘制多个几何对象。这种形式是隐式分组,不需要添加图例和区分特征。
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy))
# 将线性映射为drv(驱动方式,d,f,4)就会自动变成三条线型不同的线。
# 将颜色映射为drv,就会自动变成三条颜色不用的线。
#分组
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy, group = drv))
隐式分组-线型
ggplot(data = mpg) +
geom_smooth(
mapping = aes(x = displ, y = hwy, linetype = drv),
)
隐式分组-颜色
ggplot(data = mpg) +
geom_smooth(
mapping = aes(x = displ, y = hwy, color = drv),
show.legend = FALSE #不显示图例
)
(2)同一张图显示多个几何对象--局部映射和全局映射
# --这里涉及到图层啦。
# 局部映射-映射只对改图层有效
#
# 有多个几何对象时,映射语句要重复多次,又丑又麻烦。
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
geom_smooth(mapping = aes(x = displ, y = hwy))
![image.png](https://upload-images.jianshu.io/upload_images/19009296-37dad55eb1601c71.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#全局映射--对所有图层生效
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point() +
geom_smooth()
![image.png](https://upload-images.jianshu.io/upload_images/19009296-7d66299bb919081e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
# 局部映射与全局映射冲突时,服从局部映射。
例如:
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth(data = filter(mpg, class == "subcompact"), se = FALSE)
![image.png](https://upload-images.jianshu.io/upload_images/19009296-6e61517d84c433fe.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
# 如果出现报错,请library(dplyr) 或library(tidyverse)
# Error in class == "subcompact" : comparison (1) is possible only for atomic and list types
# 这个报错是因为filter函数出自dplyr包,不加载就不能用~
# ps:关于se=FALSE
#
# se是standard error的缩写,se参数为拟合曲线添加标准误差带,也就是那个灰不啦叽的灰色背景带,默认是TRUE。
参考
[R数据科学--详解ggplot2](https://mp.weixin.qq.com/s?__biz=MzU4NjU4ODQ2MQ==&mid=2247484642&idx=1&sn=182a3a95a2dca006bdcb977bb8f53763&chksm=fdf84ca0ca8fc5b6a1982341ba0c659554c669146f75438f7a3c39eaac9171679d66059640ab&scene=21#wechat_redirect)