添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
# 基础图形
bp <- ggplot(dat, aes(x=cond, y=result)) +
    geom_bar(position=position_dodge(), stat="identity")
 
# 添加一条水平线
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)
 
##        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
 
##        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")
 
# 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 + 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 + 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")
本文档旨在使用 knitr 包进行处理,这是 Sweave 的重新实现和泛化,它将使用代码生成报告的概念提升到一个完全不同的水平。 有关该软件包的信息,请参阅在 Demos 链接下查找越来越多的文档。 knitr 中代码块的结构类似于 Sweave 中的代码块,但头部中的参数通常是不同的。 基本结构是 << name xss=removed xss=removed xss=removed>> = your code here 其中第一个参数是块的名称,其余参数是块选项。 有关更多详细信息,请参阅 。 源文件 ggplot2-0.9.0.Rnw 包含在这个 repo 中。 要自己在 R 中运行源代码,您需要最新版本的 knitr 和至少 0.9.0 的 ggplot2 版本(在[重新]编写时,当前版本是 0.9.3)。 下载源文件后,将其放入一
推荐 https://blog.csdn.net/qazplm12_3/article/details/110252322 (生信宝典) 今天,主要简单的介绍了ggplot2 R包,在数据分析中,数据的可视化是必不可少的,故掌握一定的画图方法很有必要,使用该包可以很快上手。其图层叠加的思想是关键。 天行健,君子以自强不息 -2023-7-7 筑基篇。
ggplot2画箱线图默认情况下所有的线都是实线,如下ggplot(HMP,aes(x=country,y=log10(rel_crAss)))+ geom_boxplot() 可以通过加参数linetype更改线的类型,比如改成虚线ggplot(HMP,aes(x=country,y=log10(rel_crAss)))+ geom_boxplot(linetype="dashed")
文章目录ggplot图形分解1. ggplot中的par(mfrow = c(3,1))如何实现2. ggplot分解A. 给每个点上都加上数值标签:geom_text()B. ggplot()设置,单组数据B. 点的设置:geom_point()C.线的设置:geom_line()D. 轴范围xlim(),横纵轴名称xlab(),ylab() 如果图里面有你想看到的,希望调整的细节,就继续往下探...
2.选择:下载ZIP 3.转到“下载”,“塔夫茨·弗里德曼ggplot2 Workshop”文件夹现在应该存在 4.打开“ Tufts Friedman ggplot2 Workshop”(根据操作系统从ZIP中提取) 5.选择您将要使用的文件以及研讨会的编码部分: -PDF:不互动,研讨会幻灯片 -HTML:非交互式,单击文件,它将在浏览器页面中打开 -HMTL.nb:有点互动,单击文件,它将在浏览器页面中打开 -.RMD:Interactive,使用Rstudio打开,将在Rstudio控制台中打开一个可执行笔记本 -.R:Interactive,使用Rstudio打开,将打开您将运行的代码文件 如果其他所
关于基本的直方图设置、纵坐标对数变换以及基本的颜色设置,可以参考R语言ggplot2直方图设置。 本文使用的数据为:R语言自带的mtcars数据集的mpg变量。以下内容中将首先给出目标图片和代码,并逐一解释。欢迎批评指正! 目录1. 图片及所有代码2. 按步骤说明2.1 最基本的直方图(颜色,柱宽,位置)2.2 在直方图上加密度曲线2.3 添加(修改)标题与横纵轴名称,并修改标题格式。2.4 添加背景填充并且设置其颜色2.5 改变背景填充颜色2.6 添加直线条2.7 图中添加文字与矩形注释2.8 改变图片
ggplot2是一种常用的数据可视化工具,它可以绘制各种类型的图表,包括散点图、线性回归图等。在ggplot2中,我们可以使用geom_smooth函数来添加拟合直线。 要添加拟合直线,我们首先需要选择一种拟合方法,ggplot2支持多种拟合方法,如“lm”(线性回归)、“loess”(局部回归)、“glm”(广义线性模型)等。例如,如果我们想要添加一条线性回归线,可以在geom_smooth函数中加入method="lm"参数,这样ggplot2就会自动拟合一条线性回归线。 此外,在添加拟合直线的同时,我们还可以在图表中添加拟合值,也就是在拟合直线添加数值标注。这可以通过在geom_text函数中加入aes(x,y,label)参数实现,其中x和y是标注的位置,label是标注的内容。例如,如果我们想要在图表中添加线性回归线的截距和斜率,可以使用geom_text函数来实现。 总之,ggplot2拟合直线添加值是通过使用geom_smooth函数来实现的,我们可以根据需要选择不同的拟合方法,并在图表中添加标注,使得数据可视化更加清晰和直观。