目录
R语言之可视化①误差棒
R语言之可视化②点图
R语言之可视化③点图续
R语言之可视化④点韦恩图upsetR
R语言之可视化⑤R图形系统
R语言之可视化⑥R图形系统续
R语言之可视化⑦easyGgplot2散点图
R语言之可视化⑧easyGgplot2散点图续
R语言之可视化⑨火山图
R语言之可视化⑩坐标系统
R语言之可视化①①热图绘制heatmap
R语言之可视化①②热图绘制2
R语言之可视化①③散点图 拟合曲线
R语言之可视化①④一页多图(1)
R语言之可视化①⑤ROC曲线
R语言之可视化①⑥一页多图(2)
R语言之可视化①⑦调色板
R语言之可视化①⑧子图组合patchwork包
R语言之可视化①⑨之ggplot2中的图例修改
R语言之可视化(20)之geom_label()和geom_text()
R语言之可视化(21)令人眼前一亮的颜色包
R语言之可视化(22)绘制堆积条形图
R语言之可视化(23)高亮某一元素
R语言之可视化(24)生成带P值得箱线图
R语言之可视化(25)绘制相关图(ggcorr包)
R语言之可视化(26)ggplot2绘制饼图
R语言之可视化(27)ggplot2绘制线图
本文主要表达如何使用ggplot2绘制线图。线图一般表达的目的是:某个因变量随着自变量改变而变化的趋势。因变量可以为数值型变量或者分类变量。可供选的函数有: geom_line(), geom_step(), geom_path()
举例来说:因变量可以是
- date :时间类型数据
- texts:文字类型数据
- discrete numeric values:离散型数值
- continuous numeric values:连续性数值
基本线图
数据
数据来源于 ToothGrowth 数据集
代码语言:javascript复制df <- data.frame(dose=c("D0.5", "D1", "D2"),
len=c(4.2, 10, 29.5))
head(df)
代码语言:javascript复制## dose len
## 1 D0.5 4.2
## 2 D1 10.0
## 3 D2 29.5
- len : Tooth length
- dose : Dose in milligrams (0.5, 1, 2)
带点线图
代码语言:javascript复制library(ggplot2)
# Basic line plot with points
ggplot(data=df, aes(x=dose, y=len, group=1))
geom_line()
geom_point()
# Change the line type
ggplot(data=df, aes(x=dose, y=len, group=1))
geom_line(linetype = "dashed")
geom_point()
# Change the color
ggplot(data=df, aes(x=dose, y=len, group=1))
geom_line(color="red")
geom_point()
你可以添加一个箭头
代码语言:javascript复制library(grid)
# Add an arrow
ggplot(data=df, aes(x=dose, y=len, group=1))
geom_line(arrow = arrow())
geom_point()
# Add a closed arrow to the end of the line
myarrow=arrow(angle = 15, ends = "both", type = "closed")
ggplot(data=df, aes(x=dose, y=len, group=1))
geom_line(arrow=myarrow)
geom_point()
同样也可以用geom_step() or geom_path()将数值连接起来
代码语言:javascript复制ggplot(data=df, aes(x=dose, y=len, group=1))
geom_step()
geom_point()
ggplot(data=df, aes(x=dose, y=len, group=1))
geom_path()
geom_point()
- geom_line : 根据X轴数值连接
- geom_path() : 根据初始数值连接
- geom_step : 通过阶梯连接起来
多分组线图
数据
代码语言:javascript复制df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("D0.5", "D1", "D2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))
head(df2)
代码语言:javascript复制## supp dose len
## 1 VC D0.5 6.8
## 2 VC D1 15.0
## 3 VC D2 33.0
## 4 OJ D0.5 4.2
## 5 OJ D1 10.0
## 6 OJ D2 29.5
- len : Tooth length
- dose : Dose in milligrams (0.5, 1, 2)
- supp : Supplement type (VC or OJ) 如下图所示:通过不同的分组,绘制了两个线图
# Line plot with multiple groups
ggplot(data=df2, aes(x=dose, y=len, group=supp))
geom_line()
geom_point()
# Change line types
ggplot(data=df2, aes(x=dose, y=len, group=supp))
geom_line(linetype="dashed", color="blue", size=1.2)
geom_point(color="red", size=3)
不同分组使用不同的类型的线
代码语言:javascript复制# Change line types by groups (supp)
ggplot(df2, aes(x=dose, y=len, group=supp))
geom_line(aes(linetype=supp))
geom_point()
# Change line types and point shapes
ggplot(df2, aes(x=dose, y=len, group=supp))
geom_line(aes(linetype=supp))
geom_point(aes(shape=supp))
也可以通过 scale_linetype_manual()手段设置线的类型
代码语言:javascript复制# Set line types manually
ggplot(df2, aes(x=dose, y=len, group=supp))
geom_line(aes(linetype=supp))
geom_point()
scale_linetype_manual(values=c("twodash", "dotted"))
不同分组,绘制不同颜色的线
代码语言:javascript复制p<-ggplot(df2, aes(x=dose, y=len, group=supp))
geom_line(aes(color=supp))
geom_point(aes(color=supp))
p
也可以通过 change manually line colors 手动设置线的颜色
- scale_color_manual() : to use custom colors
- scale_color_brewer() : to use color palettes from RColorBrewer package
- scale_color_grey() : to use grey color palettes
# Use custom color palettes
p scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# Use brewer color palettes
p scale_color_brewer(palette="Dark2")
# Use grey scale
p scale_color_grey() theme_classic()
改变图例(legend)位置
代码语言:javascript复制p <- p scale_color_brewer(palette="Paired")
theme_minimal()
p theme(legend.position="top")
p theme(legend.position="bottom")
# Remove legend
p theme(legend.position="none")
legend.position 可选的选项有 : “left”,“top”, “right”, “bottom”.
绘制X轴为数值型的线图
代码语言:javascript复制# Create some data
df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("0.5", "1", "2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))
head(df2)
代码语言:javascript复制## supp dose len
## 1 VC 0.5 6.8
## 2 VC 1 15.0
## 3 VC 2 33.0
## 4 OJ 0.5 4.2
## 5 OJ 1 10.0
## 6 OJ 2 29.5
代码语言:javascript复制# x axis treated as continuous variable
df2$dose <- as.numeric(as.vector(df2$dose))
ggplot(data=df2, aes(x=dose, y=len, group=supp, color=supp))
geom_line() geom_point()
scale_color_brewer(palette="Paired")
theme_minimal()
# Axis treated as discrete variable
df2$dose<-as.factor(df2$dose)
ggplot(data=df2, aes(x=dose, y=len, group=supp, color=supp))
geom_line() geom_point()
scale_color_brewer(palette="Paired")
theme_minimal()
!](https://upload-images.jianshu.io/upload_images/9218360-42ec840eec7b60de.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240 "ggplot2 line plot - R software and data visualization")
当X轴为时间类型数据
代码语言:javascript复制head(economics)
代码语言:javascript复制## date pce pop psavert uempmed unemploy
## 1 1967-06-30 507.8 198712 9.8 4.5 2944
## 2 1967-07-31 510.9 198911 9.8 4.7 2945
## 3 1967-08-31 516.7 199113 9.0 4.6 2958
## 4 1967-09-30 513.3 199311 9.8 4.9 3143
## 5 1967-10-31 518.5 199498 9.7 4.7 3066
## 6 1967-11-30 526.2 199657 9.4 4.8 3018
代码语言:javascript复制# Basic line plot
ggplot(data=economics, aes(x=date, y=pop))
geom_line()
# Plot a subset of the data
ggplot(data=subset(economics, date > as.Date("2006-1-1")),
aes(x=date, y=pop)) geom_line()
修改线的大小
代码语言:javascript复制# Change line size
ggplot(data=economics, aes(x=date, y=pop, size=unemploy/pop))
geom_line()
绘制带有误差棒的线图
代码语言:javascript复制#
# Function to calculate the mean and the standard deviation
# for each group
#
# data : a data frame
# varname : the name of a column containing the variable
#to be summariezed
# groupnames : vector of column names to be used as
# grouping variables
data_summary <- function(data, varname, groupnames){
require(plyr)
summary_func <- function(x, col){
c(mean = mean(x[[col]], na.rm=TRUE),
sd = sd(x[[col]], na.rm=TRUE))
}
data_sum<-ddply(data, groupnames, .fun=summary_func,
varname)
data_sum <- rename(data_sum, c("mean" = varname))
return(data_sum)
}
代码语言:javascript复制df3 <- data_summary(ToothGrowth, varname="len",
groupnames=c("supp", "dose"))
head(df3)
代码语言:javascript复制## supp dose len sd
## 1 OJ 0.5 13.23 4.459709
## 2 OJ 1.0 22.70 3.910953
## 3 OJ 2.0 26.06 2.655058
## 4 VC 0.5 7.98 2.746634
## 5 VC 1.0 16.77 2.515309
## 6 VC 2.0 26.14 4.797731
函数 geom_errorbar()可以用来绘制带有误差棒的线图
代码语言:javascript复制# Standard deviation of the mean
ggplot(df3, aes(x=dose, y=len, group=supp, color=supp))
geom_errorbar(aes(ymin=len-sd, ymax=len sd), width=.1)
geom_line() geom_point()
scale_color_brewer(palette="Paired") theme_minimal()
# Use position_dodge to move overlapped errorbars horizontally
ggplot(df3, aes(x=dose, y=len, group=supp, color=supp))
geom_errorbar(aes(ymin=len-sd, ymax=len sd), width=.1,
position=position_dodge(0.05))
geom_line() geom_point()
scale_color_brewer(palette="Paired") theme_minimal()
自定义线图的一些例子
代码语言:javascript复制# Simple line plot
# Change point shapes and line types by groups
ggplot(df3, aes(x=dose, y=len, group = supp, shape=supp, linetype=supp))
geom_errorbar(aes(ymin=len-sd, ymax=len sd), width=.1,
position=position_dodge(0.05))
geom_line()
geom_point()
labs(title="Plot of lengthby dose",x="Dose (mg)", y = "Length")
theme_classic()
# Change color by groups
# Add error bars
p <- ggplot(df3, aes(x=dose, y=len, group = supp, color=supp))
geom_errorbar(aes(ymin=len-sd, ymax=len sd), width=.1,
position=position_dodge(0.05))
geom_line(aes(linetype=supp))
geom_point(aes(shape=supp))
labs(title="Plot of lengthby dose",x="Dose (mg)", y = "Length")
theme_classic()
p theme_classic() scale_color_manual(values=c('#999999','#E69F00'))
代码语言:javascript复制p scale_color_brewer(palette="Paired") theme_minimal()
# Greens
p scale_color_brewer(palette="Greens") theme_minimal()
# Reds
p scale_color_brewer(palette="Reds") theme_minimal()