折线图可以反映某种现象的趋势。通常折线图的横坐标是时间变量,纵坐标则是一般的数值型变量。当然,折线图也允许横纵坐标为离散型和数值型。

1.绘制单条折线图
有关时间序列的折线图

library(ggplot2)
library(lubridate) # 处理日期时间相关的R包
Year <- year(seq(from = as.Date("2006-01-01"),to = as.Date("2015-01-01"),by = "year"))
Weight <- c(23,35,43,57,60,62,63,66,61,62)
df <- data.frame(Tear = Year,Weight = Weight)

# x轴为原始变量
ggplot(df,aes(x = Year,y = Weight,group = 1))+
    geom_line()+xlab("Year")
# x轴处理成因子变量
ggplot(df,aes(x = factor(Year),y = Weight,group = 1))+
  geom_line()+xlab("Year")

本例中Year为连续型变量,把它处理成因子型变量,然后当作分类变量来处理;当x为分类变量时,用命令aes(group = )确保ggplot2知道数据点属于同一个分组,否则默认分成所对应的类,出现好几条折线。

有关离散变量的折线图

type <- c("A","B","C","D","E")
quanlity <- c(1,1.1,2.1,1.5,1.7)
df <- data.frame(type = type,quanlity = quanlity)
ggplot(df,aes(x = type,y = quanlity,group = 1))+
  geom_line()

有关连续变量的折线图

set.seed(1)
times <- 1:5
value <- runif(15,min = 5,max = 15)
df <- data.frame(times = times,value = value)
ggplot(data = df,aes(x = times,y = value))+
  geom_line()

以上三段代码中,重要的不同之处为:前两段代码都有”group = “,这样做是因为横坐标将连续型的年份和离散型的字符都转换为因子,如果不添加”group = 1”这样的条件,绘图将会报错。

向折线图中添加点标记(加重点的符号)
当数据点密度比较小或采集间隔分布不均匀时,为折线图做上标记将会产生非常好的效果。只需在折线图的基础上再加上geom_point()函数即可。

set.seed(112)
year <- c(1990,1995,2000,2003,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015)
value <- runif(15,min = 10,max = 50)
df <- data.frame(year = year,value = value)
ggplot(df,aes(x = year,y = value))+
  geom_line()+
  geom_point()

2.绘制多条折线图
对于两条或两个以上的折线图该如何绘制?可以用映射分组的办法来绘多条折线图

基于颜色的多条折线图

set.seed(112)
year <- rep(1990:2015,times = 2)
type <- rep(c("A","B"),each = 26)
value <- c(runif(26),runif(26,min = 1,max = 1.5))
df <- data.frame(year = year,type = type,value = value)

ggplot(df,aes(x = year,y = value,colour = type))+
  geom_line()

基于形状的多条折线图

ggplot(df,aes(x = year,y = value,linetype = type))+
  geom_line()

注意:在绘制多条折线图时,如果横坐标为因子,必须得加上”group = 分组变量”参数,否则会报错,或绘制出错误的图形。

一些属性设置:
scale_colour_manual() # 自定义线条或点的颜色
scale_linetype_manual() #自定义线条类型
scale_shape_manual() #自定义点的形状
scale_size_manual() #自定义点的大小或线条的宽度
scale_alpha_manual() #自定义透明度
综合示例:

ggplot(df,aes(x = year,y = value,linetype = type),
       colour = type,shape = type,fill = type)+
  geom_line()+
  geom_point(size = 2)+
  scale_linetype_manual(values = c(1,2))+
  scale_colour_manual(values = c("steelblue","darkred"))+
  scale_shape_manual(values = c(21,23))+
  scale_fill_manual(values = c("red","black"))

3.绘制堆积面积图
堆叠的面积图只需要geom_area()函数,再加上一个离散变量映射到fill即可

set.seed(112)
year <- rep(1990:2015,times = 2)
type <- rep(c("A","B"),each = 26)
value <- c(runif(26),runif(26,min = 1,max = 1.5))
df <- data.frame(year = year,type = type,value = value)
ggplot(df,aes(x = year,y = value,fill = type))+
  geom_area()

# 反转图例顺序
ggplot(df,aes(x = year,y = value,fill = type))+
  geom_area()+
  guides(fill = guide_legend(reverse = TRUE))

# 面积图的顶部加上直线
ggplot(df,aes(x = year,y = value,fill = type))+
  geom_area(colour = "black",size = 1,alpha = 0.7)

# 面积图的顶部边缘加折线
ggplot(df,aes(x = year,y = value,fill = type))+
  geom_area()+
  geom_line(colour = "black",size = 1,alpha = 0.6,position = "stack")
 #必须有position = "stack",否则只是添加了两条折线,并不会与面积的边缘重叠

以上两幅添加折线图的区别是:第二幅图没有绘制面积图左右边框和底边框。在实际应用中,建议不要在面积图中绘制边框线,因为边框的存在可能产生误导。

4.绘制百分比堆积面积图

set.seed(112)
yaer <- rep(1990:2015,times = 4)
type <- rep(c("A","B","C","D"),each = 26)
value <- c(runif(26),runif(26,min = 1,max = 1.5),
           runif(26,min = 1.5,max = 2),runif(26,min = 2,max = 2.5))
df <- data.frame(year = year,type = type,value = value)
ggplot(df,aes(x = year,y = value,fill = type))+
  geom_area(position = "fill",alpha = 0.6)+
  guides(fill = guide_legend(reverse = TRUE))

上图的缺陷在于,无法绘制出百分比堆积面积的折线线条。
此时需要对数据作处理,进一步汇总.
“`{r}
library(dplyr)
df_by_type <- group_by(df,year)
df_summarize <- mutate(df_by_type,value2 = value/sum(value))
ggplot(df_summarize,aes(x = year,y = value2,fill = type))+
geom_area(alpha = 0.6)+
geom_line(colour = “black”,position = “stack”,size = 1,alpha = 0.6)+
guides(fill = guide_legend(reverse = TRUE))