首页 ggplot2-cheatsheet(1)

ggplot2-cheatsheet(1)

举报
开通vip

ggplot2-cheatsheet(1)ggplot2-cheatsheetGeoms-Useageomtorepresentdatapoints,usethegeom'saestheticpropertiestorepresentvariables.Eachfunctionreturnsalayer.TwoVariablesASi+geom_hex()x,y,alpha,colour,fillsizeniLLcoordinatesystema□Cf+geom_text(aes(label=cty))x,y,label,alpha,ang(le,co|<...

ggplot2-cheatsheet(1)
ggplot2-cheatsheetGeoms-Useageomtorepresentdatapoints,usethegeom'saestheticpropertiestorepresentvariables.Eachfunctionreturnsalayer.TwoVariablesASi+geom_hex()x,y,alpha,colour,fillsizeniLLcoordinatesystema□Cf+geom_text(aes(label=cty))x,y,label,alpha,ang(le,co|+geom_bar()x<^ipohiia,color,fill,linetype,size,weighte+geom_segment(aes(xend=long+delta_long,yend=lat+delta_lat))x,xend,y,yend,alpha,color,linetype,sizee+geom_rect(aes(xmin=long,ymin=lat,xmax=long+delta_long,ymax=lat+delta_lat))xmax,xmin,ymax,ymin,alpha,color,fill,linetype,sized<-))gK^lc>)^l^<^co>nomics,aes(date,unemploy))d+geom_path(lineend="butt",(inejoin="round',linemteeA)xmUsescalefunctionstoupdatelegendlabelsDiscrete<-b+geom_bar(aes(fill=fl))n■scale_fill_brewer(palette="Blues")Forpalettechoices:library(RcolorBrewer)display.brewer.all()n■scale_fill_grey(start=0.2,end=0.8,na.value="red")IT"1aht"rigor**'•□nEachpositionadjustmentcanberecastasafunctionwithmanualwidthandheightargumentss+geom_barposition=position_dodge(width=1))geom_point(aes(shape=fl))pscale_shape(solid=FALSE)pscale_shape_manual(values=c(3:7))Shapevaluesshowninchartonright24A25V1□2O2A3—4X5O12E013ix)14015B16®17#18*19#20•21•22□23#Themes—;Withoutclipping(preferred)t+coord_cartesian(xlim=c(0,100),ylim=c(10,20))■theme_classic()Whitebackgroundnogridlines■theme_minimal()Minimalthemer■theme_bw()Whitebackgroundwithgridlinesr■theme_grey()Greybackground(defaulttheme)JJoOoO«+vJJWithclipping(removesunseendatapoints)t+xlim(0,100)+ylim(10,20)t■scale_x_continuous(limits=c(0,100))+scale_y_continuous(limits=c(0,100))0q<-fgeom_point(aes(size=cyl))ggthemes-Packagewithadditionalggplot2themesLIm+stat_contour(aes(z=z))3Variablesx,y,z,order|..level..m+stat_spoke(aes(radius=z,angle=z))angle,radius,x,xend,y,yend|..x..,..xend..,..y..,..yend..m+stat_summary_hex(aes(z=z),bins=30,fun=mean)x,y,z,fill|..value..m+stat_summary2d(aes(z=z),bins=30,fun=mean)x,y,z,fill|..value..g+stat_boxplot(coef=1.5)Comparisonsx,y|..lower..,..middle..,..upper..,..outliers..g+stat_ydensity(adjust=1,kernel="gaussian",scale="area")x,y|..density..,..scaled..,..count..,..n..,..violinwidth..,..width..f+stat_ecdf(n=40)Functionsx,y|..x..,..y..fstat_quantile(quantiles=c(0.25,0.5,0.75),formula=y~log(x),method="rq")x,y|..quantile..,..x..,..y..fstat_smooth(method="auto",formula=y~x,se=TRUE,n=80,fullrange=FALSE,level=0.95)x,y|..se..,..x..,..y..,..ymin..,..ymax..ggplot()+stat_function(aes(x=-3:3),fun=dnorm,n=101,args=list(sd=0.5))x|..y..fstat_identity()ggplot()+stat_qq(aes(sample=1:100),distribution=qt,dparams=list(df=5))sample,x,y|..x..,..y..fstat_sum()x,y,size|..size..fstat_summary(fun.data="mean_cl_boot")fstat_unique()Eachstatcreatesadditionalvariablestomapaestheticsto.Thesevariablesuseacommon..name..syntax.statfunctionsandgeomfunctionsbothcombineastatwithageomtomakealayer,i.e.stat_bin(geom="bar")doesthesameasgeom_bar(stat="bin")Someplotsvisualizeatransformationoftheoriginaldataset.Useastattochooseacommontransformationtovisualize,e.g.a+geom_bar(stat="bin")Iflllctycyl|a+stat_bin(binwidth=1,origin=10)x,y|..count..,..ncount..,..density..,..ndensity..a+stat_bindot(binwidth=1,binaxis="x")x,y,|..count..,..ncount..a+stat_density(adjust=1,kernel="gaussian")x,y,|..count..,..density..,..scaled..fstat_bin2d(bins=30,drop=TRUE)x,y,fill|..count..,..density..fstat_binhex(bins=30)x,y,fill|..count..,..density..fstat_density2d(contour=TRUE,n=100)x,y,color,size|..level..GeneralPurpose□geomcoordinatex=xsystem\/=rniinl"2DdistributionsGeneralPurposescalesUsewithanyaesthetic:alpha,color,fill,linetype,shape,sizescale_*_continuous()-mapcont'valuestovisualvaluesscale_*_discrete()-mapdiscretevaluestovisualvaluesscale_*_identity()-usedatavaluesasvisualvaluesscale_*_manual(values=c())-mapdiscretevaluestomanuallychosenvisualvaluesXandYlocationscalesUsewithxoryaesthetics(xshownhere)scale_x_date(labels=date_format("%m/%d"),breaks=date_breaks("2weeks"))-treatxvaluesasdates.See?strptimeforlabelformats.scale_x_datetime()-treatxvaluesasdatetimes.Usesameargumentsasscale_x_date().scale_x_log10()-Plotxonlog10scalescale_x_reverse()-Reversedirectionofxaxisscale_x_sqrt()-PlotxonsquarerootscaleScalescontrolhowaplotmapsdatavaluestothevisualvaluesofanaesthetic.Tochangethemapping,addacustomscale.n<-b+geom_bar(aes(fill=fl))Continuouso<-a+geom_dotplot(aes(fill=..x..))oscale_fill_gradient(low="red",high="yellow")oscale_fill_gradient2(low="red",hight="blue",mid="white",midpoint=25)oscale_fill_gradientn(colours=terrain.colors(6))Also:rainbow(),heat.colors(),topo.colors(),cm.colors(),RColorBrewer::brewer.pal()ShapescalesManualshapevaluesColorandfillscales■6V708:9<10®11$qscale_size_area(max=6)Valuemappedtoareaofcircle(notradius)Sizescalesr<-b+geom_bar()r+coord_cartesian(xlim=c(0,5))xlim,ylimThedefaultcartesiancoordinatesystemr+coord_fixed(ratio=1/2)ratio,xlim,ylimCartesiancoordinateswithfixedaspectratiobetweenxandyunitsjj11111r+coord_flip()Lxlim,ylim^^■—1FlippedCartesiancoordinatesr+coord_polar(theta="x",direction=1)theta,start,directionPolarcoordinatesr+coord_trans(ytrans="sqrt")■xtrans,ytrans,limx,limyHBITransformedcartesiancoordinates.Setextrasandstrainstothenameofawindowfunction.z+coord_map(projection="ortho",orientation=c(41,-74,0))projection,orientation,xlim,ylimPositionadjustmentsdeterminehowtoarrangegeomsthatwouldotherwiseoccupythesamespace.s<-ggplot(mpg,aes(fl,fill=drv))s+geom_baiposition="dodge"Arrangeelementssidebysides+geom_ba!position="fill")Stackelementsontopofoneanother,normalizeheightSs+geom_ba!position="stack")Stackelementsontopofoneanotherf+geom_point(position="jitter")AddrandomnoisetoXandYpositionofeachelementtoavoidoverplottingPositionAdjustmentsMapprojectionsfromthemapprojpackage(mercator(default),azequalarea,lagrange,etc.)Facetsdivideaplotintosubplotsbasedonthevaluesofoneormorediscretevariables.t<-ggplot(mpg,aes(cty,hwy))+geom_point()t+facet_grid(.~fl)facetintocolumnsbasedonflt+facet_grid(year~.)facetintorowsbasedonyearIt+facet_grid(year~fl)facetintobothrowsandcolumnst+facet_wrap(~fl)wrapfacetsintoarectangularlayoutSetscalestoletaxislimitsvaryacrossfacetst+facet_grid(y~xscales="free")xandyaxislimitsadjusttoindividualfacets•"free_x"-xaxislimitsadjust•"free_y"-yaxislimitsadjustSetlabellertoadjustfacetlabelst+facet_grid(.~fl,labeller=label_both)fl:cfl:dfl:efl:pfl:rt+facet_grid(.~fl,labeller=label_bquote(alphaA(x)))acadaeapart+facet_grid(.~fl,labeller=label_parsed)cdeprt+ggtitle("NewPlotTitle")Addamaintitleabovetheplott+xlab("NewXlabel")ChangethelabelontheXaxist+ylab("NewYlabel")ChangethelabelontheYaxist+labs(title="Newtitle",x="Newx",y="Newy")Alloftheabovet+theme(legend.position="bottom1Placelegendat"bottom","top","left",t+guides(color="none")Setlegendtypeforeachaesthetic:colorbar,legend,ornone(nolegend)t+scale_fill_discrete(name="Title",labels=c("A","B","C"))Setlegendtitleandlabelswithascalefunction.LegendsZoominLabels")Learnmoreatdocs.ggplot2.org•ggplot20.9.3.1•Updated:3/15RStudio®isatrademarkofRStudio,Inc.•CCBYRStudio•info@rstudio.com•844-448-1212•rstudio.com
本文档为【ggplot2-cheatsheet(1)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_633808
暂无简介~
格式:doc
大小:104KB
软件:Word
页数:0
分类:
上传时间:2021-08-30
浏览量:43