ggplot2绘图学习 line plot
之前我们学习了ggplot绘制单变量,两个连续变量的图形,两个离散型变量。对于一个离散型变量,一个连续型变量,有很多作图方式,包括箱图,点图等等
· geom_boxplot() for box plot
· geom_violin() for violin plot
· geom_dotplot() for dot plot
· geom_jitter() for stripchart
· geom_line() for line plot
· geom_bar() for bar plot
今天我们介绍一下line plot
library(ggplot2)
主要函数及参数
· Key functions: geom_line(), geom_step(), geom_path()
· Key arguments to customize the plot: alpha, color, linetype and size.
绘图数据
df <- data.frame(dose=c("D0.5", "D1", "D2"),
len=c(4.2, 10, 29.5))
head(df)
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)
基本图形
p <- ggplot(data=df, aes(x=dose, y=len, group=1))
# 线加点
p + geom_line() + geom_point()
# 改变线的类型和颜色
p + geom_line(linetype = "dashed", color = "steelblue")+
geom_point(color = "steelblue")
# 用geom_step()函数
p + geom_step() + geom_point()
多组线图
p <- ggplot(df2, aes(x=dose, y=len, group=supp))
# 不同组不同线的类型和点的形状
p + geom_line(aes(linetype = supp)) +
geom_point(aes(shape = supp))
# 增加颜色
p + geom_line(aes(linetype=supp, color = supp))+
geom_point(aes(shape=supp, color = supp))
# 手动设置颜色
p + geom_line(aes(linetype=supp, color = supp))+
geom_point(aes(shape=supp, color = supp)) +
scale_color_manual(values=c("#999999", "#E69F00"))
以数值型变量/分类变量为x轴
df3 <- 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(df3)
# 连续性变量
df3$dose <- as.numeric(as.vector(df3$dose))
ggplot(data=df3, aes(x = dose, y = len, group = supp, color = supp)) +
geom_line() + geom_point()
# 离散变量
df2$dose<-as.factor(df3$dose)
ggplot(data=df2, aes(x = dose, y = len, group = supp, color = supp)) +
geom_line() + geom_point()
可见X轴的效果以及图形的效果都是不一样的
X轴时间序列
head(economics)
以时间为X轴
ggplot(data=economics, aes(x = date, y = pop))+
geom_line()
对时间进行筛选(2006年之后)
ss <- subset(economics, date > as.Date("2006-1-1"))
ggplot(data = ss, aes(x = date, y = pop)) + geom_line()
给线的粗细添加变量
ggplot(data = economics, aes(x = date, y = pop, size = unemploy/pop)) +
geom_line()
多个时间序列
ggplot(economics, aes(x=date)) +
geom_line(aes(y = psavert), color = "darkred") +
geom_line(aes(y = uempmed), color="steelblue", linetype="twodash") +
theme_minimal()
require(reshape2)
df <- melt(economics[, c("date", "psavert", "uempmed")], id="date")
ggplot(df, aes(x = date, y = value)) +
geom_line(aes(color = variable), size=1) +
scale_color_manual(values = c("#999999", "#E69F00")) +
theme_minimal()
面积图展示
ggplot(economics, aes(x=date)) +
geom_area(aes(y=psavert), fill = "#999999",
color = "#999999", alpha=0.5) +
geom_area(aes(y=uempmed), fill = "#E69F00",
color = "#E69F00", alpha=0.5) +
theme_minimal()
单基因泛癌分析
TCGA单基因免疫相关泛癌分析(应要求,对出图添加更细致的描述)
资源贴
赞 (0)