ggplot2(14)-ggplot2练习

前言

这一篇笔记主要是翻译国外的一个网站的教程,它对ggplot2的绘图做了很好的汇总,原文地址:R: basic graphs 2 (with ggplot2),原始数据在原文上有。

第一部分:导入数据

原始数据文件有7个,我已经下载到本地硬盘,如下所示:

mark

直接导入即可,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
library(ggplot2)
phdfig21a <- read.table("D:\\Personal\\R_code\\data\\ggplot2\\PhD_fig21A.csv",header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
# header=TRUE表示要读取标题
# sep=","表示数据文件PhD_fig21A.csv中的数据是以逗号分割的,csv文件默认都是以逗号分割的,如果是以空格分割的,则用sep=" "(中间空一格)。
# na.strings="NA"是将空白的数据用NA替代
# dec="."用来表示小数点
# strip.white=TRUE 表示当指定了sep时,则将字符元素前后的空格去掉(而数值元素则默认是去掉前后的空格)
phdfig37b <- read.table("D:\\Personal\\R_code\\data\\ggplot2\\PhD_fig37B.csv", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
phdfig22d <- read.table("D:\\Personal\\R_code\\data\\ggplot2\\PhD_fig22D.csv", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
phdfig23 <- read.table("D:\\Personal\\R_code\\data\\ggplot2\\PhD_fig23.csv", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
phdfig28c <- read.table("D:\\Personal\\R_code\\data\\ggplot2\\PhD_fig28C.csv", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
mdfig30a <- read.table("D:\\Personal\\R_code\\data\\ggplot2\\MD_fig30A.csv", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
mdfig30b <- read.table("D:\\Personal\\R_code\\data\\ggplot2\\MD_fig30B.csv", header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)

查看一下其中的某个数据,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> head(phdfig37b) # 先查看一下phdfig37b的数据集
session sham ACCX SEM_sham SEM_accx
1 1 0.0000000 -0.2 0.7881701 0.4163332
2 2 0.6666667 -0.3 0.4819992 0.3666667
3 3 -0.3333333 -0.2 0.7212503 0.5333333
4 4 1.5833333 1.0 0.5833333 0.4714045
5 5 -0.2500000 -0.3 0.7699370 0.5174725
6 6 1.7500000 -0.8 0.6410810 0.4898979
> tail(phdfig37b)
session sham ACCX SEM_sham SEM_accx
7 7 1.000000 -1.1 0.8438727 0.7951240
8 8 4.083333 0.2 1.0405297 0.6463573
9 9 4.666667 1.6 0.6999278 0.5617433
10 10 3.333333 3.3 1.0249415 1.0005554
11 11 4.500000 5.3 0.7229988 1.0857665
12 12 4.416667 3.9 0.6331539 0.7063207

现在构建ggplot2能够识别的数据框,如下所示:

1
2
3
4
5
6
7
phdfig37b_long = with(phdfig37b,
rbind( data.frame( lesion = "sham", session = session, diffscore = sham, sem = SEM_sham ),
data.frame( lesion = "ACCX", session = session, diffscore = ACCX, sem = SEM_accx ) )
)
# 建立一个数据框,第1列为lesion,并且为"sham"
# 第2列为session,值等于phdfig37b中的session
# 第3列为diffscore,值等于sham,第4列为sem,值等于SEM_sham

查看一下数据框,如下所示:

1
2
3
4
5
6
7
8
> head(phdfig37b_long)
lesion session diffscore sem
1 sham 1 0.0000000 0.7881701
2 sham 2 0.6666667 0.4819992
3 sham 3 -0.3333333 0.7212503
4 sham 4 1.5833333 0.5833333
5 sham 5 -0.2500000 0.7699370
6 sham 6 1.7500000 0.6410810

第二部分 qplot快速制图

1
2
3
4
5
6
7
p<-qplot(session, diffscore, colour=lesion, data=phdfig37b_long)
p<-p+ geom_line()
p<-p+ geom_errorbar(aes(ymin = diffscore - sem, ymax = diffscore + sem))
p
# 绘制数据集phdfig37b_long的图形,使x轴=session,y轴=diffscore,颜色属性=lesion
# geom_line() 添加折线图
# geom_errorbar 添加误差线

mark

第三部分:ggplot制图

目标:

  1. 绘制折线图;
  2. 添加误差线;
  3. 分类;
  4. 添加参考线。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
library(grid) # 后面要用到unit函数
f1 <- ggplot(data = phdfig37b_long, aes(x = session, y = diffscore, group = lesion) ) + # 绘图phdfig37b_long数据集的图形,使x轴=session,y轴=diffscore,group在这里的意思是,desion中的数据不属于一组
geom_errorbar(aes(ymin = diffscore - sem, ymax = diffscore + sem), width=0.3) +
# 添加误差线,指定宽度为0.4
geom_line() + # 添加折线
geom_point(aes(shape=lesion, fill=lesion), size=5) + # 添加数据点,将lesion映射到形状属性上,将将lesion映射到填充属性上(即数据点内部的颜色,非轮廓)
scale_x_continuous("Session", breaks=1:12) + # 对x轴上的sesssion指定刻度,从1到12
scale_y_continuous("Difference score", limits = c(-4, 8), breaks=seq(-4, 8, by = 2)) + # 指定y轴的刻度
scale_shape_manual(values=c(24,21)) + # 设置数据点的形状,一个是24,一个是21,24表示正三角,21表示圆形
scale_fill_manual(values=c("white","black")) + # 指定数据点的颜色,一个是白色,一个是黑色
geom_abline(intercept=0, slope=0, linetype="dotted") + # 添加参考线,截距是0,斜率是0,点的类型是点状
annotate("text", x=11, y=-0.25, label="chance") + # 添加注释,坐标为(11,-0.25),文字为"chance"
theme_bw() + # 将绘图的主题改为白色
ggtitle("Two-stimulus discriminated approach task")+ # 添加图形的标题
theme(
plot.title = element_text(face="bold", size=14), # 修改标题文本的属性
axis.title.x = element_text(face="bold", size=12), # 修改x轴标签文本的属性
axis.title.y = element_text(face="bold", size=12, angle=90),# 修改y轴标签文本的属性
axis.text.x=element_text(size=15), # 修改x轴坐标轴刻度的大小
panel.grid.major = element_blank(), # 移除绘图区域的主要网格线
panel.grid.minor = element_blank(), # 移除绘图区域的次要网格线
legend.position = c(0.2,0.8), # 图例的位置坐标
legend.title = element_blank(), # 图例标题为空
legend.text = element_text(size=12), # 图例中项目文本的大小
legend.key.size = unit(1.5, "lines"), # 图例中项目的间隔,需要grid包
legend.key = element_blank() # 移除图例中项目的边框
)
print(f1)

mark

第四部分:输出图形

保存图形命令如下所示:

1
2
3
4
5
6
7
ggsave(filename="d:\\我的图形.pdf",plot=f1)形为f1
## Saving 5 x 4 in image
# 另外一些保存图形的方法,如下所示:
ggsave("d:\\f1.pdf")
## Saving 5 x 4 in image
ggsave("d:\\f1.png",dpi=300) # 保存f1,分辨率为300dpi
## Saving 5 x 4 in image

第五部分:自由浮动的SED

A.构建数据框

如下所示:

1
2
3
4
5
6
7
phdfig21a_long <- with(phdfig21a,
rbind( data.frame( lesion = "sham", lever = "CRf", dose = dose, sqrtpresses = shamCR, sem = SEMshamCR, halfSED = halfSED[1] ),
data.frame( lesion = "sham", lever = "NCRf", dose = dose, sqrtpresses = shamNCR, sem = SEMshamNCR, halfSED = halfSED[1] ),
data.frame( lesion = "ACCX", lever = "CRf", dose = dose, sqrtpresses = ACCXCR, sem = SEMACCXCR, halfSED = halfSED[1] ),
data.frame( lesion = "ACCX", lever = "NCRf", dose = dose, sqrtpresses = ACCXNCR, sem = SEMACCXNCR, halfSED = halfSED[1] ) )
)
head(phdfig21a_long)

数据如下所示:

1
2
3
4
5
6
7
8
> head(phdfig21a_long)
lesion lever dose sqrtpresses sem halfSED
1 sham CRf 0 5.112528 0.5539816 0.9396
2 sham CRf 3 6.829842 1.0673567 0.9396
3 sham CRf 10 9.230089 0.9947341 0.9396
4 sham CRf 20 8.563017 0.8215663 0.9396
5 sham NCRf 0 3.685772 0.4961057 0.9396
6 sham NCRf 3 4.024871 0.6831637 0.9396

B. 再添加一个新的变量,并查看数据,如下所示:

1
2
3
4
5
6
7
8
9
phdfig21a_long <- within(phdfig21a_long, {
# 加入一列新的变量
lesion_lever <- do.call(paste, c(phdfig21a_long[c("lesion", "lever")], sep = " "))
# do.call表示,运行函数paste,将phdfig21a_long中的lession与lever用空格连接起来,并生成一列新的变量
lesion_lever <- factor(lesion_lever, levels=unique(lesion_lever))
# unique(lesion_lever)返回phdfig21a_long数据集中的lesion_lever中的重复元素,即提取出互不相同的元素(如果有相同的几个则取一个,只有一个的,也取一个)
dose <- factor(dose)
})
head(phdfig21a_long)

修改后的数据框如下所示:

1
2
3
4
5
6
7
8
> head(phdfig21a_long)
lesion lever dose sqrtpresses sem halfSED lesion_lever
1 sham CRf 0 5.112528 0.5539816 0.9396 sham CRf
2 sham CRf 3 6.829842 1.0673567 0.9396 sham CRf
3 sham CRf 10 9.230089 0.9947341 0.9396 sham CRf
4 sham CRf 20 8.563017 0.8215663 0.9396 sham CRf
5 sham NCRf 0 3.685772 0.4961057 0.9396 sham NCRf
6 sham NCRf 3 4.024871 0.6831637 0.9396 sham NCRf

C.绘图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
f2 <- ggplot(data = phdfig21a_long, aes(x = dose, y = sqrtpresses, group = lesion_lever ) ) +
geom_line() +
geom_point(aes(shape=lesion_lever, fill=lesion_lever), size=5) + # 将变量lesion_lever映射到shape上,将lesion_lever映射到填充色上
geom_errorbar(aes(x=1, ymin=10 - phdfig21a$halfSED[1], ymax=10 + phdfig21a$halfSED[1], width = 0.2)) + # 绘制一个误差棒,位置是x=1,宽度为0.2
scale_shape_manual(values=c(21,24,21,24)) + # 指定shamCR, shamNCR, ACCXCR, ACCXNCR这四种类型的形状,分别为圆、三角、圆、三角
scale_fill_manual(values=c("white","white","black","black")) + # 指定shamCR, shamNCR, ACCXCR, ACCXNCR这四种类型的颜色,分别为白、白、黑、黑
xlab(substitute(paste("Intra-Acb amphetamine (",mu,"g)"))) + #添加x轴标签,substitute用来显示公式,
scale_y_continuous(expression(sqrt("lever presses")), expand=c(0,0), limits = c(0, 14), breaks=seq(0, 14, by = 2)) + # the "expand" bit prevents ggplot2 from expanding the axis a little beyond what you specify
annotate("text", x=1.4, y=10, label="SED") +
theme_bw() +
ggtitle("Conditioned reinforcement")+ # 添加图形的标题
theme(plot.title = element_text(face="bold", size=14),
axis.title.x = element_text(face="bold", size=12),
axis.title.y = element_text(face="bold", size=12, angle=90),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border=element_blank(), # 取消绘图区域四周的边框
axis.line=element_line(colour="black"), # 添加坐标轴(即x轴与y轴)的直线
legend.position = c(0.8,0.2),
legend.title = element_blank(),
legend.text = element_text(size=10),
legend.key.size = unit(1, "lines"),
legend.key = element_blank()
)
print(f2)

mark

第六部分 绘制双变量的条形图及误差棒

A.整理数据

查看数据,如下所示:

1
2
3
4
> phdfig22d
group CSpluslatency CSminuslatency CSplusSEM CSminusSEM
1 sham 3.884335 4.212531 0.2248404 0.2593122
2 ACCX 4.672486 3.619434 0.3476289 0.4902273

整理数据,如下所示:

1
2
3
4
5
phdfig22d_long = with(phdfig22d,
rbind( data.frame( lesion=group, stimulus="CS+", latency=CSpluslatency, sem=CSplusSEM ),
data.frame( lesion=group, stimulus="CS-", latency=CSminuslatency, sem=CSminusSEM ) )
)
phdfig22d_long$lesion = factor(phdfig22d_long$lesion, levels=unique(phdfig22d_long$lesion))

上述代码是生成phdfig22d_long数据框,这个数据框有四列变量,分别为lesion,stimulus,lantency,sem,查看数据,如下所示:

1
2
3
4
5
6
> phdfig22d_long
lesion stimulus latency sem
1 sham CS+ 3.884335 0.2248404
2 ACCX CS+ 4.672486 0.3476289
3 sham CS- 4.212531 0.2593122
4 ACCX CS- 3.619434 0.4902273

B.绘图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
f3 <- ggplot(data = phdfig22d_long, aes(x=lesion, y=latency, fill=stimulus) ) +
geom_errorbar(aes(ymin=latency, ymax=latency+sem, width = 0.2),position=position_dodge(width=0.90)) + # 误差棒只添加上部分,因此误差棒的最低部是latency,最上部是lantency + sem,宽度是0.2,最低部与最上部进行堆叠(position_dodge)
geom_bar(stat="identity", position="dodge") +
geom_bar(stat="identity", position="dodge", colour="black") +
scale_fill_manual(values=c("grey80", "white")) + # 对两个变量进行颜色填充,分别为灰度80与白色
xlab("") + # x轴不添加标签
scale_y_continuous("Latency to approach (s)", expand=c(0,0), limits = c(0, 5.5)) + # expand=c(0,0) 表示将y轴从0开始
theme_bw() +
ggtitle("Autoshaping approach latencies")+
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.line=element_line(colour="black"),
axis.text.y=element_text(angle=90, hjust=0.5),
legend.position = c(0.25,0.85),
legend.key = element_rect()
)
print(f3)

mark

第七部分:带有注释线的误差棒

A.查看数据

1
2

B.整理数据

1
2
3
4
phdfig23_long = with(phdfig23,
rbind( data.frame( lesion=group, stimulus="CS+", approaches=CSplus, sem=SEMCSplus ),
data.frame( lesion=group, stimulus="CS-", approaches=CSminus, sem=SEMCSminus ) )
)

查看数据,如下所示:

1
2
3
4
5
6
> phdfig23_long
lesion stimulus approaches sem
1 sham CS+ 14.000000 0.9309493
2 ACCX CS+ 9.857143 1.0785478
3 sham CS- 4.500000 0.8595865
4 ACCX CS- 6.428571 1.0658585

C.绘图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
f4 <- ggplot(data = phdfig23_long, aes(x=lesion, y=approaches, fill=stimulus, width=0.5) ) +
geom_errorbar(aes(ymin=approaches, ymax=approaches+sem, width = 0.2), position=position_dodge(width=0.8)) +
geom_bar(stat="identity", position=position_dodge(width=0.8)) +
geom_bar(stat="identity", position=position_dodge(width=0.8), colour="black") +
scale_fill_manual(values=c("grey80", "white")) +
scale_x_discrete("Group", limits=c("sham", "ACCX")) + # 设置x轴上的分类变量,顺序为sham,ACCX
scale_y_continuous("Number of approaches", expand=c(0,0), limits = c(0, 17), breaks=seq(0, 16, by=2)) +
geom_segment(aes(x=0.8, y=15.3, xend=0.8, yend=16)) +
geom_segment(aes(x=0.8, y=16, xend=1.2, yend=16)) +
geom_segment(aes(x=1.2, y=6, xend=1.2, yend=16)) + # 添加注释线,注意坐标
annotate("text", x=1, y=15.5, label="***") +
theme_bw() +
ggtitle("Autoshaping probe test")+
theme( plot.title = element_text(face="bold", size=14),
axis.title.x = element_text(face="bold", size=12),
axis.title.y = element_text(face="bold", size=12, angle=90),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.line=element_line(colour="black"),
axis.text.y=element_text(angle=90, hjust=0.5),
legend.title = element_blank(),
legend.position = c(0.85,0.85),
legend.key.size = unit(1.5, "lines"),
legend.key = element_rect()
)
print(f4)

mark

代码解释:

上述代码中有一部分是添加注释线的,就是与三个星号在一起的一段线。代码为:

1
2
3
4
geom_segment(aes(x=0.8, y=15.3, xend=0.8, yend=16)) +
geom_segment(aes(x=0.8, y=16, xend=1.2, yend=16)) +
geom_segment(aes(x=1.2, y=6, xend=1.2, yend=16)) + # 添加注释线,注意坐标
annotate("text", x=1, y=15.5, label="***") +

在添加注释线的时候,如果要精确地绘制,需要知道x轴上条形图中心的坐标,可以用str(print(f4))查看,str(print(f4)),前5行的数字为:

1
2
3
4
5
List of 3
$ data :List of 7
..$ :'data.frame': 4 obs. of 9 variables:
.. ..$ fill : chr [1:4] "grey80" "white" "grey80" "white"
.. ..$ x : num [1:4] 0.8 1.2 1.8 2.2

其中第5行是:

1
.. ..$ x : num [1:4] 0.8 1.2 1.8 2.2

可以发现,条形图第一个条带的中心处,对应的x轴坐标为0.8,因此在绘制注释线的时候,x的起点为是0.8。

第八部分:含有双向x轴的绘图

A.查看原始数据

1
2
3
4
5
6
7
8
9
10
11
> phdfig28c
block sham ACCX SEMsham SEMACCX
1 -3 0.8905724 0.8648725 0.04328269 0.05561053
2 -2 0.9212121 0.8789751 0.03030303 0.03002131
3 -1 0.9237374 0.9204775 0.02654952 0.02897810
4 0 NA NA NA NA
5 1 0.7755162 0.6273393 0.04621826 0.05090232
6 2 0.8674289 0.6453398 0.05521676 0.05800651
7 3 0.8327228 0.6989440 0.07589090 0.06084011
8 4 0.9145688 0.8424770 0.03463735 0.03901640
9 5 0.8874644 0.8592287 0.03880296 0.04260364

B.整理数据

1
2
3
4
phdfig28c_long <- with(phdfig28c,
rbind( data.frame( block=block, lesion="sham", diffscore=sham, sem=SEMsham ),
data.frame( block=block, lesion="ACCX", diffscore=ACCX, sem=SEMACCX ) ) )
phdfig28c_long # 查看整理后的数据

整理后的数据如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
> phdfig28c_long # 查看整理后的数据
block lesion diffscore sem
1 -3 sham 0.8905724 0.04328269
2 -2 sham 0.9212121 0.03030303
3 -1 sham 0.9237374 0.02654952
4 0 sham NA NA
5 1 sham 0.7755162 0.04621826
6 2 sham 0.8674289 0.05521676
7 3 sham 0.8327228 0.07589090
8 4 sham 0.9145688 0.03463735
9 5 sham 0.8874644 0.03880296
10 -3 ACCX 0.8648725 0.05561053
11 -2 ACCX 0.8789751 0.03002131
12 -1 ACCX 0.9204775 0.02897810
13 0 ACCX NA NA
14 1 ACCX 0.6273393 0.05090232
15 2 ACCX 0.6453398 0.05800651
16 3 ACCX 0.6989440 0.06084011
17 4 ACCX 0.8424770 0.03901640
18 5 ACCX 0.8592287 0.04260364

C.绘图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
f5 <- ggplot(data = phdfig28c_long, aes(x = block, y = diffscore, group = lesion) ) +
geom_errorbar(aes(ymin = diffscore - sem, ymax = diffscore + sem), width=0.3) +
geom_line() +
geom_point(aes(shape=lesion, fill=lesion), size=5) +
scale_x_continuous("Trial block", expand=c(0,0), limits=c(-3.5,5.5), breaks=-3:5, labels=c("-3","-2","-1","surgery","1","2","3","4","5")) +
scale_y_continuous("Difference score", expand=c(0,0), limits = c(0.4, 1), breaks=seq(0.4, 1, by = 0.1)) +
scale_shape_manual(values=c(24,21)) +
scale_fill_manual(values=c("white","black")) +
geom_abline(intercept=0.5, slope=0, linetype="dotted") + # 截距为0.5,斜率为0的虚线
annotate("text", x=-2, y=0.48, label="chance") +
theme_bw() +
theme(plot.margin=unit(c(1,1,1.5,1.5), "lines"), # 分别指的是整个绘图区域(包括x轴与y轴的标签)距离顶边,右边,底边,左边的距离(默认是1,1,0.5,0.5)
axis.ticks.margin=unit(0.25,"lines"), # 坐标轴刻度的值距离刻度线的距离
axis.title.x = element_text(face="bold", size=12, vjust=-1), # use vjust to move text away from the axis
axis.title.y = element_text(face="bold", size=12, angle=90, vjust=0), # likewise
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = c(0.2,0.4),
legend.title = element_blank(),
legend.text = element_text(size=12),
legend.key.size = unit(1.5, "lines"),
legend.key = element_blank() # switch off the rectangle around symbols in the legend
)
print(f5)

mark

第九部分:分面、折线、星号

A.查看原始数据

1
2
3
4
5
> mdfig30a
delay sham SEMsham AcbC SEMAcbC
1 0 2.640708 0.09574035 3.015727 0.07431359
2 10 2.276907 0.07223806 1.977225 0.12287066
3 20 1.956931 0.07676898 1.400463 0.11313513

B.整理数据

1
2
3
4
5
6
7
8
9
10
11
mdfig30_long <- rbind( data.frame( type="programmed", delay=mdfig30a$delay, lesion="sham", sqrtpresses = mdfig30a$sham, ysem = mdfig30a$SEMsham, xsem = 0 ),
data.frame( type="programmed", delay=mdfig30a$delay, lesion="AcbC", sqrtpresses = mdfig30a$AcbC, ysem = mdfig30a$SEMAcbC, xsem = 0 ),
data.frame( type="experienced", delay=mdfig30b$shamdelay, lesion="sham", sqrtpresses = mdfig30b$shamresponses, ysem = mdfig30b$SEMshamresponses, xsem = mdfig30b$SEMshamdelay ),
data.frame( type="experienced", delay=mdfig30b$AcbCdelay, lesion="AcbC", sqrtpresses = mdfig30b$AcbCresponses, ysem = mdfig30b$SEMAcbCresponses, xsem = mdfig30b$SEMAcbCdelay ) )
mdfig30_annotations <- data.frame(
x = c(0,10,20,10),
y = c(3.2,1.7,1.2,1.4),
type = c("programmed", "programmed", "programmed", "experienced"),
label = c("*", "*", "***", "###"),
lesion = c("sham", "sham", "sham", "sham") )

查看整整后的数据,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
> mdfig30_long
type delay lesion sqrtpresses ysem xsem
1 programmed 0.000000 sham 2.640708 0.09574035 0.0000000
2 programmed 10.000000 sham 2.276907 0.07223806 0.0000000
3 programmed 20.000000 sham 1.956931 0.07676898 0.0000000
4 programmed 0.000000 AcbC 3.015727 0.07431359 0.0000000
5 programmed 10.000000 AcbC 1.977225 0.12287066 0.0000000
6 programmed 20.000000 AcbC 1.400463 0.11313513 0.0000000
7 experienced 2.127342 sham 2.640708 0.09574035 0.3954407
8 experienced 9.483373 sham 2.276907 0.07223806 0.4826215
9 experienced 15.812267 sham 1.956931 0.07676898 1.0514280
10 experienced 2.307076 AcbC 3.015727 0.07431359 0.3887686
11 experienced 11.680260 AcbC 1.977225 0.12287066 0.5989249
12 experienced 20.936766 AcbC 1.400463 0.11313513 1.8152600
> mdfig30_annotations
x y type label lesion
1 0 3.2 programmed * sham
2 10 1.7 programmed * sham
3 20 1.2 programmed *** sham
4 10 1.4 experienced ### sham

C.绘图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
f6 <- ggplot(data = mdfig30_long, aes(x = delay, y = sqrtpresses, fill = lesion) ) +
facet_grid(. ~ type) +
geom_line() +
geom_errorbar(aes(ymin=sqrtpresses - ysem, ymax=sqrtpresses + ysem), width=0.8) +
geom_errorbarh(aes(xmin=delay - xsem, xmax=delay + xsem), height=0.05) +
geom_point(shape=21, colour="black", size=5) +
scale_fill_manual(values=c("white","black")) +
scale_x_continuous("Delay from response to reinforcer (s)") +
scale_y_continuous(expression(sqrt("active lever presses per minute in session 10")), expand=c(0,0), limits = c(0, 3.5), breaks=0:3) +
geom_text(data = mdfig30_annotations, aes(x = x, y = y, label = label) ) +
theme_bw() +
theme(plot.margin=unit(c(1,1,1.5,2), "lines"),
axis.title.x = element_text(size=12, vjust=-1),
axis.title.y = element_text(size=12, angle=90, vjust=0), # likewise
legend.title = element_blank(),
legend.key.size = unit(1.5, "lines"),
legend.key = element_blank()
)
print(f6)

mark

参考资料

http://egret.psychol.cam.ac.uk/statistics/R/graphs2.html