【r<-ggplot2】ggplot2精要:线图

作者: 王诗翔 | 来源:发表于2019-03-06 12:44 被阅读24次

这个R教程讲解如何使用R和ggplot2包创建线图。

在一个线图中,观察值都按照x排列并连接起来。

可以使用函数geom_line()geom_step()geom_path()

x值可以是:

  • 日期:对于时间序列数据
  • 文本
  • 离散的数值
  • 连续的数值
ggplot2 line plot - R software and data visualization

基本的线图

数据

这里使用的数据来自数据集ToothGrowth,描述了不同剂量药物下牙齿的生长情况。

df <- data.frame(dose=c("D0.5", "D1", "D2"),
                len=c(4.2, 10, 29.5))
head(df)
##   dose  len
## 1 D0.5  4.2
## 2   D1 10.0
## 3   D2 29.5
  • len : 牙齿长度
  • dose : 以毫克为单位的药物剂量 (0.5, 1, 2)

创建带点的线图

library(ggplot2)
# 带点的基本线图
ggplot(data=df, aes(x=dose, y=len, group=1)) +
  geom_line()+
  geom_point()
# 改变线型
ggplot(data=df, aes(x=dose, y=len, group=1)) +
  geom_line(linetype = "dashed")+
  geom_point()
# 改变颜色
ggplot(data=df, aes(x=dose, y=len, group=1)) +
  geom_line(color="red")+
  geom_point()

阅读更多线型 : ggplot2 line types

你可以使用grid包为线条添加一个箭头:

library(grid)
# 添加箭头
ggplot(data=df, aes(x=dose, y=len, group=1)) +
  geom_line(arrow = arrow())+
  geom_point()
# 添加一个封闭的箭头
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()geom_path() 进行连接:

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 : Connecting observations, ordered by x value
  • geom_path() : Observations are connected in original order
  • geom_step : Connecting observations by stairs

有多个分组的线图

数据

数据还是使用ToothGrowth数据集:

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)
##   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 : 牙齿长度
  • dose : 药物剂量,以毫克为单位 (0.5, 1, 2)
  • supp : 添加类型 (VC or OJ)

创建线图

在下面的图中,两个组别线型、颜色和大小都是相同的。

# 有多个组别的线图
ggplot(data=df2, aes(x=dose, y=len, group=supp)) +
  geom_line()+
  geom_point()
# 改变线型
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)

按组别改变线型

下面图中,组别的线型、点类型都是根据变了supp自动控制的:

# 按组别 (supp) 改变线型
ggplot(df2, aes(x=dose, y=len, group=supp)) +
  geom_line(aes(linetype=supp))+
  geom_point()
# 改变线型和点的类型
ggplot(df2, aes(x=dose, y=len, group=supp)) +
  geom_line(aes(linetype=supp))+
  geom_point(aes(shape=supp))

也可以使用函数 scale_linetype_manual()手动更改线型。

# 手动设置线型
ggplot(df2, aes(x=dose, y=len, group=supp)) +
  geom_line(aes(linetype=supp))+
  geom_point()+
  scale_linetype_manual(values=c("twodash", "dotted"))

阅读更多线型: ggplot2 line types

如果你想要更改点的类型,阅读文章 ggplot2 point shapes

按组别更改线条颜色

线条颜色也可以由变量自动控制:

p<-ggplot(df2, aes(x=dose, y=len, group=supp)) +
  geom_line(aes(color=supp))+
  geom_point(aes(color=supp))
p

也可以使用下面的函数手动设置线条颜色:

  • scale_color_manual() : 使用自定义颜色
  • scale_color_brewer() : 使用RColorBrewer包提供的调色板
  • scale_color_grey() : 使用灰度调色板
# 使用自定义的调色板
p+scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# 使用brewer调色板
p+scale_color_brewer(palette="Dark2")
# 使用灰色值
p + scale_color_grey() + theme_classic()

阅读更多关于ggplot2颜色 : ggplot2 colors

更改图例位置

p <- p + scale_color_brewer(palette="Paired")+
  theme_minimal()
p + theme(legend.position="top")
p + theme(legend.position="bottom")
# 移除图例
p + theme(legend.position="none")

legend.position参数允许的值包括“left”,“top”, “right”, “bottom”。

阅读ggplot2图例: ggplot2 legend

带数值型x轴的线图

如果x轴的变量是数值型,我可以可以将它根据自己的需要看做连续值或转换为因子变量。

# 创建一些数据
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)
##   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
# x 轴看做连续变量
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()
# x轴看做离散变量
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()

x轴是日期的线图

这里是有economics数据集:

head(economics)
##         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

绘图:

# 基本的线图
ggplot(data=economics, aes(x=date, y=pop))+
  geom_line()
# 绘制子集
ggplot(data=subset(economics, date > as.Date("2006-1-1")), 
       aes(x=date, y=pop))+geom_line()

改变线的大小:

# Change line size
ggplot(data=economics, aes(x=date, y=pop, size=unemploy/pop))+
  geom_line()

带误差棒的线图

下面函数为每一个组别计算感兴趣变量的均值和标准差:

#+++++++++++++++++++++++++
# 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)
}

汇总数据:

df3 <- data_summary(ToothGrowth, varname="len", 
                    groupnames=c("supp", "dose"))
head(df3)
##   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()可以用来生成带误差棒的线图:

# 均值的标准差
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()
# 使用position_dodge水平移动误差棒以避免重叠
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()

自定义线图

# 简单的线图
# 分组改变点型和线型
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()
# 分组改变颜色
# 添加误差棒
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'))

手动改变颜色:

p + scale_color_brewer(palette="Paired") + theme_minimal()
# 绿色
p + scale_color_brewer(palette="Greens") + theme_minimal()
# 红色
p + scale_color_brewer(palette="Reds") + theme_minimal()

原英文地址:http://www.sthda.com/english/wiki/ggplot2-line-plot-quick-start-guide-r-software-and-data-visualization

相关文章

网友评论

    本文标题:【r<-ggplot2】ggplot2精要:线图

    本文链接:https://www.haomeiwen.com/subject/duxtpqtx.html