ggplot2图册--一个knitr例子021-ggplot2-geoms_W_第1页
ggplot2图册--一个knitr例子021-ggplot2-geoms_W_第2页
ggplot2图册--一个knitr例子021-ggplot2-geoms_W_第3页
ggplot2图册--一个knitr例子021-ggplot2-geoms_W_第4页
ggplot2图册--一个knitr例子021-ggplot2-geoms_W_第5页
已阅读5页,还剩221页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、G G P L O T 2G A L L E R Yggplot2 gallery109Contentsgeom_abline3geom_area10geom_bar10geom_bin2d25geom_blank27geom_boxplot29geom_contour45geom_crossbar45geom_density45geom_density2d46geom_dotplot46geom_errorbar58geom_errorbarh63geom_freqpoly65geom_hex68geom_histogram68geom_hline96geom_jitter100geom_l

2、ine106geom_linerange121geom_map126geom_path128geom_point145geom_pointrangegeom_polygon15159geom_quantile161geom_raster161geom_rect165geom_ribbon65geom_rug171geom_segment174geom_smooth179geom_step181geom_text185geom_tile196geom_violin206geom_vline220geom_abline# Name: geom_abline# Title: Line specifi

3、ed by slope and intercept. # Aliases: geom_abline# * Examplesp - qplot(wt, mpg, data = mtcars) # Fixed slopes and interceptsp + geom_abline() # Cant see it - outside the range of thedata3530mpg252015102345wt p + geom_abline(intercept = 20)3530mpg252015102345wtp + geom_abline(intercept = 37, slope =

4、-5)wt-5.344#37.285# Calculate slope and intercept of line of best fitcoef(lm(mpg wt, data = mtcars)# (Intercept)3530mpg252015102345wt p + geom_abline(intercept = 10, colour = red, size = 2)3530mpg252015102345wt# See ?stat_smooth for fitting smooth models to data p + stat_smooth(method=lm, se=FALSE)3

5、53025mpg2015102345wt# Slopes and intercepts as datap - ggplot(mtcars, aes(x = wt, y=mpg), . cyl) + geom_point() df - data.frame(a=rnorm(10, 25), b=rnorm(10, 0)p + geom_abline(aes(intercept=a, slope=b), data=df)3530mpg252015102345wt# Slopes and intercepts from linear modellibrary(plyr)coefs - ddply(m

6、tcars, .(cyl), function(df) m - lm(mpg wt, data=df)data.frame(a = coef(m)1, b = coef(m)2)str(coefs)# data.frame: 3 obs. of3 variables: #$ cyl: num4 6 8#$ a: num39.6 28.4 23.9#$ b: num-5.65 -2.78 -2.19p + geom_abline(data=coefs, aes(intercept=a, slope=b)3530mpg252015102345wt# Its actually a bit easie

7、r to do this with stat_smooth p + geom_smooth(aes(group=cyl), method=lm)3530mpg252015102345wt p + geom_smooth(aes(group=cyl), method=lm, fullrange=TRUE)30mpg201002345wt# With coordinate transformsp + geom_abline(intercept = 37, slope = -5) + coord_flip()5wt432101520253035mpg5243 p + geom_abline(inte

8、rcept = 37, slope = -5) + coord_polar() 302520mpg15wtgeom_area# Name: geom_area# Title: Area plot.# Aliases: geom_area # * Examples# see geom_ribbongeom_bar# Name: geom_bar# Title: Bars, rectangles with bases on x-axis # Aliases: geom_bar# * Examples # No test:# Generate datac - ggplot(mtcars, aes(f

9、actor(cyl) c + geom_bar()count1050468factor(cyl) c + geom_bar(width=.5)count1050468factor(cyl) c + geom_bar() + coord_flip()8factor(cyl)640510count c + geom_bar(fill=white, colour=darkgreen)count1050468factor(cyl)# Use qplotqplot(factor(cyl), data=mtcars, geom=bar)count1050468factor(cyl) qplot(facto

10、r(cyl), data=mtcars, geom=bar, fill=factor(cyl)10factor(cyl)count46850468factor(cyl)# Stacked bar chartsqplot(factor(cyl), data=mtcars, geom=bar, fill=factor(vs)10countfactor(vs)0150468factor(cyl) qplot(factor(cyl), data=mtcars, geom=bar, fill=factor(gear)10factor(gear)count34550468factor(cyl)# Stac

11、ked bar charts are easy in ggplot2, but not effective visually,# particularly when there are many different things being stackedggplot(diamonds, aes(clarity, fill=cut) + geom_bar()10000count5000cutFair GoodVery Good Premium Ideal0I1SI2SI1VS2VS1VVS2 VVS1IFclarity ggplot(diamonds, aes(color, fill=cut)

12、 + geom_bar() + coord_flip()JIHcutcolorGFair GoodVery Good Premium IdealFED0300060009000count# Faceting is a good alternative:ggplot(diamonds, aes(clarity) + geom_bar() +facet_wrap( cut)FairGoodVery GoodPremiumIdeal50004000300020001000count0500040003000200010000I1 SI2 SI1VS2VS1VVSV2VS1IFI1 SI2 SI1VS

13、2VS1VVSV2VS1IFclarity# If the x axis is ordered, using a line instead of bars is # possibility:ggplot(diamonds, aes(clarity) +geom_freqpoly(aes(group = cut, colour = cut)another50004000count3000200010000I1SI2SI1VS2VS1VVS2 VVS1IFclaritycutFair GoodVery Good Premium Ideal# Dodged bar chartsggplot(diam

14、onds, aes(clarity, fill=cut) + geom_bar(position=dodge)50004000count30002000cutFair GoodVery Good Premium Ideal10000I1SI2SI1VS2VS1VVS2 VVS1IFclarity# compare withggplot(diamonds, aes(cut, fill=cut) + geom_bar() +facet_grid(. clarity)IFVS1VVS2VVS1VS2SI1SI2I150004000count30002000cutFair GoodVery Good

15、Premium Ideal10000VFGaeiProryoredGmIodioeudamVFGlaeiProryoredGmIodioeudamVFGlaePiroryoredGmIodioeudamVFGlaeiProryoredGmIodioeudamVFGlaeiProryoredGmIodioeudamFVGlaePiorryoredGmIodioeudamVFGlaePiorryoredGmIodioeudamVFGlaeiProryoredGmIodioeudamlcut# But again, probably better to use frequency polygons

16、instead:ggplot(diamonds, aes(clarity, colour=cut) +geom_freqpoly(aes(group = cut)50004000count3000200010000I1SI2SI1VS2VS1VVS2 VVS1IFclaritycutFair GoodVery Good Premium Ideal# Often we dont want the height of the bar to represent the # count of observations, but the sum of some other variable. # For

17、 example, the following plot shows the number of diamonds # of each colourqplot(color, data=diamonds, geom=bar)9000count600030000DEFGHIJcolor# If, however, we want to see the total number of carats in # we need to weight by the carat variableeach colourqplot(color, data=diamonds, geom=bar, weight=ca

18、rat, ylab=carat)7500carat500025000DEFGHIJcolor# A bar chart used to display meansmeanprice - tapply(diamonds$price, diamonds$cut, mean)cut - factor(levels(diamonds$cut), levels = levels(diamonds$cut) qplot(cut, meanprice)46004400meanprice4200400038003600FairGoodVery GoodPremiumIdealcut qplot(cut, me

19、anprice, geom=bar, stat=identity)4000meanprice3000200010000FairGoodVery GoodPremiumIdealcut qplot(cut, meanprice, geom=bar, stat=identity, fill = I(grey50)4000meanprice3000200010000FairGoodVery GoodPremiumIdealcut# Another stacked bar chart examplek - ggplot(mpg, aes(manufacturer, fill=class) k + ge

20、om_bar()30class2seater compactcount20midsizeminivan pickup subcompact suv100audcihevrodleotdgefordhondhayundajieelapnd rovlinercomlnercunryissapnontiascubartuoyvootlakswagenmanufacturer# Use scales to change aesthetics defaults k + geom_bar() + scale_fill_brewer()30class2seater compactcount20midsize

21、minivan pickup subcompact suv100audcihevrodleotdgefordhondhayundajieelapnd rovlinecr omlnercunryissapnontiascubartuoyvootlakswagenmanufacturerk + geom_bar() + scale_fill_grey()30class2seater compactcount20midsizeminivan pickup subcompact suv100audcihevrodleotdgefordhondhayundajieelapnd rovlinecr oml

22、nercunryissapnontiascubartuoyvootlakswagenmanufacturer# To change plot order of class varible # use factor() to change order of levelsmpg$class - factor(mpg$class, levels = c(midsize,minivan,suv, compact, 2seater, subcompact, pickup) m - ggplot(mpg, aes(manufacturer, fill=class)m + geom_bar()30class

23、midsize minivancount20suvcompact 2seater subcompact pickup100audcihevrodleotdgefordhondhayundajieelapnd rovliencr omlnercunryissapnontiascubartuoyvootlakswagenmanufacturer # End(No test)geom_bin2d# Name: geom_bin2d# Title: Add heatmap of 2d bin counts. # Aliases: geom_bin2d# * Examplesd - ggplot(dia

24、monds, aes(x = x, y = y) + xlim(4,10) + ylim(4,10) d + geom_bin2d()108count50004000y3000200010006446810x d + geom_bin2d(binwidth = c(0.1, 0.1)108county200010006446810x # See ?stat_bin2d for more examplesgeom_blank# Name: geom_blank# Title: Blank, draws nothing. # Aliases: geom_blank# * Examplesqplot

25、(length, rating, data = movies, geom = blank)10.07.5rating5.02.5010002000300040005000length# Nothing to see here!# Take the following scatter plota - ggplot(mtcars, aes(x = wt, y = mpg), . cyl) + geom_point() # Add to that some lines with geom_abline()df - data.frame(a = rnorm(10, 25), b = rnorm(10,

26、 0)a + geom_abline(aes(intercept = a, slope = b), data = df)3530mpg252015102345wt# Suppose you then wanted to remove the geom_point layer # If you just remove geom_point, you will get an error b - ggplot(mtcars, aes(x = wt, y = mpg)# Not run: b + geom_abline(aes(intercept = a, slope = b),# Switching

27、 to geom_blank() gets the desired plotc - ggplot(mtcars, aes(x = wt, y = mpg) + geom_blank() c + geom_abline(aes(intercept = a, slope = b), data = df)data = df)3530mpg252015102345wtgeom_boxplot# Name: geom_boxplot# Title: Box and whiskers plot. # Aliases: geom_boxplot# * Examples # No test:p - ggplo

28、t(mtcars, aes(factor(cyl), mpg) p + geom_boxplot()3530mpg25201510468factor(cyl) qplot(factor(cyl), mpg, data = mtcars, geom = boxplot)3530mpg25201510468factor(cyl) p + geom_boxplot() + geom_jitter()3530mpg25201510468factor(cyl) p + geom_boxplot() + coord_flip()8factor(cyl)64101520253035mpgqplot(fact

29、or(cyl), mpg, data = mtcars, geom = boxplot) +coord_flip()8factor(cyl)64101520253035mpgp + geom_boxplot(notch = TRUE)# Warning:notch went outside hinges.Try setting notch=FALSE. # Warning:notch went outside hinges.Try setting notch=FALSE.3530mpg25201510468factor(cyl)p + geom_boxplot(notch = TRUE, no

30、tchwidth = .3)# Warning:notch went outside hinges.Try setting notch=FALSE. # Warning:notch went outside hinges.Try setting notch=FALSE.3530mpg25201510468factor(cyl) p + geom_boxplot(outlier.colour = green, outlier.size = 3)3530mpg25201510468factor(cyl)# Add aesthetic mappings# Note that boxplots are

31、 automatically dodged when any aesthetic is # a factorp + geom_boxplot(aes(fill = cyl)353025cyl8mpg7652041510468factor(cyl) p + geom_boxplot(aes(fill = factor(cyl)353025factor(cyl)mpg468201510468factor(cyl) p + geom_boxplot(aes(fill = factor(vs)353025mpgfactor(vs)01201510468factor(cyl) p + geom_boxp

32、lot(aes(fill = factor(am)353025factor(am)mpg01201510468factor(cyl)# Set aesthetics to fixed valuep + geom_boxplot(fill = grey80, colour = #3366FF)3530mpg25201510468factor(cyl)qplot(factor(cyl), mpg, data = mtcars, geom = boxplot,colour = I(#3366FF)3530mpg25201510468factor(cyl)# Scales vs. coordinate

33、 transforms -# Scale transformations occur before the boxplot statistics # Coordinate transformations occur afterwards. Observe the # number of outliers.library(plyr) # to access round_anym - ggplot(movies, aes(y = votes, x = rating, group = round_any(rating, 0.5)m + geom_boxplot()# Warning: positio

34、n_dodge requires constant width: output may be incorrectare computed. effect on the150000votes1000005000002.55.07.510.0ratingm + geom_boxplot() + scale_y_log10()# Warning:position_dodge requires constant width:output may be incorrectvotes100001002.55.07.510.0ratingm + geom_boxplot() + coord_trans(y

35、= log10)# Warning:position_dodge requires constant width:output may be incorrect1200008000040000votes2.55.07.5ratingm + geom_boxplot() + scale_y_log10() + coord_trans(y = log10)# Warning:position_dodge requires constant width:output may be incorrect10000votes1002.55.07.5rating# Boxplots with continu

36、ous x:# Use the group aesthetic to group observations in boxplotsqplot(year, budget, data = movies, geom = boxplot)# Warning:Removed 53573 rows containing non-finite values (stat_boxplot).2.0e+081.5e+08budget1.0e+085.0e+070.0e+001925195019752000yearqplot(year, budget, data = movies, geom = boxplot, group = round_any(year, 10, floor)# Warning:Removed 53573 rows containing non-finite values (stat_boxplot).# Warning:position_dodge requires constant width:outputmay be incorrect2.0e+081.5e+08budget1.0e+085.0e+070.0e+00190019251950197

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论