ggplot2-为图形添加直线

本文更新地址:ggplot2-为图形添加直线_ggplot2 add line-CSDN博客

本文在 Scatterplots (ggplot2) 的基础上加入了自己的理解

对于连续型数据轴和离散型数据轴

# Some sample data
dat <- read.table(header=TRUE, text='
     cond result
  control     10
treatment   11.5
')

library(ggplot2)
# 基础图形
bp <- ggplot(dat, aes(x=cond, y=result)) +
    geom_bar(position=position_dodge(), stat="identity")
bp

# 添加一条水平线
bp + geom_hline(aes(yintercept=12))

# linetype 设置线型为 虚线
bp + geom_hline(aes(yintercept=12), colour="#990000", linetype="dashed")

根据分类情况,添加直线

# Draw separate hlines for each bar. First add another column to dat
dat$hline <- c(9,12)
dat
##        cond result hline
## 1   control   10.0     9
## 2 treatment   11.5    12
# Need to re-specify bp, because the data has changed
bp <- ggplot(dat, aes(x=cond, y=result)) +
    geom_bar(position=position_dodge(), stat="identity")

# 对每一个条形添加分割线
bp + geom_errorbar(aes(y=hline, ymax=hline, ymin=hline), colour="#AA0000")

bp + geom_errorbar(width=0.5, # 设置线宽度
                   aes(y=hline, ymax=hline, ymin=hline), colour="#AA0000")

# 重新定义数据
dat_hlines <- data.frame(cond=c("control","treatment"), hline=c(9,12))
dat_hlines
##        cond hline
## 1   control     9
## 2 treatment    12
# 条形图的数据来自于 dat, 但是 线的数据来自于 dat_hlines
bp + geom_errorbar(data=dat_hlines, aes(y=hline, ymax=hline, ymin=hline), colour="#AA0000")

直线穿过各组数据

这里实际上是有4条线, 但是前两条线与后两条线的数值相同,看上去是两条线.

dat <- read.table(header=TRUE, text='
     cond group result hline
  control     A     10     9
treatment     A   11.5    12
  control     B     12     9
treatment     B     14    12
')
dat
##        cond group result hline
## 1   control     A   10.0     9
## 2 treatment     A   11.5    12
## 3   control     B   12.0     9
## 4 treatment     B   14.0    12
# Define basic bar plot
bp <- ggplot(dat, aes(x=cond, y=result, fill=group)) +
    geom_bar(position=position_dodge(), stat="identity")
bp

# The error bars get plotted over one another -- there are four but it looks
# like two
bp + geom_errorbar(aes(y=hline, ymax=hline, ymin=hline), linetype="dashed")

每组数据分别划线

dat <- read.table(header=TRUE, text='
     cond group result hline
  control     A     10    11
treatment     A   11.5    12
  control     B     12  12.5
treatment     B     14    15
')

# Define basic bar plot
bp <- ggplot(dat, aes(x=cond, y=result, fill=group)) +
    geom_bar(position=position_dodge(), stat="identity")
bp

bp + geom_errorbar(aes(y=hline, ymax=hline, ymin=hline), linetype="dashed",
                   position=position_dodge())

为连续型数据轴添加直线

dat <- read.table(header=TRUE, text='
      cond xval yval
   control 11.5 10.8
   control  9.3 12.9
   control  8.0  9.9
   control 11.5 10.1
   control  8.6  8.3
   control  9.9  9.5
   control  8.8  8.7
   control 11.7 10.1
   control  9.7  9.3
   control  9.8 12.0
 treatment 10.4 10.6
 treatment 12.1  8.6
 treatment 11.2 11.0
 treatment 10.0  8.8
 treatment 12.9  9.5
 treatment  9.1 10.0
 treatment 13.4  9.6
 treatment 11.6  9.8
 treatment 11.5  9.8
 treatment 12.0 10.6
')

library(ggplot2)

Basic lines

# 基本散点图
sp <- ggplot(dat, aes(x=xval, y=yval, colour=cond)) + geom_point()

# 水平线
sp + geom_hline(aes(yintercept=10))

# 垂直虚线
sp + geom_hline(aes(yintercept=10)) +
    geom_vline(aes(xintercept=11.5), colour="#BB0000", linetype="dashed")

为均值自动划线

# Add colored lines for the mean xval of each group
sp + geom_hline(aes(yintercept=10)) +
     geom_line(stat="vline", xintercept="mean")

# 根据 cond 分面
spf <- sp + facet_grid(. ~ cond)
spf

# 在所有的面上划线
spf + geom_hline(aes(yintercept=10))

如果想为不同的分面, 添加不同的直线, 这里有两种设置. 一种是重新生成一个满足数据需要的 data.frame 另一种是使用 geom_line() 的statxintercept

# 第一种方法
dat_vlines <- data.frame(cond=levels(factor(dat$cond)), xval=c(10,11.5))
dat_vlines
##        cond xval
## 1   control 10.0
## 2 treatment 11.5
spf + geom_hline(aes(yintercept=10)) +
      geom_vline(aes(xintercept=xval), data=dat_vlines,
                    colour="#990000", linetype="dashed")

# 第二种方法
spf + geom_hline(aes(yintercept=10)) +
      geom_line(stat="vline", xintercept="mean")

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

探索者v

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值