Graphing with R Language

R 语言作图

Author
Affiliation

Dr. Peng Zhao | 赵鹏

Department of Health and Environmental Sciences, School of Science, Xi’an Jiaotong-Liverpool University
西交利物浦大学 理学院 健康与环境科学系

Published

April 7, 2025

1 Preparation | 课前预备

  1. Fundamental knowledge of R language. 具备 R 语言基本常识.
  2. Install R and RStudio IDE Tutorial. 安装 R 和 RStudio IDE: 指南.
  3. Install the following packages. 安装以下扩展包.
Code
install.packages(c('ggplot2', 'GGally', 'MSG'))
  1. Prepare a dataset for practice. 准备自己的一个数据文件,以便课上练习.

2 Objectives | 课程目标

  1. Understand what graphs can be plotted using R language in scientific research. 了解 R 语言在科研领域能绘制哪些图形.
  2. Learn about two common plotting systems in R (namely base R and the ggplot2 package) and two implementation methods (graphical interface and coding). 了解 R 语言作图的两种常见体系(即 base R 和 ggplot2 包)和两种实现手段(即图形界面和代码).
  3. Master the methods of using AI tools to assist in plotting common statistical graphs with R. 掌握以人工智能工具为辅助R语言绘制常见统计图形的方法.

3 AI Tools | 人工智能工具

3.1 Common AI Tools | 常用人工智能工具

3.2 Using AI Tools with R | 人工智能工具在 R 语言的用途

  • Self-learning. 自学.
  • Recommend graphs and generate codes. 选择图形,生成代码.
  • Explain the codes. 解释代码
  • Optimize the graphs. 优化图形

4 What R can Plot | R 能做哪些图

4.1 Scenarios | 从使用场景看

Academic Papers | 学术论文

Presentations | 演示文稿

Interactive | 交互图

Animation | 动画

Posters | 海报

Ubiquitous | 无处不在

4.2 Data Types & Statistical Methods | 从数据类型和统计方法看

5 Two Ways | 两种实现手段

5.1 GUI | 图形界面

Code
library(GrapheR)
run.GrapheR()
Code
library(Rcmdr)
Code
ggplotgui::ggplot_shiny()
Code
library(Deducer)
JGR()

5.2 Codes | 代码

Code
library(ggplot2)
ggplot(cars) + 
  geom_point(aes(speed, dist))

6 Components of Statistical Graphs | 统计图形的构成

  • Graphs | 图形

    • Plotting area | 绘图区
    • Axes | 坐标轴
    • Legends | 图例
  • Numbering and caption | 编号和标题

7 Common Graphs | 常见图形的做法

7.1 Probability density | 概率密度图

Code
curve(dnorm(x), from = -5, to = 5)
segments(c(-1, 1), 0, c(-1, 1), dnorm(c(-1, 1)), 'red')

Code
curve(dt(x, df = 10),  from = -5, to = 5)
abline(h = 0)
abline(v = qnorm(0.95), col = 'red')

Code
curve(dchisq(x, df = 10),  from = 0, to = 30)
abline(h = 0)
abline(v = qchisq(0.95, df = 10), col = 'red')

Distribution p- q- d- r-
Binomial pbinom qbinom dbinom rbinom
Chi-Square pchisq qchisq dchisq rchisq
F pf qf df rf
Logistic plogis qlogis dlogis rlogis
Log Normal plnorm qlnorm dlnorm rlnorm
Normal pnorm qnorm dnorm rnorm
Poisson ppois qpois dpois rpois
Student t pt qt dt rt
Uniform punif qunif dunif runif
Weibull pweibull qweibull dweibull rweibull
Wilcoxon Rank Sum Statistic pwilcox qwilcox dwilcox rwilcox
Wilcoxon Signed Rank Statistic psignrank qsignrank dsignrank rsignrank

7.2 Bar Charts | 条形图,柱状图

Code
data(diet, package = "Epi")
tab1 <- table(diet$job)
barplot(tab1, horiz = TRUE, las = 1)

Code
ggplot(diet) +
  geom_bar(aes(job)) +
  coord_flip()

Code
tab2 <- table(diet[, c('job', 'chd')])
barplot(tab2, xlab = 'CHD', beside = TRUE, col = 1:3,
        legend.text = TRUE, 
        args.legend = list(
          x = 8, y = 100,
          legend = dimnames(tab2)$job,
          bty = 'n', col = 1:3))

Code
ggplot(diet) +
  geom_bar(aes(chd, fill = job), position = 'dodge')

Code
ggplot(diet) +
  geom_bar(aes(chd, fill = job)) +
  facet_grid( ~ energy.grp) # or facet_wrap( ~ energy.grp)

7.3 Histograms, Box/Violin Charts | 直方图,箱线图,小提琴图

Code
hist(diet$weight, freq = FALSE)
lines(density(na.omit(diet$weight)))

Code
ggplot(diet) +
  geom_histogram(aes(x = weight)) + 
  facet_grid(~job)

Code
boxplot(diet$weight ~ diet$job)
diet_mean <- tapply(diet$weight, diet$job, mean, na.rm = TRUE)
points(1:3, diet_mean, pch = 4)

Code
ggplot() + 
  geom_boxplot(aes(job, weight), data = diet) +
  geom_point(aes(1:3, diet_mean))

Code
library(vioplot)
vioplot(diet$weight, horizontal = TRUE)
points(mean(diet$weight, na.rm = TRUE), 1, pch = 4, col = "red")

Code
ggplot(diet, aes(x = weight, y = 0)) +
  geom_violin(fill = 'grey') +
  geom_boxplot(width = 0.1) +
  geom_point(aes(x = mean(diet$weight, na.rm = TRUE), y = 0), pch = 4)

7.4 Scatter plots | 散点图

Code
ggplot(diet, aes(height, weight)) +
  geom_point(alpha = 0.1) +
  # geom_bin2d()
  geom_smooth(method = 'lm') +
  labs(x = 'Height(cm)', y = 'Weight (km)') +
  theme_bw()

8 More | 更多图形

8.1 Base R

Common base R plotting functions
Function Description
barplot() Generate a bar chart.
boxplot() Generate a box plot (also called box-and-whisker plot).
contour() Generate a contour plot.
coplot() Generate a conditional plot.
curve() Generate a curve corresponding to a function.
dotchart() Generate a Cleveland dot plot.
hist() Generate a histogram.
image() Generate a grid of colored or gray-scale rectangles with colors corresponding to the values
matplot() Wrapper of plot() for plotting columns of one matrix against columns of another.
pairs() Generate a matrix of scatterplots.
persp() Generate a perspective plot of a surface
pie() Generate a pie chart.
plot() Generic plotting function, depending on the object’s class.
stripchart() Generate a one dimensional scatter plot.
stars() Generate a star plot or segment diagram of a multivariate data set.
symbols() Generate a plot with symbols (circles, squares, rectangles, stars, thermometers, and boxplots) representing data values.
ts.plot() Plot multiple Time Series.
abline() Add straight lines to a graph.
arrows() Add arrows to a graph.
axis() Add an axis to a graph.
box() Add a surrounding box to a graph.
legend() Add legends to a graph.
lines() Add connected line segments to a graph.
mtext() Add text to the margins of a graph.
points() Add points to a graph.
polygon() Add polygons to a graph.
rect() Add a rectangle to a graph.
rug() Add short lines representing data values to a graph.
segments() Add line segments to a graph.
text() Add text to a graph.

8.2 ggplot2

Common ggplot plotting functions
Function Description
geom_abline(), geom_hline(), geom_vline() Straight lines.
geom_area(), geom_ribbon() Area and ribbon plot.
geom_bar() Bar chart.
geom_histogram(), geom_freqpoly(), geom_bin2d(), geom_hex() 1D/2D histogram or frequency polygons.
geom_boxplot() Box plot.
geom_contour() Contour plot.
geom_crossbar(), geom_errorbar(), geom_errorbarh(), geom_linerange(),geom_pointrange() Data value with intervals.
geom_density(), geom_density2d() Distribution density curve/surface.
geom_line(), geom_path() Line chart.
geom_point() Scatterplot.
geom_polygon(), geom_rect(), geom_tile() Polygons/rectangles.
geom_rug() Rugs.
geom_segment(), geom_curve() Line segments and curves.
geom_smooth(), geom_quantile() Fitted lines.
geom_text() Text.
Code
ggplot() +      # mandatory: data mapping
  geom_xxx() +  # mandatory: geometry shape
  coordinate_xxx() +  
  theme() +          
  facet_xxx() +      
  ...

8.3 Two Examples | 两个例子

Code
library(fecitr)
plot_summary(diet, if_box = TRUE)

library(GGally)
ggpairs(diet)

9 Further Readings | 延伸阅读