ggplot2 002 快速绘图qplot()

http://www.sthda.com/english/wiki/ggplot2-essentials

1.介绍

ggplot2是功能强大且灵活的R包,由Hadley Wickham实施,用于产生精美的图形。
ggplot2将绘图分为三个不同的基本部分:Plot = data + Aesthetics + Geometry(绘图 = 数据+美学+几何)
每个图的主要成分可以定义如下:

  • data是一个数据框(data.frame)
  • Aesthetics用于指示x和y变量。 它也可以用来控制颜色,点的大小或形状,条的高度等。
  • Geometry定义了图形的类型(直方图,箱形图,线形图,密度图,点图等)。

ggplot2软件包中有两个主要函数:

  • qplot()代表快速绘图,可用于生成简单的绘图。
  • ggplot()函数比qplot更加灵活和健壮,可用于逐段构建绘图。

2.安装及加载ggplot2

# Installation
install.packages('ggplot2')
# Loading
library(ggplot2)

3.数据准备

# Load the data
data(mtcars)
df <- mtcars[, c("mpg", "cyl", "wt")]
head(df)

4.qplot()绘图

函数qplot()与R base软件包中的基本绘图函数plot()函数非常相似,它可用于创建和轻松组合不同类型的图。 但是它仍然不如函数ggplot()灵活。
qplot(): Quick plot with ggplot2

4.1.1 用法

qplot( x, y, ..., data, facets = NULL, margins = FALSE, geom = "auto", xlim = c(NA, NA), ylim = c(NA, NA), log = "", main = NULL, xlab = NULL, ylab = NULL, asp = NA, stat = NULL, position = NULL )

quickplot( x, y, ..., data, facets = NULL, margins = FALSE, geom = "auto", xlim = c(NA, NA), ylim = c(NA, NA), log = "", main = NULL, xlab = NULL, ylab = NULL, asp = NA, stat = NULL, position = NULL )

4.1.2 散点图 Scatter plots

基本散点图
# qplot()
# 散点图
# Use data from numeric vectors
x <- 1:10; y = x*x
# Basic plot
p1 <- qplot(x,y)
# Add line
p2 <- qplot(x, y, geom=c("point", "line"))
# Use data from a data frame
p3 <- qplot(mpg, wt, data=mtcars)
# 此处为了演示方便,将p1,p2,p3横向组合在一张图上,使用ggpubr包的ggarrange函数
library(ggpubr)
ggarrange(p1,p2,p3,nrow = 1)
image.png
带有平滑线的散点图
qplot(mpg, wt, data = mtcars, geom = c("point", "smooth"))
image.png
按组添加平滑线
# Linear fits by group
qplot(mpg, wt, data = mtcars, color = factor(cyl),
      geom=c("point", "smooth"))
image.png
更改散点图颜色
# Change the color by a continuous numeric variable
p4 <- qplot(mpg, wt, data = mtcars, colour = cyl)
# Change the color by groups (factor)
df <- mtcars
df[,'cyl'] <- as.factor(df[,'cyl'])
p5 <- qplot(mpg, wt, data = df, colour = cyl)
# Add lines
p6 <- qplot(mpg, wt, data = df, colour = cyl,
      geom=c("point", "line"))
# 使用需要加载library(ggpubr)
ggarrange(p4,p5,p6,nrow = 1)
image.png
更改点的形状和大小
# Change the size of points according to 
# the values of a continuous variable
p7 <- qplot(mpg, wt, data = mtcars, size = mpg)
# Change point shapes by groups
p8 <- qplot(mpg, wt, data = mtcars, shape = factor(cyl))
ggarrange(p7,p8)
image.png
给散点图添加文本
qplot(mpg, wt, data = mtcars, label = rownames(mtcars), 
      geom=c("point", "text"),
      hjust=0, vjust=0)
image.png

4.1.3 箱线图,点图和小提琴图

# 检查数据
head(PlantGrowth)
# Basic box plot from a numeric vector
x <- "1"
y <- rnorm(100)
p9 <- qplot(x, y, geom="boxplot")
# Basic box plot from data frame
p10 <- qplot(group, weight, data = PlantGrowth, 
      geom=c("boxplot"))
# Dot plot
p11 <- qplot(group, weight, data = PlantGrowth, 
      geom=c("dotplot"), 
      stackdir = "center", binaxis = "y")
# Violin plot
p12 <- qplot(group, weight, data = PlantGrowth, 
      geom=c("violin"), trim = FALSE)
ggarrange(p9,p10,p11,p12,nrow=2,ncol = 2)
image.png
更改颜色
# Box plot from a data frame
# Add jitter and change fill color by group
p13 <- qplot(group, weight, data = PlantGrowth, 
      geom=c("boxplot", "jitter"), fill = group)
# Dot plot
p14 <- qplot(group, weight, data = PlantGrowth, 
      geom = "dotplot", stackdir = "center", binaxis = "y",
      color = group, fill = group)
ggarrange(p13,p14)
image.png

4.1.4 直方图和密度图

直方图
set.seed(1234)
mydata = data.frame(
  sex = factor(rep(c("F", "M"), each=200)),
  weight = c(rnorm(200, 55), rnorm(200, 58)))
head(mydata)
# Basic histogram
p15 <- qplot(weight, data = mydata, geom = "histogram")
# Change histogram fill color by group (sex)
p16 <- qplot(weight, data = mydata, geom = "histogram",
      fill = sex)
ggarrange(p15,p16)
image.png
密度图
set.seed(1234)
mydata = data.frame(
  sex = factor(rep(c("F", "M"), each=200)),
  weight = c(rnorm(200, 55), rnorm(200, 58)))
head(mydata)
# Basic density plot
p17 <- qplot(weight, data = mydata, geom = "density")
# Change density plot line color by group (sex)
# change line type
p18 <-qplot(weight, data = mydata, geom = "density",
      color = sex, linetype = sex)
ggarrange(p17,p18)
image.png

4.1.5 主要标题和轴标签

qplot(weight, data = mydata, geom = "density",
      xlab = "Weight (kg)", ylab = "Density", 
      main = "Density plot of Weight")
image.png

你可能感兴趣的:(ggplot2 002 快速绘图qplot())