99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀R語言中qplot()的用法
R語言中qplot()的用法
2018-01-15
收藏

R語言中qplot()的用法

a, b, c, d, e, f, g, h = range(8)
ggplot2()函數(shù)
ggplot2是一個(gè)強(qiáng)大的作圖工具,它可以讓你不受現(xiàn)有圖形類型的限制,創(chuàng)造出任何有助于解決你所遇到問題的圖形。
qplot()
qplot()屬于ggplot2(),可以理解成是它的簡化版本。
qplot 即“快速作圖”(quick plot),顧名思義,能快速對(duì)數(shù)據(jù)進(jìn)行可視化分析。它的用法和R base包的plot函數(shù)很相似。
qplot()
參數(shù)
qplot(x, y = NULL, ..., data, facets = NULL,
      margins = FALSE, geom = "auto", stat = list(NULL),
      position = list(NULL), xlim = c(NA, NA),
      ylim = c(NA, NA), log = "", main = NULL,
      xlab = deparse(substitute(x)),
      ylab = deparse(substitute(y)), asp = NA)
各項(xiàng)參數(shù)詳解
1.x, y:變量名
2.data: 為數(shù)據(jù)框(data.frame)類型;如果有這個(gè)參數(shù),那么x,y的名稱必需對(duì)應(yīng)數(shù)據(jù)框中某列變量的名稱
3.facets: 圖形/數(shù)據(jù)的分面。這是ggplot2作圖比較特殊的一個(gè)概念,它把數(shù)據(jù)按某種規(guī)則進(jìn)行分類,每一類數(shù)據(jù)做一個(gè)圖形,所以最終效果就是一頁多圖
4.margins: 是否顯示邊界
5.geom: 圖形的幾何類型(geometry),這又是ggplot2的作圖概念。ggplot2用幾何類型表示圖形類別,比如point表示散點(diǎn)圖、line表示曲線圖、bar表示柱形圖等。
6.stat: 統(tǒng)計(jì)類型(statistics),這個(gè)更加特殊。直接將數(shù)據(jù)統(tǒng)計(jì)和圖形結(jié)合,這是ggplot2強(qiáng)大和受歡迎的原因之一。
7.position: 圖形或者數(shù)據(jù)的位置調(diào)整,這不算太特殊,但對(duì)于圖形但外觀很重要
8.xlim, ylim, 設(shè)置軸的上下限
9.xlab, ylab, 在x,y軸上增加標(biāo)簽
10.asp: 圖形縱橫比
qplot做散點(diǎn)圖
使用向量數(shù)據(jù)
plot函數(shù)一樣,如果不指定圖形的類型,qplot默認(rèn)做出散點(diǎn)圖。對(duì)于給定的x和y向量做散點(diǎn)圖,qplot用法也和plot函數(shù)差不多
> library(ggplot2)
> x <- 1:1000
> y <- rnorm(1000)
> plot(x, y, main="Scatter plot by plot()")
> qplot(x,y, main="Scatter plot by qplot()")


使用數(shù)據(jù)框數(shù)據(jù)
雖然可以直接使用向量數(shù)據(jù),但ggplot2更傾向于使用數(shù)據(jù)框類型的數(shù)據(jù)作圖。使用數(shù)據(jù)框有幾個(gè)好處:數(shù)據(jù)框可以用來存儲(chǔ)數(shù)值、字符串、因子等不同類型等數(shù)據(jù);把數(shù)據(jù)放在同一個(gè)R數(shù)據(jù)框?qū)ο笾锌梢员苊馐褂眠^程中數(shù)據(jù)關(guān)系的混亂;數(shù)據(jù)外觀的整理和轉(zhuǎn)換方便。ggplot2中使用數(shù)據(jù)框作圖的最直接的一個(gè)效果就是:你可以直接用數(shù)據(jù)的分類特性(數(shù)據(jù)框中的列變量)來決定圖形元素的外觀,這個(gè)過程在ggplot2中稱為映射(mapping),是自動(dòng)的。
在演示使用數(shù)據(jù)框作圖的好處之前我們先了解以下ggplot2提供的一組有關(guān)鉆石的示范數(shù)據(jù) diamonds:
> str(diamonds)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   53940 obs. of  10 variables:
 $ carat  : num  0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ...
 $ cut    : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ...
 $ color  : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ...
 $ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ...
 $ depth  : num  61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ...
 $ table  : num  55 61 65 58 58 57 57 55 61 61 ...
 $ price  : int  326 326 327 334 335 336 336 337 337 338 ...
 $ x      : num  3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ...
 $ y      : num  3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ...
 $ z      : num  2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...
可以看到這是數(shù)據(jù)框(data.frame)類型,有10個(gè)變量(列),每個(gè)變量有53940個(gè)測(cè)量值(行)。第一列為鉆石的克拉數(shù)(carat),為數(shù)字型數(shù)據(jù);第二列為鉆石的切工好壞(cut),為因子類型數(shù)據(jù),有5個(gè)水平;第三列為鉆石顏色(color),為7水平的因子;后面還有其他數(shù)據(jù)。由于數(shù)據(jù)太多,我們只取前7列的100個(gè)隨機(jī)觀測(cè)值。數(shù)據(jù)基本就是我們平時(shí)記錄原始數(shù)據(jù)的樣式:
> set.seed(1000) # 設(shè)置隨機(jī)種子,使隨機(jī)取樣具有可重復(fù)性
> datax<- diamonds[ seq(1,7)]
> head(datax, 4)
##       carat   cut color clarity depth table price
## 17686  1.23 Ideal     H     VS2  62.2    55  7130
## 40932  0.30 Ideal     E     SI1  61.7    58   499
## 6146   0.90  Good     H     VS2  61.9    58  3989
## 37258  0.31 Ideal     G    VVS1  62.8    57   977
如果要做鉆石克拉和價(jià)格關(guān)系的曲線圖,用plot和qplot函數(shù)都差不多:
plot(x=datax$carat, y=datax$price, xlab="Carat", ylab="Price", main="plot function")
qplot(x=carat, y=price, data=datax, xlab="Carat", ylab="Price", main="qplot function")



但如果要按切工進(jìn)行分類作圖,plot函數(shù)的處理就復(fù)雜了,你首先得將數(shù)據(jù)進(jìn)行分類提取,然后再一個(gè)個(gè)作圖。雖然可以用循環(huán)完成,但作圖后圖標(biāo)的添加還得非常小心,你得自己保證數(shù)據(jù)和圖形外觀之間的對(duì)應(yīng)關(guān)系:

plot(x=datax$carat, y=datax$price, xlab="Carat", ylab="Price", main="plot function", type='n')
cut.levels <- levels(datax$cut)
cut.n <- length(cut.levels)
for(i in seq(1,cut.n)){
  subdatax <- datax[datax$cut==cut.levels[i], ]
  points(x=subdatax$carat, y=subdatax$price, col=i, pch=i)
}
legend("topleft", legend=cut.levels, col=seq(1,cut.n), pch=seq(1,cut.n), box.col="transparent", cex=0.8)

但用ggplot2作圖你需要考慮數(shù)據(jù)分類和圖形元素方面的問題就很少,你只要告訴它用做分類的數(shù)據(jù)就可以了:
qplot(x=carat, y=price, data=datax, color=cut, shape=cut, main="qplot function")

qplot做曲線圖
和plot函數(shù)一樣,qplot也可以通過設(shè)置合適的參數(shù)產(chǎn)生曲線圖,這個(gè)參數(shù)就是geom(幾何類型)。圖形的組合非常直接,組合表示幾何類型的向量即可:
qplot(x=carat, y=price, data=datax, color=cut, geom="line", main="geom=\"line\"")
qplot(x=carat, y=price, data=datax, color=cut, geom=c("line", "point"), main="geom=c(\"line\", \"point\")")

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }