ggplot2
現在學習不再是先看書,而是先用起來,即從實例入手來學習,遇到不懂再去翻書或藉助其他手段。這篇文字我會從例子入手,致力於把每一個難懂的函數用例子解釋給自己。為什麼不先看書或資料,而是先從實例上手呢?因為我就發現啊,有些函數的用途即使人家給我用文字解釋了,我還是蒙圈。比如:jitter()這個函數,R給的解釋是:Add a small amount of noise to a numeric vector.每個單詞我都認識,可對不起R,我還是不懂!所以呢,我就進入例子去理解。例子如下:
rep(0,7)n[1] 0 0 0 0 0 0 0n> jitter(rep(0,7))n[1] 0.015774358 -0.004772041 0.017789333 0.005229265 0.008213961 -0.016115553 -0.018884070n> rep(10000,5)n[1] 10000 10000 10000 10000 10000n> jitter(rep(10000,5))n[1] 9965.141 9942.514 9893.793 9889.499 9974.294n> rep(1,3)n[1] 1 1 1n> jitter(rep(1,3))n[1] 0.9873112 0.9997499 0.9948069n
琢磨一下實例,jitter函數其實就是一個數值的近似值。
好,文章當中我會先用例子來解釋函數,再用ggplot來繪製圖,這樣有助於理解ggplot的語法規則。
一、創建數據集並用ggplot繪圖
#letters是英文字母,a字母排序1,b字母排序2,c字母排序3……[1:3]意思是字母排序1到3的字母。each=10意思每一個字母重複輸出10次,函數rep,即repeatna<-rep(letters[1:3],each=10)n> an [1] "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "b" "b" "b" "b" "b" "b" "b" "b" "b" "b" "c" "c"n[23] "c" "c" "c" "c" "c" "c" "c" "c"n
#rnorm函數可獲得正態分布數值,rnorm(30)獲得30個有效數字為9位的數值nrnorm(30)n [1] 1.03829162 -0.85022195 0.27133714 0.17391163 0.06414701 0.96619785 1.48380631n [8] -1.37744219 -0.36735187 -1.50955431 0.50013867 -0.34759058 -1.62538044 -0.09412440n[15] 0.20860409 -0.19574512 0.08940817 0.32516945 -0.03321329 -0.41864851 -0.15958371n[22] -1.42287182 -1.25427656 -0.66499348 -0.98552862 -0.71776099 0.11585889 0.66912913n[29] 1.02096457 0.79034638n> rnorm(30)n [1] -0.974192689 0.033015367 0.123732240 -0.137091304 -0.814544338 0.440082107n [7] 0.882621980 -0.551608429 -0.004116999 -0.853615740 -3.221827070 0.770860440n[13] 0.325192883 0.299609050 0.416332974 -0.344236467 -1.768057760 -0.168796977n[19] -0.907067761 -0.692311398 0.345076270 -0.167178226 0.029155106 -0.275322937n[25] 0.752652192 -0.670116134 0.449364801 1.485498559 0.968167496 0.863486801n
#生成數據框dfndf<-data.frame(gp=factor(rep(letters[1:3],each=10)),y=rnorm(30))nnames(df)n[1] "gp" "y" n
#生成數據框dsnds<-plyr::ddply(df,"gp",plyr::summarise,mean=mean(y),sd=sd(y))nnames(ds)n[1] "gp" "mean" "sd"n
ggplot(df,aes(gp,y))+n+ geom_point()+n+ geom_point(data = ds,aes(y=mean),colour=red,size=3)n
ggplot(df)+n+ geom_point(aes(gp,y))+n+ geom_point(data = ds,aes(gp,mean),colour=red,size=3)#aes從ggplot移至geom_point,則aes需對應起來。n
#圖數據來自數據框df,x軸gp列,y軸y列,添加y列元素均值mean到圖,再添加y列元素的波動範圍到圖。nggplot()+n+ geom_point(data = df,aes(gp,y))+n+ geom_point(data = ds,aes(gp,mean),colour=red,size=3)+n+ geom_errorbar(data = ds,aes(gp,mean,ymin=mean-sd,ymax=mean+sd),colour=red,width_=0.4)n
二、用mtcars的數據集,繼續來研究ggplot的使用方法
head(mpg)n# A tibble: 6 × 11n manufacturer model displ year cyl trans drv cty hwy fl classn <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>n1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compactn2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compactn3 audi a4 2.0 2008 4 manual(m6) f 20 31 p compactn4 audi a4 2.0 2008 4 auto(av) f 21 30 p compactn5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compactn6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compactn> names(mpg)n [1] "manufacturer" "model" "displ" "year" "cyl" "trans" n [7] "drv" "cty" "hwy" "fl" "class"n
#圖數據來自數據框mpg,X軸為列displ,Y軸為列hwy,圖形象為點形象。nggplot(mpg,aes(displ,hwy))+n+ geom_point()n
#異曲同工nggplot(mpg)+n+ geom_point(aes(displ,hwy))n
base<-ggplot(mpg,aes(displ,hwy))+n+ geom_point()n> class(base)n[1] "gg" "ggplot"n> base+geom_smooth()#添加平滑曲線到圖n
三、符號%+%的使用方法
base%+%subset(mpg,fl==p)n
四、list函數的使用
base<-ggplot(mpg,aes(displ,hwy))+n+ geom_point()n> class(base)n[1] "gg" "ggplot"n base+list(subset(mpg,fl==p),geom_smooth())n
五、ggplot圖的保存之ggsave函數的使用
ggplot(mtcars,aes(mpg,wt))+n+ geom_point()n> ggsave("mtcars.pdf")n#保存n> getwd()n[1] "C:/Users/acer/Documents"n
ggsave("mtcars.pdf",width = 4,height = 4)n> unlink("mtcars.pdf")#刪除n
qplot(mpg,wt,data = mtcars)n
六、qplot函數的應用
qplot(mpg,wt,data = mtcars,colour=cyl)#三個變數n names(mtcars)n [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb"n
qplot(mpg,wt,data = mtcars,size=cyl)n
qplot(mpg,wt,data = mtcars,facets = vs~am)#四個變數n
#正態分布nrnorm(10)n [1] -0.35704965 0.02660889 0.03238472 0.61877584 -0.98355292 0.85565786 -0.94475915n [8] 0.41714348 0.52949343 1.91490716n> rnorm(10)n [1] 1.4122814 -1.2331096 1.2202021 2.3083913 -0.5642851 1.2629438 -1.6097689 -0.7968245n [9] 1.1334308 -1.9392059n#均勻分布nrunif(10)n [1] 0.60219665 0.06826834 0.35725795 0.44583206 0.36228436 0.18225361 0.83247226 0.73497623n [9] 0.16244837 0.75137018n> runif(10)n [1] 0.93938189 0.09835945 0.72656509 0.73801112 0.35441418 0.26325779 0.47906843 0.99179176n [9] 0.05175832 0.13997369n
qplot(1:10,rnorm(10),colour=runif(10))n
> letters[1:10]n [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"n> qplot(1:10,letters[1:10])n
七、用qplot函數繪製殘差圖
#mpg因變數,wt自變數,n mod<-lm(mpg~wt,data = mtcars)n> modnnCall:nlm(formula = mpg ~ wt, data = mtcars)nnCoefficients:n(Intercept) wt n 37.285 -5.344 n#resid殘差值(實際值與預測值之差),fitted預測值,nqplot(resid(mod),fitted(mod))n
八、qplot函數繪製自定義函數圖
a<-1:10n> an [1] 1 2 3 4 5 6 7 8 9 10n> b<-a^2n> bn [1] 1 4 9 16 25 36 49 64 81 100nf<-function(){a<-1:10n+ b<-a^2n+ qplot(a,b)n+ }n> f()n
qplot(mpg,wt,data = mtcars,colour=I("red"))#函數I()從算術的角度解釋括弧中的元素n
qplot(mpg,wt,data = mtcars)n
qplot(mpg,data = mtcars)n
y<-mtcars$mpgn> yn [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4n[19] 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4n> seq_along(y)n#sequence序號n [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30n[31] 31 32n> length(y)#y里有多少個元素n[1] 32nqplot(y=mpg,data = mtcars)n
九、qplot函數繪製路徑圖
qplot(mpg,wt,data = mtcars,geom = "path")n
十、添加箱線圖
b<-mtcars$cyln> bn [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4nclass(mtcars$cyl)n[1] "numeric"n> qplot(factor(cyl),wt,data = mtcars,geom = c("boxplot","jitter"))n
十一、(qplot函數即quickplot函數)繪製dotplot圖
十二、添加horizontal水平線、vertical垂直線、diagonal斜線到圖對應函數geom_hline、geom_vline、geom_abline
mtcars$wtn [1] 2.620 2.875 2.320 3.215 3.440 3.460 3.570 3.190 3.150 3.440 3.440 4.070 3.730 3.780 5.250n[16] 5.424 5.345 2.200 1.615 1.835 2.465 3.520 3.435 3.840 3.845 1.935 2.140 1.513 3.170 2.770n[31] 3.570 2.780n> summary(mtcars$wt)n Min. 1st Qu. Median Mean 3rd Qu. Max. n 1.513 2.581 3.325 3.217 3.610 5.424 n> p<-ggplot(mtcars,aes(wt,mpg))+geom_point()n> p+geom_vline(xintercept = 5)#在X軸值即wt值為5的位置添加垂直線n
p+geom_vline(xintercept = 1:5)#在X軸值即wt值為1、2、3、4、5的位置添加垂直線n
<-mtcars$mpgn [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4n[19] 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0 21.4n> summary(mtcars$mpg)n Min. 1st Qu. Median Mean 3rd Qu. Max. n 10.40 15.42 19.20 20.09 22.80 33.90 n> p+geom_hline(yintercept = 20)#在Y軸值即mpg值為20的位置添加水平線n
p+geom_abline()#數據範圍之外n
p+geom_abline(intercept = 20)#斜線在Y軸的截距n
#coefficient函數列出模型的截距,截距:函數與坐標軸相交,交點坐標與原點坐標之差。(交點到X軸的距離或到Y軸的距離)斜率:直線或曲線某點切線相對於X軸的傾斜程度。ncoef(lm(mpg~wt,data = mtcars))n(Intercept) wt n 37.285126 -5.344472 n> p+geom_abline(intercept = 37,slope = -5)#Y軸截距,X軸截距n
十三、添加模型到圖
p<-ggplot(mtcars,aes(wt,mpg))+geom_point() np+geom_smooth(method = "lm",se=FALSE)#lm線性模型n
p<-ggplot(mtcars,aes(wt,mpg))+geom_point()n p+geom_smooth(method = "lm",se=TRUE)#se參數陰影形象n
table(mtcars$cyl)nn 4 6 8 n11 7 14 np<-ggplot(mtcars,aes(mpg,wt))+n+ geom_point()+n+ facet_wrap(~cyl)#按cyl汽缸數4、6、8分組繪圖n
m<-data.frame(cyl=c(4,6,8),wt=c(2.28,3.11,4.00))#創建數據框m,ggplot的數據取自數據框data.framen p+geom_hline(aes(yintercept=wt),m)#在Y軸值為2.28,3.11,4.00的位置添加水平線n
推薦閱讀:
TAG:学习方法 |