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