# qplot入门

qplot是quick plot的缩写，与R系统的基本绘图函数plot类似。

``````# 查看qplot的帮助文档
?qplot
``````

## diamonds数据集

diamonds是ggplot2中的内置数据集，内含54000颗钻石的价格和质量的信息。钻石质量的四个“C”：克拉重量（carat）、切工（cut）、颜色（color）和净度（clarity），五个物理指标：深度（depth）、钻面宽度（table）、x、y和z。

``````## 让样本可重复
> library(ggplot2)
> set.seed(1410)
> dsmall <- diamonds[sample(nrow(diamonds), 100), ]
``````

## qplot绘制散点图

qplot(carat, price, data = diamonds)

qplot(log(carat), log(price), data = diamonds)

qplot(carat, x * y * z, data = diamonds)

qplot可以自动识别分配不同组别的颜色

qplot(carat, price, data = dsmall, colour = color)

qplot(carat, price, data = dsmall, shape = cut)

## 调节图片的透明度

qplot(carat, price, data = diamonds, alpha = I(1/10))

alpha = I(1/10)

qplot(carat, price, data = diamonds, alpha = I(1/100))

alpha = I(1/100)

qplot(carat, price, data = diamonds, alpha = I(1/200))

alpha = I(1/200)

• 二维的变量关系：
qplot的参数geom可以设置绘制图形的种类
geom = "point" 散点图
geom = "smooth" 平滑曲线，展示标准误差
geom = "boxplot" 箱线图
geom = "path"和geom = "line"可以在数据点之间连线
• 一维的变量分布
• 连续变量
geom = "histogram" 直方图
geom = "freqpoly" 频率多边图
geom = "density" 密度曲线
• 离散变量
geom = "bar"

## 添加平滑曲线

qplot(carat, price, data = dsmall, geom = c("point", "smooth"))

dsmall平滑曲线与散点图

qplot(carat, price, data = diamonds, geom = c("point", "smooth"))

diamonds平滑曲线与散点图

qplot(carat, price, data = diamonds, geom = c("point", "smooth"), se = FALSE)

### 当`geom = "smooth"`时

qplot(carat, price, data = dsmall, geom = c("point", "smooth"), span = 0.2)

span = 0.2

qplot(carat, price, data = dsmall, geom = c("point", "smooth"), span = 1)

span = 1

## 箱线图与扰动点图

qplot(color, price/carat, data = diamonds, geom = "jitter")

qplot(color, price/carat, data = diamonds, geom = "boxplot")

### 改变 alpha 的取值

qplot(color, price/carat, data = diamonds, geom = "jitter", alpha = I(1/5))

image.png

qplot(color, price/carat, data = diamonds, geom = "jitter", alpha = I(1/50))

image.png

qplot(color, price/carat, data = diamonds, geom = "jitter", alpha = I(1/200))

image.png

## geom=' density'。

qplot(carat, data = diamonds, geom = "histogram")

qplot(carat, data = diamonds, geom = "density")

### 调整直方图的组距。

qplot(carat, data = diamonds, geom = "histogram", binwidth = 1, xlim = c(0, 3))

binwidth = 1

qplot(carat, data = diamonds, geom = "histogram", binwidth = 0.1, xlim = c(0, 3))

binwidth = 0.1

qplot(carat, data = diamonds, geom = "histogram", binwidth = 0.01, xlim = c(0, 3))

binwidth = 0.01

### 分组比较变量

qplot(carat, data = diamonds, geom = "density", colour = color)

qplot(carat, data = diamonds, geom = "histogram", fill = color)

## 钻石颜色的条形图

qplot(color, data = diamonds, geom = "bar")

qplot(color, data = diamonds, geom = "bar", weight = carat) + scale_y_continuous("carat")

## 时间序列的线条图和路径图

### 使用 geom = 'line' 绘制线条图

qplot(date, unemploy/pop, data = economics, geom = "line")

qplot(date, uempmed, data = economics, geom = "line")

### 使用geom = "path"绘制路径图

qplot(unemploy/pop, uempmed, data = economics, geom = c("point", "path"))

year <- function(x) as.POSIXlt(x)\$year + 1900
qplot(unemploy/pop, uempmed, data = economics, geom = "path", colour = year(date))

## 在一个面板上绘制多个图形

qplot(carat, data = diamonds, facets = color ~ ., geom = "histogram", binwidth = 0.1,
xlim = c(0, 3))

qplot(carat, ..density.., data = diamonds, facets = color ~ ., geom = "histogram", binwidth = 0.1, xlim = c(0, 3))

## 其他参数

• main：设置图形的标题
• xlab, ylab：设置x轴、y轴的标签文字

qplot(carat, price, data = dsmall, xlab = "Price (\$)", ylab = "Weight (carats)", main = "Price-weight relationship")

• xlim, ylim：设置x轴和y轴的显示区间

qplot(carat, price/carat, data = dsmall, ylab = expression(frac(price, carat)), xlab = "Weight (carats)", main = "Small diamonds", xlim = c(0.2, 1))

• log：字符型向量，显示对哪一坐标轴取log，log = "x"对x轴取对数，log = "xy"对x轴和y轴都取对数

qplot(carat, price, data = dsmall, log = "xy")

https://mp.weixin.qq.com/s/E9ykuIbc-2Ja9HOY0bn_6g
B站公益74小时生信工程师教学视频合辑
https://mp.weixin.qq.com/s/IyFK7l_WBAiUgqQi8O7Hxw

https://mp.weixin.qq.com/s/KgbilzXnFjbKKunuw7NVfw