ggplot2代碼文檔.doc_第1頁
ggplot2代碼文檔.doc_第2頁
ggplot2代碼文檔.doc_第3頁
ggplot2代碼文檔.doc_第4頁
ggplot2代碼文檔.doc_第5頁
已閱讀5頁,還剩31頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

附錄代碼library(ggplot2)library(gtable)library(grid) # 需要 editGrob() 函數(shù)p - qplot(wt, mpg, data = mtcars, colour = cyl, main = Title text)p# 修改圖形元件:圖形題目字體改為斜體紅色g - ggplotGrob(p)idx - which(g$layout$name = title)g$grobsidx - editGrob(g$grobsidx, gp = gpar(fontface = italic, col = red)# 重新繪制grid.draw(g)第一章代碼:第二章代碼:library(ggplot2)# 圖2.1 無代碼# 章節(jié)2.2set.seed(1410) # 讓樣本可重復dsmall - diamondssample(nrow(diamonds), 100), # 章節(jié)2.3qplot(carat, price, data = diamonds)# 章節(jié)2.3qplot(log(carat), log(price), data = diamonds)# 章節(jié)2.3qplot(carat, x * y * z, data = diamonds)# 圖2.2 將 color 變量映射到點的顏色 (左),cut 變量映射到點的形狀 (右)qplot(carat, price, data = dsmall, colour = color)qplot(carat, price, data = dsmall, shape = cut)# 圖2.3 將 alpha 值從 1/10(左) 變動到 1/100(中) 再到# 1/200(右),來看大部分的點在哪里 進行重疊。qplot(carat, price, data = diamonds, alpha = I(1/10)qplot(carat, price, data = diamonds, alpha = I(1/100)qplot(carat, price, data = diamonds, alpha = I(1/200)# 圖2.4# 重量與價格的散點圖中加入了平滑曲線。左圖為dsmall數(shù)據(jù)集,右圖為完整數(shù)據(jù)集。qplot(carat, price, data = dsmall, geom = c(point, smooth)qplot(carat, price, data = diamonds, geom = c(point, smooth)# 圖2.5 span 參數(shù)的作用。左圖是 span=0.2,右圖是 span=1。qplot(carat, price, data = dsmall, geom = c(point, smooth), span = 0.2)qplot(carat, price, data = dsmall, geom = c(point, smooth), span = 1)# 圖2.6 在運用廣義可加模型作為平滑器時 formula 參數(shù)的作用。左圖是# formula=ys( x),右圖是 formula=ys(x,bs=cs)。library(mgcv)qplot(carat, price, data = dsmall, geom = c(point, smooth), method = gam, formula = y s(x)qplot(carat, price, data = dsmall, geom = c(point, smooth), method = gam, formula = y s(x, bs = cs)# 圖2.7 在運用線性模型作為平滑器時 formula 參數(shù)的作用。左圖是 formula =# y x 的默認值, 右圖是 formula = y ns(x, 5)。library(splines)qplot(carat, price, data = dsmall, geom = c(point, smooth), method = lm)qplot(carat, price, data = dsmall, geom = c(point, smooth), method = lm, formula = y ns(x, 5)# 圖2.8 (書中無代碼)# 利用擾動點圖(左)和箱線圖(右)來考察以顏色為條件的每克拉價格的分布。# 隨著顏色的改變(從左到右),每克拉價格的跨度逐漸減小,但分布的中位數(shù)沒有明顯的變化。qplot(color, price/carat, data = diamonds, geom = jitter)qplot(color, price/carat, data = diamonds, geom = boxplot)# 圖2.9 改變 alpha 的取值,從左到右分別為 1/5,1/50 和# 1/200。隨著不透明度的降低,# 我們可以看出數(shù)據(jù)集中的地方。然而,箱線圖依然是一個更好的選擇。qplot(color, price/carat, data = diamonds, geom = jitter, alpha = I(1/5)qplot(color, price/carat, data = diamonds, geom = jitter, alpha = I(1/50)qplot(color, price/carat, data = diamonds, geom = jitter, alpha = I(1/200)# 圖2.10 展示鉆石重量的分布。左圖使用的是 geom=histogram 右圖使用的是# geom= density。qplot(carat, data = diamonds, geom = histogram)qplot(carat, data = diamonds, geom = density)# 圖2.11 變動直方圖的組距可以顯示出有意思的模式。從左到右,組距分別為# 1,0.1 和 0.01。只有重量在 0 到 3 克拉之間的鉆石顯示在圖中。qplot(carat, data = diamonds, geom = histogram, binwidth = 1, xlim = c(0, 3)qplot(carat, data = diamonds, geom = histogram, binwidth = 0.1, xlim = c(0, 3)qplot(carat, data = diamonds, geom = histogram, binwidth = 0.01, xlim = c(0, 3)# 圖2.12# 當一個分類變量被映射到某個圖形屬性上,幾何對象會自動按這個變量進行拆分。# 左圖是重疊的密度曲線圖,右圖是堆疊起來的直方圖。qplot(carat, data = diamonds, geom = density, colour = color)qplot(carat, data = diamonds, geom = histogram, fill = color)# 圖2.13 鉆石顏色的條形圖。左圖顯示的是分組的計數(shù),右圖是按 weight=carat# 進行加 權(quán),展示了每種顏色的鉆石的總重量。qplot(color, data = diamonds, geom = bar)qplot(color, data = diamonds, geom = bar, weight = carat) + scale_y_continuous(carat)# 圖2.14# 衡量失業(yè)程度的兩張時序圖。左圖是失業(yè)人口的比例,右圖是失業(yè)星期數(shù)的中位# 數(shù)。圖形是用 geom=line 進行繪制的。qplot(date, unemploy/pop, data = economics, geom = line)qplot(date, uempmed, data = economics, geom = line)# 圖2.15# 展示失業(yè)率和失業(yè)時間長度之間關系的路徑圖。左圖是重疊在一起的的散點圖和路# 徑圖,右圖只有路徑圖,其中年份用顏色進行了展示。year - function(x) as.POSIXlt(x)$year + 1900qplot(unemploy/pop, uempmed, data = economics, geom = c(point, path)qplot(unemploy/pop, uempmed, data = economics, geom = path, colour = year(date)# 圖2.16# 展示以顏色為條件的重量的直方圖。左圖展示的是頻數(shù),右圖展示的是頻率。頻率# 圖可以使得比較不同組的分布時不會受該組樣本量大小的影響。高質(zhì)量的鉆石# (顏色 D) 在小# 尺寸上的分布是偏斜的,而隨著質(zhì)量的下降,重量的分布會變得越來越平坦。qplot(carat, data = diamonds, facets = color ., geom = histogram, binwidth = 0.1, xlim = c(0, 3)qplot(carat, .density., data = diamonds, facets = color ., geom = histogram, binwidth = 0.1, xlim = c(0, 3)# 章節(jié)2.7qplot(carat, price, data = dsmall, xlab = Price ($), ylab = Weight (carats), main = Price-weight relationship)# 章節(jié)2.7qplot(carat, price/carat, data = dsmall, ylab = expression(frac(price, carat), xlab = Weight (carats), main = Small diamonds, xlim = c(0.2, 1)# 章節(jié)2.7qplot(carat, price, data = dsmall, log = xy)第三章代碼:library(ggplot2)# 圖3.1 發(fā)動機排量(以升為單位 displ)對高速公路耗油量(英里每加侖# hwy)散點圖。點# 根據(jù)汽缸數(shù)目著色。該圖可以發(fā)現(xiàn)影響燃油經(jīng)濟性最重要的因素:發(fā)動機排量大# 小。qplot(displ, hwy, data = mpg, colour = factor(cyl)# 圖3.2 :無代碼# 圖3.3 更復雜的圖形一般沒有特定的名稱。這幅圖在圖3.1的基礎上對每個# 組添加了回歸線。這個圖應該叫什么名字呢?qplot(displ, hwy, data = mpg, colour = factor(cyl) + geom_smooth(data = subset(mpg, cyl != 5), method = lm)# 圖3.6 一個含有分面和多個圖層的復雜圖形qplot(displ, hwy, data = mpg, facets = . year) + geom_smooth()# 圖3.8# 四種不同標度的圖例。從左到右依次是:連續(xù)型變量映射到大小和顏色,離散型變# 量映射到形狀和顏色。x - 1:10y - factor(letters1:5)qplot(x, x, size = x)qplot(x, x, 1:10, colour = x)qplot(y, y, 1:10, shape = y)qplot(y, y, 1:10, colour = y)# 圖3.9# 三種不同坐標系的坐標軸和網(wǎng)格線:笛卡爾(Cartesian)、半對數(shù)(semi-log)和極# 坐標系(polar)。極坐標系展示了非笛卡爾坐標系的缺點:很難畫好坐標軸。x1 - c(1, 10)y1 - c(1, 5)p - qplot(x1, y1, geom = blank, xlab = NULL, ylab = NULL) + theme_bw()pp + coord_trans(y = log10)p + coord_polar()p - qplot(displ, hwy, data = mpg, colour = factor(cyl)summary(p)# 保存圖形對象save(p, file = plot.rdata)# 讀入圖形對象load(plot.rdata)# 將圖片保存成png格式ggsave(plot.png, width = 5, height = 5) 第四章代碼library(ggplot2)# 通過ggplot創(chuàng)建圖形對象p - ggplot(diamonds, aes(carat, price, colour = cut)# 添加“點”幾何對象p - p + layer(geom = point)# 例:手動創(chuàng)建圖形對象并添加圖層p - ggplot(diamonds, aes(x = carat)p - p + layer(geom = bar, geom_params = list(fill = steelblue), stat = bin, stat_params = list(binwidth = 2)p# 應用“快捷函數(shù)”,得到與上例相同的圖形p + geom_histogram(binwidth = 2, fill = steelblue)# 在用ggplot創(chuàng)建的圖形對象上添加圖層ggplot(msleep, aes(sleep_rem/sleep_total, awake) + geom_point()# 等價于qplot(sleep_rem/sleep_total, awake, data = msleep)# 也可以給qplot添加圖層qplot(sleep_rem/sleep_total, awake, data = msleep) + geom_smooth()# 等價于qplot(sleep_rem/sleep_total, awake, data = msleep, geom = c(point, smooth)# 或ggplot(msleep, aes(sleep_rem/sleep_total, awake) + geom_point() + geom_smooth()# 例:summary給出圖形對象的默認設置和每個圖層的信息p - ggplot(msleep, aes(sleep_rem/sleep_total, awake)summary(p)p - p + geom_point()summary(p)# 例:用不同的數(shù)據(jù)初始化后添加相同的圖層library(scales)bestfit - geom_smooth(method = lm, se = F, colour = alpha(steelblue, 0.5), size = 2)qplot(sleep_rem, sleep_total, data = msleep) + bestfitqplot(awake, brainwt, data = msleep, log = y) + bestfitqplot(bodywt, brainwt, data = msleep, log = xy) + bestfit# 用%*%添加新的數(shù)據(jù)集來代替原來的數(shù)據(jù)集p - ggplot(mtcars, aes(mpg, wt, colour = cyl) + geom_point()pmtcars - transform(mtcars, mpg = mpg2)p %+% mtcars# aes函數(shù)的參數(shù)aes(x = weight, y = height, colour = age)# 也可以使用變量的函數(shù)值作為參數(shù)aes(weight, height, colour = sqrt(age)p - ggplot(mtcars)summary(p)p - p + aes(wt, hp)summary(p)# 使用默認的參數(shù)映射來添加圖層p - ggplot(mtcars, aes(x = mpg, y = wt)p + geom_point()# 圖4.1 修改圖形屬性。用factor(cyl)修改顏色(左),用disp修改y坐標(右)。p + geom_point(aes(colour = factor(cyl)p + geom_point(aes(y = disp)p - ggplot(mtcars, aes(mpg, wt)p + geom_point(colour = darkblue)# 注意這里將顏色映射到darkblue與上面將顏色設定給darkblue的區(qū)別p + geom_point(aes(colour = darkblue)# The difference between (left) setting colour to codedarkblue and# (right) mapping colour to codedarkblue. When codedarkblue# is mapped to colour, it is treated as a regular value and scaled with# the default colour scale. This results in pinkish points and a legend.# 圖4.2# 將顏色設定為darkblue(左)與將顏色映射到darkblue(右)的區(qū)別。當顏色映# 射到darkblue時,darkblue將被看作一個普通的字符串,使用默認的顏色標# 度進行標度轉(zhuǎn)換,結(jié)果得到了粉紅色的點和圖例。qplot(mpg, wt, data = mtcars, colour = I(darkblue)qplot(mpg, wt, data = mtcars, colour = darkblue)# 圖4.3 正確分組時(分組變量group =# Subject)每個個體的折線圖(左)。錯誤的分組時連# 接所有觀測點的折線圖(右)。此處省略了分組圖形屬性,效果等同于group =# 1。data(Oxboys, package = nlme)# 左圖的代碼p - ggplot(Oxboys, aes(age, height, group = Subject) + geom_line()# 或qplot(age, height, data = Oxboys, group = Subject, geom = line)# 右圖的代碼qplot(age, height, data = Oxboys, geom = line)# 圖4.4# 給Oxboys數(shù)據(jù)添加光滑曲線。左圖用了和折線圖同樣的分組變量,得到了每個男# 孩的擬合直線。右圖在平滑層里用了aes(group = 1),得到了所有男孩的擬合直# 線。 左圖p + geom_smooth(aes(group = Subject), method = lm, se = F)# 或qplot(age, height, data = Oxboys, group = Subject, geom = line) + geom_smooth(method = lm, se = F)# 右圖p + geom_smooth(aes(group = 1), method = lm, size = 2, se = F)# 或qplot(age, height, data = Oxboys, group = Subject, geom = line) + geom_smooth(aes(group = 1), method = lm, size = 2, se = F)# 圖4.5# 如果想用箱線圖來查看每個時期的身高分布,默認的分組是正確的(左圖)。如果# 想用textttgeom_line()添加每個男孩的軌跡,就需要在新圖層里設定# aes(group = Subject)(右圖)。 左圖qplot(Occasion, height, data = Oxboys, geom = boxplot)# 右圖qplot(Occasion, height, data = Oxboys, geom = boxplot) + geom_line(aes(group = Subject), colour = #3366FF)# 或boysbox - ggplot(Oxboys, aes(Occasion, height) + geom_boxplot()boysbox + geom_line(aes(group = Subject), colour = #3366FF)# 圖4.6# 對于線條和路徑,線段的圖形屬性是由起始點的圖形屬性決定的。如果顏色是離# 散的(左圖),在相鄰的顏色間插入其他顏色是沒有任何意義的。如果顏色是連續(xù)# 的(右圖),可以在相鄰的顏色間進行插補,但默認條件下R不會這樣做。df - data.frame(x = 1:3, y = 1:3, colour = c(1, 3, 5)qplot(x, y, data = df, colour = factor(colour), size = I(5) + geom_line(aes(group = 1), size = 2)qplot(x, y, data = df, colour = colour, size = I(5) + geom_line(size = 2)# 用線性插值法做顏色漸變線條xgrid - with(df, seq(min(x), max(x), length = 50)interp - data.frame(x = xgrid, y = approx(df$x, df$y, xout = xgrid)$y, colour = approx(df$x, df$colour, xout = xgrid)$y)qplot(x, y, data = df, colour = colour, size = I(5) + geom_line(data = interp, size = 2)# 圖4.7 一個條形圖(左)按組分解后得到的疊加條形圖(右),兩者輪廓相同。qplot(color, data = diamonds)qplot(color, data = diamonds, fill = cut)# 例:生成變量ggplot(diamonds, aes(carat) + geom_histogram(aes(y = .density.), binwidth = 0.1)# 或qplot(carat, .density., data = diamonds, geom = histogram, binwidth = 0.1)# 圖4.8 應用于條形圖的三種位置調(diào)整。從左到右依次是:堆疊(stacking),填充# (filling)和并列(dodging)dplot - ggplot(diamonds, aes(clarity, fill = cut)dplot + geom_bar(position = stack)dplot + geom_bar(position = fill)dplot + geom_bar(position = dodge)# 圖4.9 同一調(diào)整(identity# adjustment)不適用于條形圖(左),因為后畫的條形會擋住先# 畫的條形。但它適用于線型圖(右),因為線條不存在相互遮掩的問題。dplot + geom_bar(position = identity)qplot(clarity, data = diamonds, geom = line, colour = cut, stat = bin, group = cut)# 圖4.10 直方圖的三種變體。頻率多邊形(frequency# polygon)(左);散點圖,點的大小和# 高度都映射給了頻率(中);熱圖(heatmap)用顏色來表示頻率。d - ggplot(diamonds, aes(carat) + xlim(0, 3)d + stat_bin(aes(ymax = .count.), binwidth = 0.1, geom = area)d + stat_bin(aes(size = .density.), binwidth = 0.1, geom = point, position = identity)d + stat_bin(aes(y = 1, fill = .count.), binwidth = 0.1, geom = tile, position = identity)# 例:nlme包的Oxboys數(shù)據(jù)集require(nlme, quiet = TRUE, warn.conflicts = FALSE)model - lme(height age, data = Oxboys, random = 1 + age | Subject)oplot - ggplot(Oxboys, aes(age, height, group = Subject) + geom_line()age_grid - seq(-1, 1, length = 10)subjects - unique(Oxboys$Subject)preds - expand.grid(age = age_grid, Subject = subjects)preds$height - predict(model, preds)oplot + geom_line(data = preds, colour = #3366FF, size = 0.4)Oxboys$fitted - predict(model)Oxboys$resid - with(Oxboys, fitted - height)oplot %+% Oxboys + aes(y = resid) + geom_smooth(aes(group = 1)model2 - update(model, height age + I(age2)Oxboys$fitted2 - predict(model2)Oxboys$resid2 - with(Oxboys, fitted2 - height)oplot %+% Oxboys + aes(y = resid2) + geom_smooth(aes(group = 1) 第五章代碼library(effects)library(ggplot2)# 圖5.1# 使用不同的基本幾何對象繪制相同數(shù)據(jù)的效果。從左上到右下的圖形名稱分別為:散# 點圖、條形圖、線圖、面積圖、路徑圖、含標簽的散點圖、色深圖/水平圖和多邊形圖。注意# 觀察條形圖、面積圖和瓦片圖的坐標軸范圍:這三種幾何對象占據(jù)了數(shù)據(jù)本身范圍以外的空# 間,于是坐標軸被自動拉伸了。df - data.frame(x = c(3, 1, 5),y = c(2, 4, 6),label = c(a,b,c)p - ggplot(df, aes(x, y) + xlab(NULL) + ylab(NULL)p + geom_point() + labs(title = geom_point)p + geom_bar(stat=identity) +labs(title = geom_bar(stat=identity)p + geom_line() + labs(title = geom_line)p + geom_area() + labs(title = geom_area)p + geom_path() + labs(title = geom_path)p + geom_text(aes(label = label) + labs(title = geom_text)p + geom_tile() + labs(title = geom_tile)p + geom_polygon() + labs(title = geom_polygon)# 圖5.2# 永遠不要指望依靠默認的參數(shù)就能對某個具體的分布獲得一個表現(xiàn)力強的圖形# (左圖)。(右圖) 對 x 軸進行了放大,xlim = c(55,# 70),并選取了一個更小的組距寬度, binwidth =# 0.1,較左圖揭示出了更多細節(jié)。我們可以發(fā)現(xiàn)這個分布是輕度右偏的。同時別# 忘了在標題中寫上重要參數(shù) (如組距寬度) 的信息。qplot(depth, data = diamonds, geom = histogram)qplot(depth, data = diamonds, geom = histogram, xlim = c(55, 70), binwidth = 0.1)# 圖5.3# 鉆石數(shù)據(jù)切割和深度分布的三種視圖。從上至下分別是分面直方圖、條件密度圖和頻# 率多邊形圖。它們都顯示了出一個有趣的模式:隨著鉆石質(zhì)量的提高,分布逐漸向左偏移且# 愈發(fā)對稱。depth_dist - ggplot(diamonds, aes(depth) + xlim(58, 68)depth_dist + geom_histogram(aes(y = .density.), binwidth = 0.1) + facet_grid(cut .)depth_dist + geom_histogram(aes(fill = cut), binwidth = 0.1, position = fill)depth_dist + geom_freqpoly(aes(y = .density., colour = cut), binwidth = 0.1)# 圖5.4 箱線圖可以用于觀察針對一個類別型變量 (如 cut) 取條件時# (左圖),或連續(xù)型變量 (如 carat) 取條件時# (右圖),連續(xù)型變量的分布情況。對于連續(xù)型變量,必須設置 group 圖# 形屬性以得到多個箱線圖。此處使用了group = round_any(carat, 0.1, floor)# 來獲得針 對變量carat 以 0.1 個單位為大小分箱后的箱線圖。library(plyr)qplot(cut, depth, data = diamonds, geom = boxplot)qplot(carat, depth, data = diamonds, geom = boxplot, group = round_any(carat, 0.1, floor), xlim = c(0, 3)# 圖5.5 幾何對象 jitter# 可在二維分布中有一個離散型變量時繪制出一個較為粗略的圖# 形??傮w來說,這種數(shù)據(jù)打散的處理對小數(shù)據(jù)集更有效。上圖展示了 mpg# 數(shù)據(jù)集中離散型變 量 class 和連續(xù)型變量 city,下圖則將連續(xù)型變量 city# 替換為離散型變量 drv。qplot(class, cty, data = mpg, geom = jitter)qplot(class, drv, data = mpg, geom = jitter)# 圖5.6# 密度圖實際上就是直方圖的平滑化版本。它的理論性質(zhì)比較理想,但難以由圖回溯到# 數(shù)據(jù)本身。左圖為變量 depth 的密度圖。右圖為按照變量 cut# 的不同取值上色的版本。qplot(depth, data = diamonds, geom = density, xlim = c(54, 70)qplot(depth, data = diamonds, geom = density, xlim = c(54, 70), fill = cut, alpha = I(0.2)# 圖5.7# 修改使用的符號可以幫助我們處理輕微到中等程度的過度繪制問題。從左至右分別# 為:默認的 shape、shape = 1(中空的點),以及 shape= .(像素大小的點)。df - data.frame(x = rnorm(2000), y = rnorm(2000)norm - ggplot(df, aes(x, y)norm + geom_point()norm + geom_point(shape = 1)norm + geom_point(shape = .) # 點的大小為像素級# 圖5.8 以從一個二元正態(tài)數(shù)據(jù)中抽樣所得的數(shù)據(jù)為例,使用 alpha# 混合來減輕過度繪制問題。alpha 值從左至右分別為:1/3, 1/5, 1/10。norm + geom_point(colour = black, alpha = 1/3)norm + geom_point(colour = black, alpha = 1/5)norm + geom_point(colour = black, alpha = 1/10)# 圖5.9 diamond 數(shù)據(jù)中的變量table 和變量 depth# 組成的圖形,展示了如何使用數(shù)據(jù)打 散和 alpha# 混合來減輕離散型數(shù)據(jù)中的過度繪制問題。從左至右為:不加任何處理的點,使用# 默認擾動參數(shù)打散后的點,橫向擾動參數(shù)為 0:5 (橫軸單位距離的一半)# 時打散后的點,alpha 值 1/10,alpha 值 1/50,alpha 值 1/200。td - ggplot(diamonds, aes(table, depth) + xlim(50, 70) + ylim(50, 70)td + geom_point()td + geom_jitter()jit - position_jitter(width = 0.5)td + geom_jitter(position = jit)td + geom_jitter(position = jit, colour = black, alpha = 1/10)td + geom_jitter(position = jit, colour = black, alpha = 1/50)td + geom_jitter(position = jit, colour = black, alpha = 1/200)# 圖5.10# 第一行使用正方形顯示分箱,第二行使用六邊形顯示。左欄使用默認分箱參數(shù),中# 欄使用參數(shù)bins = 10,右欄使用參數(shù)binwidth = c(0.02,# 200)。為了節(jié)約空間,均略去 了圖例。d - ggplot(diamonds, aes(carat, price) + xlim(1, 3) + theme(legend.position = none)d + stat_bin2d()d + stat_bin2d(bins = 10)d + stat_bin2d(binwidth = c(0.02, 200)d + stat_binhex()d + stat_binhex(bins = 10)d + stat_binhex(binwidth = c(0.02, 200)# 圖5.11# 使用密度估計對點的密度建模并進行可視化。上圖為基于點和等值線的密度展示,# 下圖為基于色深的密度展示。d - ggplot(diamonds, aes(carat, price) + xlim(1, 3) + theme(legend.position = none)d + geom_point() + geom_density2d()d + stat_density2d(geom = point, aes(size = .density.), contour = F) + scale_size_area()d + stat_density2d(geom = tile, aes(fill = .density.), contour = F)last_plot() + scale_fill_gradient(limits = c(1e-05, 8e-04)# 圖5.12 函數(shù) borders() 的使用實例。左圖展示了美國 (2006 年 1 月)# 五十萬人口以上的城 市,右圖為德克薩斯州的城市區(qū)劃。library(maps)data(us.cities)big_cities 5e+05)qplot(long, lat, data = big_cities) + borders(state, size = 0.5)tx_cities - subset(us.cities, country.etc = TX)ggplot(tx_cities, aes(long, lat) + borders(county, texas, colour = grey70) + geom_point(colour = black, alpha = 0.5)# 圖5.13# 左側(cè)的等值域圖展示了各州人身傷害案件的數(shù)量,右側(cè)的等值域圖展示了人身傷害# 和謀殺類案件的比率。library(maps)states - map_data(state)arrests - USArrestsnames(arrests) - tolower(names(arrests)arrests$region - tolower(rownames(USArrests)choro - merge(states, arrests, by = region)# 由于繪制多邊形時涉及順序問題 且merge破壞了原始排序 故將行重新排序choro - choroorder(choro$order), qplot(long, lat, data = choro, group = group, fill = assault, geom = polygon)qplot(long, lat, data = choro, group = group, fill = assault/murder, geom = polygon)# 章節(jié)5.7library(plyr) # ddply()在新版本中已被剝離并整合到plyr包中,這里先載入該包ia - map_data(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論