收藏 分享(赏)

ggplot例子整理.pptx

上传人:天天快乐 文档编号:764397 上传时间:2018-04-21 格式:PPTX 页数:121 大小:1.91MB
下载 相关 举报
ggplot例子整理.pptx_第1页
第1页 / 共121页
ggplot例子整理.pptx_第2页
第2页 / 共121页
ggplot例子整理.pptx_第3页
第3页 / 共121页
ggplot例子整理.pptx_第4页
第4页 / 共121页
ggplot例子整理.pptx_第5页
第5页 / 共121页
点击查看更多>>
资源描述

1、Using ggplot2,zting整理 2.7,Hadley Wickham 是 RStudio 的首席科学家以及 Rice University 统计系的助理教授。他是著名图形可视化软件包 ggplot2 的开发者,以及其他许多被广泛使用的软件包的作者,代表作品如 plyr、reshape2 等,Layout,概念例子,Layout,数据和映射 ggplot(data,aes(x=,y=,fill=),数据集,映射,Layout,几何对象 :散点图,气泡图,条形图,折线图,箱形图 统计变换 :封箱(bin),identity位置调整:dodge,fill,identity坐标系统: xl

2、im,ylim图层 分面: 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 多因素作图,heigh

3、tweight, 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

4、_point() + 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=weig

5、htLb, 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=gu

6、ide_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 + geom

7、_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, 2

8、50, 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(height

9、weight, 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.42, x=16

10、.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=healthexp+

11、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_manual(valu

12、es=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.major.x =

13、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_blank(), lege

14、nd.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() + theme(pane

15、l.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图形的位置关系,ggp

16、lot(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=Weight, fill=

17、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, Cultivar), y

18、=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组数据标签,library(plyr)c

19、e - 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(cabbage_exp, Date,

20、 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(palette=Pastel1),33 Geo

21、m_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_flip(),35 coord_pola

22、r()柱形图升级台风图,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坐标轴刻度,sp + coord_fi

23、xed() + 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() + scale_x_discrete(limi

24、ts=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, 66, 72), labels=c(“T

25、iny”, “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)bp + theme(axis.text.x

26、 = 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添加标题,hwp - ggplot(height

27、weight, 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, y=heightIn) + geom_point

28、()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 + theme_bw() + theme(panel.borde

29、r = 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的宽度,ggplot(faithful, aes(x=wait

30、ing) +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(birthwt, aes(x=bwt) + geom_hi

31、stogram(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=density),使用adjust调整密度的曲线,ggplot(fai

32、thful, 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) + geom_density() + xlim(35, 105)

33、,数据框因子替换,library(MASS)birthwt1 - birthwtbirthwt1$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(binwidth=4),width设置箱子图的宽度,ggplot(birthwt

34、, 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()画小提琴图,p - ggplot(heightweight, aes

35、(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(),因子化后需要添加group参数,BOD1 - BOD # Make a cop

36、y of the dataBOD1$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_point(),ggplot(worldpop, aes(x=Year,

37、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=dashed, size=1, colour=blue),geom_are

38、a()画面积图,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(sunspot.year)ggplot(sunspotyear, aes(x=Year,

39、 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(),采用虚线画置信区间,ggplot(clim, aes(x=Year, y=Anomaly10y)

40、+ 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, x=2, y=0.3, parse=TRUE, label=frac(1, sqrt(2

41、 * 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_vline(xintercept = which(levels(PlantGrowth$gro

42、up)=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),通过annotate添加箭头,library(grid)p + annotate(segment, x=1850, xend=1820, y=-.8, yend=-.95, colour=blue, size=2, arrow=arrow(),复杂化

43、的箭头添加,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 + annotate(rect, xmin=1950, xmax=1980, ymin=-1, ymax=1, a

44、lpha=.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 Schoolchildren) + theme(plot.title=element_text(vjust = -2.5),修改

45、字体,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, aes(x=ageYear, y=heightIn, colour=sex) + geom_point()p + t

46、heme( 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修改每个标题,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),

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 经营企划

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报