版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年高職醫(yī)學影像(影像診斷基礎)試題及答案
- 2025年高職(無人機應用技術)航拍測繪數(shù)據(jù)處理試題及答案
- 2025年高職成本核算(會計實務)試題及答案
- 2025年大學航空技術(航空概論基礎)試題及答案
- 2025年大學本科(學前教育)幼兒游戲設計與指導試題及答案
- 2025年大學二年級(土壤學)土壤學基礎試題及答案
- 2025年高職(寵物醫(yī)療技術)寵物外傷縫合試題及答案
- 2025年高職有色金屬材料(有色報告編寫)試題及答案
- 2025年高職稅務(稅務籌劃基礎)試題及答案
- 2025年中職(智能設備維修)設備檢修階段測試題及答案
- 鐵路鐵鞋管理辦法
- 安防監(jiān)控系統(tǒng)維護與管理方案
- 2025屆重慶八中學七上數(shù)學期末復習檢測模擬試題含解析
- 2025年廣東省中考語文試卷真題(含答案解析)
- 燙熨治療法講課件
- 2025至2030中國模塊化變電站行業(yè)發(fā)展趨勢分析與未來投資戰(zhàn)略咨詢研究報告
- 電廠清潔生產(chǎn)管理制度
- 2025年江蘇省事業(yè)單位招聘考試教師招聘體育學科專業(yè)知識試題
- 機械設計年終述職報告
- 可信數(shù)據(jù)空間解決方案星環(huán)科技
- 建筑工程監(jiān)理服務承諾書范文
評論
0/150
提交評論