R语言画折线图?
测试开头
测试结尾
笔者邀请您,先思考:
1 折线图有什么作用?如何画折线图
折线图(Line chart)是将数据表示为一系列称为“标记”的数据点,数据点之间由线段连接而成。它是类似散点图,除了测量点是有序的且用直线段连接。它是许多领域中常见的基本图表类型。
一 基本折线图
折线图
1# 加载R包
2library(ggplot2)
3
4# 构造数据集
5df <- data.frame(dose=c("D0.5", "D1", "D2"),
6 len=c(4.2, 10, 29.5))
7
8# 1 基本折线图
9ggplot(data=df, aes(x=dose, y=len, group=1)) +
10 geom_line()+
11 geom_point()
12
13# 2 改变线的类型
14ggplot(data=df, aes(x=dose, y=len, group=1)) +
15 geom_line(linetype = "dashed")+
16 geom_point()
17
18# 3 改变线的颜色
19ggplot(data=df, aes(x=dose, y=len, group=1)) +
20 geom_line(color="red")+
21 geom_point()
22
1的图形结果:
2的图形结果:
3的图形结果:
二 折线图上添加箭头
1# 1 添加箭头
2library(grid)
3ggplot(data=df, aes(x=dose, y=len, group=1)) +
4 geom_line(arrow = arrow())+
5 geom_point()
6
7# 2 折线图两端添加箭头
8myarrow <- arrow(angle = 15, ends = "both", type = "closed")
9ggplot(data=df, aes(x=dose, y=len, group=1)) +
10 geom_line(arrow=myarrow)+
11 geom_point()
12
1的图形结果:
2的图形结果:
三 分组折线图
1df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
2 dose=rep(c("D0.5", "D1", "D2"),2),
3 len=c(6.8, 15, 33, 4.2, 10, 29.5))
4#1 多组折线图
5ggplot(data=df2, aes(x=dose, y=len, group=supp)) +
6 geom_line()+
7 geom_point()
8#2 改变直线类型
9ggplot(data=df2, aes(x=dose, y=len, group=supp)) +
10 geom_line(linetype="dashed", color="blue", size=1.2)+
11 geom_point(color="red", size=3)
12
1的图形结果:
2的图形结果:
四 利用分组改变折线的类型
1#1 通过分组改变线的类型
2ggplot(df2, aes(x=dose, y=len, group=supp)) +
3 geom_line(aes(linetype=supp))+
4 geom_point()
5
6#2 通过分组改变线的类型和点的形状
7ggplot(df2, aes(x=dose, y=len, group=supp)) +
8 geom_line(aes(linetype=supp))+
9 geom_point(aes(shape=supp))
1的图形结果:
2的图形结果:
五 使用函数scale_linetype_manual()手动更改线的类型
1# 手动设置线的类型
2ggplot(df2, aes(x=dose, y=len, group=supp)) +
3 geom_line(aes(linetype=supp))+
4 geom_point()+
5 scale_linetype_manual(values=c("twodash", "dotted"))
图形结果:
六 分组改变线的颜色
1# 改变线的颜色
2
3p <- ggplot(df2, aes(x=dose, y=len, group=supp)) +
4 geom_line(aes(color=supp))+
5 geom_point(aes(color=supp))
6p
7
图形结果:
七 利用函数手动改变线的颜色
1# 1 自定义调色板
2p+scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
3# 2 使用现成的调色板
4p+scale_color_brewer(palette="Dark2")
5# 3 使用灰度
6p + scale_color_grey() + theme_classic()
1的图形结果:
2的图形结果:
3的图形结果:
八 改变折线图的图例默认位置
1p <- p + scale_color_brewer(palette="Paired")+
2 theme_minimal()
3# 1 图例在上方
4p + theme(legend.position="top")
5# 2 图例在下方
6p + theme(legend.position="bottom")
7# 3 移除图例
8p + theme(legend.position="none")
1的图形结果:
2的图形结果:
3的图形结果:
九 x轴是连续值的折线图
1df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
2 dose=rep(c("0.5", "1", "2"),2),
3 len=c(6.8, 15, 33, 4.2, 10, 29.5))
4
5# x轴作为连续变量
6df2$dose <- as.numeric(as.vector(df2$dose))
7ggplot(data=df2, aes(x=dose, y=len, group=supp, color=supp)) +
8 geom_line() +
9 geom_point()+
10 scale_color_brewer(palette="Paired")+
11 theme_minimal()
图形结果:
十 x轴是日期的折线图
1head(economics)
2ggplot(data=economics, aes(x=date, y=pop))+
3 geom_line()
图形结果:
十一 带有误差条的折线图
1data_summary <- function(data, varname, groupnames){
2 require(plyr)
3 summary_func <- function(x, col){
4 c(mean = mean(x[[col]], na.rm=TRUE),
5 sd = sd(x[[col]], na.rm=TRUE))
6 }
7 data_sum<-ddply(data, groupnames, .fun=summary_func,
8 varname)
9 data_sum <- rename(data_sum, c("mean" = varname))
10 return(data_sum)
11}
12
13df3 <- data_summary(ToothGrowth, varname="len",
14 groupnames=c("supp", "dose"))
15
16# 1 均值的标准差
17ggplot(df3, aes(x=dose, y=len, group=supp, color=supp)) +
18 geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.1) +
19 geom_line() +
20 geom_point()+
21 scale_color_brewer(palette="Paired")+
22 theme_minimal()
23
24
25# 2 使用position_dodge水平移动重叠的误差条
26ggplot(df3, aes(x=dose, y=len, group=supp, color=supp)) +
27 geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.1,
28 position=position_dodge(0.05)) +
29 geom_line() +
30 geom_point()+
31 scale_color_brewer(palette="Paired")+
32 theme_minimal()
1的图形结果:
2的图形结果:
十二 自定义折线图
1# 1 简单的折线图
2# 通过分组改变点的形状和线的类型
3ggplot(df3, aes(x=dose, y=len, group = supp, shape=supp, linetype=supp))+
4 geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.1,
5 position=position_dodge(0.05)) +
6 geom_line() +
7 geom_point()+
8 labs(title="Plot of lengthby dose",x="Dose (mg)", y = "Length")+
9 theme_classic()
10
11# 2 分组改变颜色和添加误差条
12p <- ggplot(df3, aes(x=dose, y=len, group = supp, color=supp))+
13 geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.1,
14 position=position_dodge(0.05)) +
15 geom_line(aes(linetype=supp)) +
16 geom_point(aes(shape=supp))+
17 labs(title="Plot of lengthby dose",x="Dose (mg)", y = "Length")+
18 theme_classic()
19p + theme_classic() + scale_color_manual(values=c('#999999','#E69F00'))
20
1的图形结果:
2的图形结果:
参考资料:
1 ggplot2折线图
内容推荐
数据人网:数据人学习,交流和分享的平台,诚邀您创造和分享数据知识,共建和共享数据智库。
请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!