版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Using ggplot2 Hadley Wickham 是 RStudio 的首席科学家以及 Rice University 统计系的助理教授。他是著名图形可视化软件 包 ggplot2 的开发者,以及其他许多被广泛使用的软件包 的作者,代表作品如 plyr、reshape2 等 Layout 概念 例子 Layout 数据和映射 ggplot(data,aes(x=,y=,fill=) 数据集 映射 Layout 几何对象 :散点图,气泡图,条形图,折线图,箱形图 统计变换 :封箱(bin),identity 位置调整:dodge,fill,identity 坐标系统: xlim,ylim
2、 图层 分面: facet_grid 例子 1散点图起手势 ggplot(mtcars, aes(x=wt, y=mpg) + geom_point() Mtcars数据格式 2shape参数修改图形的形状 ggplot(mtcars, aes(x=wt, y=mpg) + geom_point(shape=21) 3size参数修改图形的大小 ggplot(mtcars, aes(x=wt, y=mpg) + geom_point(size=1.5) ggplot(mtcars, aes(x=wt, y=mpg) + geom_point(size=5) 4 多因素作图 heightweig
3、ht, c(sex, ageYear, heightIn) ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=sex) + geom_point() ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex,color=sex) + geom_point() 5 scale_shape_manual 设置想要shape ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex, colour=sex) + geom_po
4、int() + scale_shape_manual(values=c(1,2) + scale_colour_brewer(palette=Set1) ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=weightLb) + geom_point() ggplot(heightweight, aes(x=ageYear, y=heightIn, size=weightLb) + geom_point() 6scale_fill_gradient设置legend的颜色 ggplot(heightweight, aes(x=weight
5、Lb, y=heightIn, fill=ageYear) + geom_point(shape=21, size=2.5) + scale_fill_gradient(low=black, high=red) 7scale_fill_gradient设置break的大小 ggplot(heightweight, aes(x=weightLb, y=heightIn, fill=ageYear) + geom_point(shape=21, size=2.5) + scale_fill_gradient(low=black, high=red, breaks=12:17, guide=guid
6、e_legend() 8scale_size_area()使图形比例恰当 ggplot(heightweight, aes(x=ageYear, y=heightIn, size=weightLb, colour=sex) + geom_point(alpha=.5) + scale_size_area() +scale_colour_brewer(palette=Set1) 9Alpha参数修改透明度 sp - ggplot(diamonds, aes(x=carat, y=price) sp + geom_point() sp + geom_point(alpha=.1) sp + geo
7、m_point(alpha=.01) 10使用bin方法显示高密度散点图 sp + stat_bin2d() 10Bin设置区分度, scale_fill_gradient limit 设置上标下标 sp + stat_bin2d(bins=50) + scale_fill_gradient(low=lightblue, high=red, limits=c(0, 6000) 11scale_fill_gradient设置legend 的坐标 sp + stat_bin2d() + scale_fill_gradient(low=lightblue, high=red, breaks=c(0,
8、 250, 500, 1000, 2000, 4000, 6000), limits=c(0, 6000) 12Position=“jetter”画扰动图 sp1 - ggplot(ChickWeight, aes(x=Time, y=weight) sp1 + geom_point() sp1 + geom_point(position=jitter) head(ChickWeight) 13Geom_boxplot的几何对象 sp1 + geom_boxplot(aes(group=Time) 14stat_smooth(method=lm)对散点图增加线 性回归 sp - ggplot(
9、heightweight, aes(x=ageYear, y=heightIn) sp + geom_point() + stat_smooth(method=lm) # 默认95%置信 15Level函数设置线性回归设置置信区域 sp + geom_point() + stat_smooth(method=lm, level=0.99) sp + geom_point() + stat_smooth(method=lm, se=FALSE,colour=“black”) method=loess 16 Annotate添加文本 sp + annotate(text, label=r2=0.4
10、2, x=16.5, y=52) 17 Annotate添加标签 sp 2000), aes(x=healthexp, y=infmortality) +geom_point() sp + annotate(text, x=4350, y=5.4, label=Canada) + annotate(text, x=7400, y=6.8, label=USA) 18 Geom_text(aes(lable=)批量添加标签 sp + geom_text(aes(label=Name), size=4) 19Geom_text通过设置x=确定标签位置 sp + geom_text(aes(x=he
11、althexp+100, label=Name), size=4, hjust=0) 20stat_density2d()画二维散点图 p - ggplot(faithful, aes(x=eruptions, y=waiting) p + geom_point() + stat_density2d() 21 scale_colour_brewer(palette=Set1) 对调色板进行选择 ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex, colour=sex) + geom_point() + scale_shape_m
12、anual(values=c(1,2) + scale_colour_brewer(palette=Set1) 22Element_blank()表示清空 ggplot(tophit, aes(x=reorder(name, avg), y=avg) + geom_point(size=3)+theme_bw() + theme(axis.text.x = element_text(angle=60, hjust=1), panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank(), panel.grid.
13、major.x = element_line(colour=grey60, linetype=dashed) 23Legend设置的重要参数 ggplot(tophit, aes(x=avg, y=name) + geom_segment(aes(yend=name), xend=0, colour=grey50) + geom_point(size=3, aes(colour=lg) + scale_colour_brewer(palette=Set1, limits=c(NL,AL) + theme_bw() + theme(panel.grid.major.y = element_bla
14、nk(), legend.position=c(1, 0.55), # Put legend inside plot area legend.justification=c(1, 0.5) ggplot(tophit, aes(x=avg, y=name) + geom_segment(aes(yend=name), xend=0, colour=grey50) + geom_point(size=3, aes(colour=lg) + scale_colour_brewer(palette=Set1, limits=c(NL,AL), guide=FALSE) + theme_bw() +
15、theme(panel.grid.major.y = element_blank() + facet_grid(lg ., scales=free_y, space=free_y) 24 条形图起手势 ggplot(pg_mean, aes(x=group, y=weight) + geom_bar(stat=identity) 25 Stat identity对数据不进行统计变化 ggplot(pg_mean, aes(x=group, y=weight) + geom_bar(stat=identity, fill=lightblue, colour=black) 26 position图
16、形的位置关系 ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar) + geom_bar(position=“dodge”) #去掉position则柱形图堆起来 27 width调整柱形图的宽 ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar) + geom_bar(stat=identity, width=0.5) 28 position=position_dodge(0.7) 调整柱形图相对位置 ggplot(cabbage_exp, aes(x=Date, y=We
17、ight, fill=Cultivar) + geom_bar(stat=identity, width=0.5, position=position_dodge(0.7) 29 Geom_text()添加数据标签 ggplot(cabbage_exp, aes(x=interaction(Date, Cultivar), y=Weight) + geom_bar(stat=identity) + geom_text(aes(label=Weight), vjust=1.5, colour=white) ggplot(cabbage_exp, aes(x=interaction(Date, C
18、ultivar), y=Weight) + geom_bar(stat=identity) + geom_text(aes(label=Weight), vjust=-0.2) 30 Xlim,ylim设置xy的距离 ggplot(cabbage_exp, aes(x=interaction(Date, Cultivar), y=Weight) + geom_bar(stat=identity) + geom_text(aes(label=Weight), vjust=-0.2) + ylim(0, max(cabbage_exp$Weight) * 1.05) 31 添加2组数据标签 lib
19、rary(plyr) ce - arrange(cabbage_exp, Date, Cultivar) ce - ddply(ce, Date, transform, label_y=cumsum(Weight) ggplot(ce, aes(x=Date, y=Weight, fill=Cultivar) + geom_bar(stat=identity) + geom_text(aes(y=label_y, label=Weight), vjust=1.5, colour=white) 32 使用paste形成想要的lable library(plyr) ce - arrange(cab
20、bage_exp, Date, Cultivar) ce - ddply(ce, Date, transform, label_y=cumsum(Weight) ggplot(ce, aes(x=Date, y=Weight, fill=Cultivar) + geom_bar(stat=identity) + geom_text(aes(y=label_y, label=paste(format(Weight, nsmall=2), kg), size=4) + guides(fill=guide_legend(reverse=TRUE) + scale_fill_brewer(palett
21、e=Pastel1) 33 Geom_errobar柱形图添加误差线 ce - subset(cabbage_exp, Cultivar = c39) ggplot(ce, aes(x=Date, y=Weight) + geom_bar(fill=white, colour=black) + geom_errorbar(aes(ymin=Weight-se, ymax=Weight+se), width=.2) 34 coord_flip()坐标系翻转 ggplot(PlantGrowth, aes(x=group, y=weight) + geom_boxplot() + coord_fl
22、ip() 35 coord_polar()柱形图升级台风图 ggplot(wind, aes(x=DirCat, fill=SpeedCat) + geom_histogram(binwidth=15, origin=-7.5) + coord_polar() + scale_x_continuous(limits=c(0,360) 36 coord_fixed()XY轴比例相同 sp - ggplot(marathon, aes(x=Half,y=Full) + geom_point() sp + coord_fixed() 37 scale_y_continuous配合break设置x,y
23、坐标 轴刻度 sp + coord_fixed() + scale_y_continuous(breaks=seq(0, 420, 30) + scale_x_continuous(breaks=seq(0, 420, 30) ggplot(PlantGrowth, aes(x=group, y=weight) + geom_boxplot() + scale_y_continuous(breaks=c(4, 4.25, 4.5, 5, 6, 8) 38坐标刻度用字符 ggplot(PlantGrowth, aes(x=group, y=weight) + geom_boxplot() + s
24、cale_x_discrete(limits=c(trt2, ctrl), breaks=ctrl) 39 去除Y的坐标系 p - ggplot(PlantGrowth, aes(x=group, y=weight) + geom_boxplot() p + theme(axis.text.y = element_blank() 40 breaks=NULL去除Y的刻度 p + scale_y_continuous(breaks=NULL) 41 scale_y_continuous修改刻度的lable hwp + scale_y_continuous(breaks=c(50, 56, 60,
25、 66, 72), labels=c(“Tiny”, “Reallynshort”, “Short”, Medium, Tallish) 41 theme(axis.text.x = element_text(angle=90, hjust=1, vjust=.5) 调整lable的角度 bp - ggplot(PlantGrowth, aes(x=group, y=weight) + geom_boxplot() + scale_x_discrete(breaks=c(ctrl, trt1, trt2), labels=c(Control, Treatment 1, Treatment 2)
26、 bp + theme(axis.text.x = element_text(angle=90, hjust=1, vjust=.5) 42 调整lable角度为30 bp + theme(axis.text.x = element_text(angle=30, hjust=1, vjust=1) 43theme(axis.text.x = element_text修改字体 和颜色 bp + theme(axis.text.x = element_text(family=Times, face=italic, colour=darkred, size=rel(0.9) 45Xlab和 ylab
27、添加标题 hwp - ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=sex) +geom_point() hwp + xlab(Age in years) + ylab(Height in inches) 46去除x轴标题 p - ggplot(PlantGrowth, aes(x=group, y=weight) + geom_boxplot() p + theme(axis.title.x=element_blank() 47修改title的字体 hwp - ggplot(heightweight, aes(x=ageYear
28、, y=heightIn) + geom_point() hwp + theme(axis.title.x=element_text(face=italic, colour=darkred, size=14) 48 theme(axis.line =element_line(colour=black) 坐标轴改颜色 p - ggplot(heightweight, aes(x=ageYear, y=heightIn) + geom_point() p + theme(axis.line = element_line(colour=black) 49 theme_bw()背景颜色去掉 p + t
29、heme_bw() + theme(panel.border = element_blank(), axis.line = element_line(colour=black) 50 调整XY坐标轴粗细 p + theme_bw() + theme(panel.border = element_blank(), axis.line = element_line(colour=black, size=4, lineend=square) 51 柱形图起手 ggplot(faithful, aes(x=waiting) + geom_histogram() 52 Binwidth设置每个bin的宽
30、度 ggplot(faithful, aes(x=waiting) +geom_histogram(binwidth=5, fill=white, colour=black) Orign设定图形起始位置 h - ggplot(faithful, aes(x=waiting) h + geom_histogram(binwidth=8, fill=white, colour=black, origin=31) h + geom_histogram(binwidth=8, fill=white, colour=black, origin=35) 使用facet_grid(x .)分页 ggplot
31、(birthwt, aes(x=bwt) + geom_histogram(fill=white, colour=black) + facet_grid(smoke .) 两因素,相互覆盖的柱形图 ggplot(birthwt1, aes(x=bwt, fill=smoke) + geom_histogram(position=identity, alpha=0.4) 用密度曲线看数据分布 ggplot(faithful, aes(x=waiting) + geom_density() ggplot(faithful, aes(x=waiting) + geom_line(stat=densi
32、ty) 使用adjust调整密度的曲线 ggplot(faithful, aes(x=waiting) + geom_line(stat=density, adjust=.25, colour=red) + geom_line(stat=density) + geom_line(stat=density, adjust=2, colour=blue) 柱形图密度图结合y=.density. ggplot(faithful, aes(x=waiting, y=.density.) + geom_histogram(fill=cornsilk, colour=grey60, size=.2) +
33、geom_density() + xlim(35, 105) 数据框因子替换 library(MASS) birthwt1 - birthwt birthwt1$smoke - factor(birthwt1$smoke) birthwt1$smoke - revalue(birthwt1$smoke, c(0=No Smoke, 1=Smoke) 频率多变图geom_freqpoly ggplot(faithful, aes(x=waiting) + geom_freqpoly() ggplot(faithful, aes(x=waiting) + geom_freqpoly(binwidt
34、h=4) width设置箱子图的宽度 ggplot(birthwt, aes(x=factor(race), y=bwt) + geom_boxplot(width=.5) outlier.size设置箱形图异常值的大小和形状 ggplot(birthwt, aes(x=factor(race), y=bwt) + geom_boxplot(outlier.size=1, outlier.shape=21) notch增加凹口 ggplot(birthwt, aes(x=factor(race), y=bwt) + geom_boxplot(notch=TRUE) geom_violin()画
35、小提琴图 p - ggplot(heightweight, aes(x=sex, y=heightIn) p + geom_violin() 图形整合 p + geom_violin(fill=yellow) + geom_boxplot(width=.1, fill=black, outlier.colour=NA) + stat_summary(fun.y=median, geom=point, fill=white, shape=21, size=2.5) Geom_line画线图 ggplot(BOD, aes(x=Time, y=demand) + geom_line() 因子化后需
36、要添加group参数 BOD1 - BOD # Make a copy of the data BOD1$Time - factor(BOD1$Time) ggplot(BOD1, aes(x=Time, y=demand, group=1) + geom_line() 线点结合,添加多个图层 ggplot(BOD, aes(x=Time, y=demand) + geom_line() +geom_point() scale_y_log10()对y轴进行log化 ggplot(worldpop, aes(x=Year, y=Population) + geom_line() + geom_p
37、oint() ggplot(worldpop, aes(x=Year, y=Population) + geom_line() + geom_point() + scale_y_log10() Linetype改变线形状 ggplot(tg, aes(x=dose, y=length, colour=supp) + geom_line() ggplot(tg, aes(x=dose, y=length, linetype=supp) + geom_line() 设置Linetype ggplot(BOD, aes(x=Time, y=demand) + geom_line(linetype=d
38、ashed, size=1, colour=blue) geom_area()画面积图 sunspotyear - data.frame( Year = as.numeric(time(sunspot.year), Sunspots = as.numeric(sunspot.year) ) ggplot(sunspotyear, aes(x=Year, y=Sunspots) + geom_area() 画面积图 sunspotyear - data.frame( Year = as.numeric(time(sunspot.year), Sunspots = as.numeric(sunsp
39、ot.year) ) ggplot(sunspotyear, aes(x=Year, y=Sunspots) + geom_area() ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup) + geom_area() 画置信区间 geom_ribbon画置信区间 ggplot(clim, aes(x=Year, y=Anomaly10y) + geom_ribbon(aes(ymin=Anomaly10y-Unc10y, ymax=Anomaly10y+Unc10y), alpha=0.2) + geom_line() 采用虚线画置
40、信区间 ggplot(clim, aes(x=Year, y=Anomaly10y) + geom_line(aes(y=Anomaly10y-Unc10y), colour=grey50, linetype=dotted) + geom_line(aes(y=Anomaly10y+Unc10y), colour=grey50, linetype=dotted) + geom_line() 添加公式,注意平方的写法 p - ggplot(data.frame(x=c(-3,3), aes(x=x) + stat_function(fun = dnorm) p + annotate(text,
41、x=2, y=0.3, parse=TRUE, label=frac(1, sqrt(2 * pi) * e -x2 / 2) Geom_hline等给图形添加线 p - ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=sex) + geom_point() p + geom_hline(yintercept=60) + geom_vline(xintercept=14) geom_abline画斜线 p + geom_abline(intercept=37.4, slope=1.75) 在某一个变量处加线 pg + geom_vl
42、ine(xintercept = which(levels(PlantGrowth$group)=ctrl) Annotate的gegment添加线段 p - ggplot(subset(climate, Source=Berkeley), aes(x=Year, y=Anomaly10y) + geom_line() p + annotate(segment, x=1950, xend=1980, y=-.25, yend=-.25) 通过annotateannotate添加箭头 library(grid) p + annotate(segment, x=1850, xend=1820, y
43、=-.8, yend=-.95, colour=blue, size=2, arrow=arrow() 复杂化的箭头添加 library(grid) p + annotate(segment, x=1850, xend=1820, y=-.8, yend=-.95, colour=blue, size=2, arrow=arrow() + annotate(segment, x=1950, xend=1980, y=-.25, yend=-.25, arrow=arrow(ends=both, angle=90, length=unit(.2,cm) Annotate的rect添加阴影 p +
44、 annotate(rect, xmin=1950, xmax=1980, ymin=-1, ymax=1, alpha=.1, fill=blue) ggtitle添加标题 p - ggplot(heightweight, aes(x=ageYear, y=heightIn) + geom_point() p + ggtitle(Age and Height of Schoolchildren) p + ggtitle(Age and Heightnof Schoolchildren) Theme设置title的位置 p + ggtitle(Age and Height of Schoolc
45、hildren) + theme(plot.title=element_text(vjust = -2.5) 修改字体 p + theme(axis.title.x=element_text(size=16, lineheight=.9, family=Times, face=bold.italic, colour=red) 文本修改的参数 使用白色的背景主题 p - ggplot(heightweight, aes(x=ageYear, y=heightIn) + geom_point() p + theme_bw() Theme设置网格线 p - ggplot(heightweight,
46、aes(x=ageYear, y=heightIn, colour=sex) + geom_point() p + theme( panel.grid.major = element_line(colour=red), panel.grid.minor = element_line(colour=red, linetype=dashed, size=0.2), panel.background = element_rect(fill=lightblue), panel.border = element_rect(colour=blue, fill=NA, size=2) theme修改每个标题
47、 p + ggtitle(Plot title here) + theme( axis.title.x = element_text(colour=red, size=14), axis.text.x = element_text(colour=blue), axis.title.y = element_text(colour=red, size=14, angle = 90), axis.text.y = element_text(colour=blue), plot.title = element_text(colour=red, size=20, face=bold) theme修改标签
48、 p + theme( legend.background = element_rect(fill=grey85, colour=red, size=1), legend.title = element_text(colour=blue, face=bold, size=14), legend.text = element_text(colour=red), legend.key = element_rect(colour=blue, size=0.25) theme修改分页选项 p + facet_grid(sex .) + theme( strip.background = element
49、_rect(fill=pink), strip.text.y = element_text(size=14, angle=-90, face=bold) p + theme_bw() + theme(axis.title.x = element_text(colour=red, size=12) 隐藏网格线 p - ggplot(heightweight, aes(x=ageYear, y=heightIn) + geom_point() p + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank() 通过guides标签去除 p - ggplot(PlantGrowth, aes(x=group, y=weight, fill=group) + geom_boxplot() p + guides(fill=FALSE) p + theme(legend.position=none) 通过theme设置legend的位置 p - ggplot(PlantGrowth, aes(x=group, y=weight, fill=group) + geom_boxplo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 根治性肾输尿管全长切除术后护理查房
- 家庭教育辅导与儿童心理成长指南
- 多场景活动策划流程及实施指导书
- 优化医疗安全保障患者利益承诺书7篇范文
- 广东省潮州市湘桥区2026届初三下学期第三次监测英语试题含解析
- 天津市河东区天铁一中学2026届初三下学期第五次过关考试物理试题含解析
- 贵州遵义市正安县重点中学2026年初三(下)入学语文试题试卷(9月份)含解析
- 随州市重点中学2025-2026学年初三下期末质量调研(一模)物理试题含解析
- 系统故障处理进展回复函4篇范本
- 会员优惠活动规则说明7篇范文
- 2026年采购经理岗位面试指南与经典问题集
- 中国低空经济与管理 课件全套 第1-15章 低空与低空经济-低空经济发展思考
- 采购风险防范措施报告
- CFG桩截桩施工技术交底
- 2025年《检验检测机构资质认定》知识考试题库及答案解析
- 海上设施直升机甲板摩擦系数测试细则
- 江苏中烟工业有限责任公司考试真题2025
- 输尿管支架植入术课件
- 2025安徽芜湖皖南医学院第一附属医院(皖南医学院弋矶山医院)补充招聘工作人员5人笔试备考试题及答案解析
- 2025年客运车辆驾驶员(技师)职业技能鉴定考试题库(含答案)
- 电梯使用单位电梯安全总监和安全员考试题库及答案
评论
0/150
提交评论