Presentation is loading. Please wait.

Presentation is loading. Please wait.

R的版面配置與繪圖功能 R的版面配置與繪圖 R附加圖形應用與3D繪圖 ggplot2套組的繪圖功能 4 2 3 1 程式迴圈繪圖.

Similar presentations


Presentation on theme: "R的版面配置與繪圖功能 R的版面配置與繪圖 R附加圖形應用與3D繪圖 ggplot2套組的繪圖功能 4 2 3 1 程式迴圈繪圖."— Presentation transcript:

1 R的版面配置與繪圖功能 R的版面配置與繪圖 R附加圖形應用與3D繪圖 ggplot2套組的繪圖功能 4 2 3 1 程式迴圈繪圖

2 R的版面配置與繪圖功能 圖形的版面配置、圖形設定參數、散佈圖、函數曲線、矩陣圖、條件散佈圖、常態機率圖、直方圖、長條圖、盒狀圖、圓餅圖、3D繪圖、附加圖形應用、進階3D繪圖、程式迴圈繪圖 、ggplot2

3 圖形區域的版面配置 R最具特色的功能之一就是繪圖功能,一般是一張紙畫一個圖形,但R可以將多張圖形放在同一張紙,但事先最好先儲存一張圖的原始設定,如oldpar=par(),之後再以指令par(oldpar)恢復原設定一張紙一個圖,方式包括以下三種: par函數搭配mfrow或參數mfcol參數: 適合規則形狀的多張圖形分布 layout函數:適合不規則的多張圖形分布 par函數搭配fig參數: 適合不規則的多張圖形分布

4 par()函數搭配mfrow參數 適合規則形狀的多張圖形分布,例如
par(mai=c(0.5,0.5,0.5,0.5),mfrow=c(3,2)) mai設定紙張邊緣(英吋) 依序是底部、左邊、頂端、右邊,mfcol是逐欄,mfrow是逐列排列 將babies.txt依年齡分為6組(~20、20-25、25-30、30-35、35-40、40~)畫6個height和weight的散佈圖

5 多張規則圖形的程式碼 babies=read.table(“d:/R/babies.txt”,header=T)
grp1=subset(babies,age<20) grp2=subset(babies,age>=20 & age<25) grp3=subset(babies,age>=25 & age<30) grp4=subset(babies,age>=30 & age<35) grp5=subset(babies,age>=35 & age<40) grp6=subset(babies,age>=40) #oldpar=par() par(mai=c(0.5,0.5,0.5,0.5),mfrow=c(3,2)) #by rows plot(grp1[,"height"],grp1[,"weight"]) #plot(x,y) x y are vectors plot(grp2[,"height"],grp2[,"weight"]) plot(grp3[,"height"],grp3[,"weight"]) plot(grp4[,"height"],grp4[,"weight"]) plot(grp5[,5],grp5[,6]) plot(grp6$height,grp6$weight) #par(oldpar)

6 6個規則圖形的散佈圖

7 layout函數:不規則的多個圖形 1 2 3 2 1 3 layout(M,widths,heights)
M是圖形分佈的矩陣,widths、heights各是設定M矩陣長、寬的比例,其基準點是左下角 上圖的指令為2x2矩陣layout(matrix(c(1,1,2,3),2,2, byrow=T)) 下圖的指令為2x2矩陣 layout(matrix(c(2,0,1,3),2,2, byrow=T), widths=c(3,1), heights=c(1,3)) 1 2 3 2 1 3

8 layout函數的程式碼 attach(iris)
layout(matrix(c(2,0,1,3),2,2, byrow=T), widths=c(2,1), heights=c(1,2)) plot(Sepal.Length,Sepal.Width,main="Sepal Length-Width Scatter Graph") hist(Sepal.Length,main="Sepal Length Histogram Graph") hist(Sepal.Width,main="Sepal Width Histogram Graph")

9 layout函數的多個圖形

10 par函數搭配fig參數 par(fig=c(x1,x2,y1,y2))
par(fig=c(0,0.8,0.7,1)) 大約是圖2的版面位置, 左下角座標(x1,y1)是(0,0.7) ,右上角座標(x2,y2)則是(0.8,1) (0,1) (1,1) 2 1 3 (1,0) (0,0)

11 par函數搭配fig參數程式碼 attach(iris) par(fig=c(0,0.6,0,0.6),new=TRUE)
plot(Sepal.Length,Sepal.Width,main="Sepal Length-Width Scatter Graph") par(fig=c(0,0.6,0.6,1),new=TRUE) hist(Sepal.Length,main="Sepal Length Histogram Graph") par(fig=c(0.6,1,0,0.6),new=TRUE) hist(Sepal.Width,main="Sepal Width Histogram Graph")

12 par函數搭配fig參數多個圖形

13 圖形基本設定參數 col=k : 設定color,可用在col.axis、col.lab、col.main、col.sub,設定座標軸、X與Y軸、主標題、附標題的顏色,可以colors()查到顏色名稱 lty=k : line type , k=1為實線 lwd=k : line width , k為標準線寬的倍數 pch=k或”圖點符號”,預設為圓點, k=0~20 font=k :設定字體,1一般字體、2粗體、3斜體、4粗斜體,可用在font.axis、font.lab、font.main、font.sub cex=k : 設定字型的倍數(可為小數),可用在cex.axis、cex.lab、cex.main、cex.sub

14 plot散佈圖、長條圖、盒狀圖 >plot(Species) >plot(iris)或 pairs(iris)
>plot(Sepal.Length,Sepal.Width) >plot(Species,Sepal.Length) >plot(iris)或 pairs(iris) >plot(~Sepal.Length+ Petal.Length+Petal.Width) >plot(Sepal.Length)

15 curve 函數曲線 curve(x^2-4*x+3,-4,4,lty=2,add=T) curve(sin(x),-4,4)

16 pair矩陣圖 >pair(iris)

17 coplot條件散佈圖(加 ,rows=1) > coplot(Sepal.Length~Petal.Length | Species)

18 常態機率圖qqnorm qqline、qqplot
qqplot(Sepal.Length,Sepal.Width) qqnorm(Sepal.Length)+ qqline(Sepal.Length)最佳線

19 直方圖hist hist(Sepal.Length,breaks=4:8) hist(Sepal.Length,nclass=8)

20 bar plot長條圖 cancers=c(11,16,17,6,12)
labels=c("乳癌","支氣管癌","結腸癌","卵巢癌","胃癌") barplot(cancers) barplot(cancers,names=labels) barplot(cancers,names=labels,horiz=T) barplot(cancers,names=labels,col=c(1,2,3,4,5)) barplot(cancers,names=labels,col=c(1,2,3,4,5), density=10) barplot(cancers,names=labels,col=c(1,2,3,4,5), density=40)

21 barplot長條圖

22 boxplot盒狀圖 boxplot(iris[,1],xlab="SLen",main="(F1)")
boxplot(iris[,1:4],main="(F2)") boxplot(iris[,1:4],main="(F3)",names=c("Slen","Swid","Plen","Pwid")) boxplot(iris[,1:4],main="(F4)",horizontal=T) boxplot(iris[,4]~iris[,5],main="(F5)",xlab="flower class",ylab="Slen") boxplot(Sepal.Length~Species,data=iris,main="(F6)",xlab="flower class",ylab="Slen“ ,col=c(2,3,4))

23 boxplot盒狀圖

24 Pie圓餅圖 sales=c(0.12,0.3,0.26,0.16,0.04, 0.12) snames=c("電腦","廚具","家電","傢俱","其他","服飾") pie(sales, label= snames)

25 3D繪圖contour 、 image 、persp
contour(x,y,z):畫出地圖效果的等高線圖 image(x,y,z):類似於contour,但可畫出色彩 persp(x,y,z,theta,phi,box=TRUE):畫出真正的三度空間透視圖,theta控制圖形上下旋轉角度,phi控制圖形左右旋轉角度,box=TRUE則不畫出框線 >demo(graphics) #展示繪圖功能 等高線圖>filled.contour(volcano,color=terrain.colors,plot.axes=contour(volcano,add=TRUE))

26 3D繪圖contour 、 image 、persp
x=seq(-3,3,0.1) y=x f=function(x,y){(1/(2*pi))*exp(-0.5*(x^2+y^2))} z=outer(x,y,f) #外積函數outer par(mfcol=c(2,2)) contour(x,y,z) image(x,y,z) persp(x,y,z) persp(x,y,z,theta=30,phi=30,box=F, main= "persp theta=30 phi=30")

27 3D繪圖contour 、 image 、persp

28 繪圖函數的共用輔助參數 main=“” 、 sub=“” 、xlab=“” 、ylab=“”
xlim=c(xmin,xmax) 、 ylim=c(0,30) add=TRUE #覆蓋前一張圖 axes=FALSE #不畫出座標軸 xaxt=“n” yaxt=“n” #不畫出座標軸格線 right=FALSE #即右邊為開放區間 < log=“x”、 log=“y”、 log=“xy” type=“p”:points only 、type=“l”:lines only type=“b”:points and lines、 type=“o”:overlap type=“s”:steps 、type=“h”:height

29 plot圖點樣式 x=rnorm(10,0,1) >plot(x,type=“p”) >plot(x,type=“l”)
>plot(x,type=“b”) >plot(x,type=“o”) >plot(x,type=“h”) >plot(x,type=“s”)

30 附加圖形:points、lines、text
height=sample(150:190, 30,replace=TRUE) score=sample(c(60:100), 30,replace=TRUE) xp=c(160,165,170,175) yp=c(80,90,80,90) plot(height,score) points(xp,yp,col=2, pch=19) lines(xp,yp,col=3) text(xp,yp+5,col=4, label=c("P1","P2","P3","P4"))

31 附加圖形:legend、title、axis
age=sample(20:60,100, replace=TRUE) sex=sample(c("M","F"),100,replace=TRUE) race=sample(c("WHITE","YELLOW","BLACK"),100,replace=TRUE) data=data.frame(age,sex,race) barplot(table(race),col= 4:6) table(race)

32 附加圖形:legend、title、axis
table(sex,race) F M barplot(table(sex,race), col =c(“pink”,“blue”), axes= FALSE, beside=TRUE) legend(0.5,40,c("Female","Male"),col=c("pink","blue"),pch=15) title(main="Race and Sex Bar Chart",sub="in1988") axis(2,las=2) 1底部 2左邊 las=2水平數字

33 也可在既有圖形加入add=T boxplot(age~race, main="Age by Race", col="yellow",boxwex=0.3) boxplot(age[sex== "M"]~race[sex=="M"],col="blue",boxwex=0.1,at= c(1.3,2.3,3.3), add=TRUE, axes=F)

34 自訂座標軸及互動式圖形 attach(iris)
plot(Sepal.Length,Petal.Length,xaxt="n",yaxt="n",xlim=c(4,8)) axis(side=1,at=seq(4,8,by=0.5)) axis(side=2,las=2) abline(v=7,col="red") identify(Sepal.Length,Petal.Length) 按右鍵的停止功能結束

35 附加圖形應用:points、 legend
plot(Sepal.Length[Species=="setosa"],Petal.Length[Species=="setosa"], pch=1 ,col="black", xlim=c(4,8), ylim= c(0,8), main="classified scatter plot", xlab= "SLen", ylab= "PLen") points(Sepal.Length[Species=="virginica"],Petal.Length[Species=="virginica"],pch=3,col="green") points(Sepal.Length[Species=="versicolor"],Petal.Length[Species=="versicolor"],pch=2,col="red") legend(4,8,legend=c("setosa","versicolor","virginica"),col=c(1,2,3),pch=c(1,2,3))

36 附加圖形應用iris

37 Taiwan map

38 Taiwan map library(maps) x=world.cities
taiwan=x[x$country.etc=="Taiwan", ] taiwan map("world",xlim=c(120,122),ylim=c(21.2,25.5),mar=c(1,1,1,1)) taiwan.city=taiwan[taiwan$name %in% c("Taipei", "Taoyuan","Taichung","Tainan","Kaohsiung"), ] map.cities(taiwan.city,capital=1) map.cities(taiwan.city,label=TRUE) mpop=taiwan[taiwan$pop > ,] symbols(mpop$long,mpop$lat,circle=mpop$pop,inches=0.2,fg=2,lwd=2,add=TRUE)

39 Scatterplot3d之一 library("scatterplot3d") scatterplot3d(iris[,1:3])
scatterplot3d(iris[,1:3],angle=55, main="3D Scatter Plot", xlab = "Sepal Length (cm)", ylab = "Sepal Width (cm)", zlab = "Petal Length (cm)") scatterplot3d(iris[,1:3], pch = 16, color="steelblue")

40 Scatterplot3d之一

41 Scatterplot3d之二 #Change point shapes and colors by groups
shapes = c(16, 17, 18) shapes <- shapes[as.numeric(iris$Species)] scatterplot3d(iris[,1:3], pch = shapes) colors <- c("#999999", "#E69F00", "#56B4E9") colors <- colors[as.numeric(iris$Species)] scatterplot3d(iris[,1:3], pch = 16, color=colors)

42 Scatterplot3d之二

43 Scatterplot3d之三 #Remove box and add bars
scatterplot3d(iris[,1:3], pch = 16, color = colors, grid=TRUE, box=FALSE) scatterplot3d(iris[,1:3], pch = 16, type="h", color=colors)

44 Scatterplot3d之三

45 Scatterplot3d之四 # Custom shapes/colors legends or points label
s3d <- scatterplot3d(iris[,1:3], pch = shapes, color=colors) legend("bottom", legend = levels(iris$Species), col = c("#999999", "#E69F00", "#56B4E9"), pch = c(16, 17, 18), inset = -0.25, xpd = TRUE, horiz = TRUE, cex=0.5) # inset:distance between plot and legend # xpd:enable legend outside plot scatterplot3d(iris[,1:3], pch = 16, color=colors) text(s3d$xyz.convert(iris[, 1:3]), labels = rownames(iris), cex= 0.7, col = "steelblue")

46 Scatterplot3d之四

47 Scatterplot3d之五 oldpar=par() par(mfcol=c(1,2))
s3d=scatterplot3d(Nuts, Eggs, Milk, col.axis="blue", col.grid="lightblue", main="scatterplot3d - 1", pch=16,color=as.numeric(Country)) text(s3d$xyz.convert(data[,c(9,4,5)]), labels = Country, cex= 0.6, col = "steelblue") t3d=scatterplot3d(RedMeat, WhiteMeat, Fish,col.axis="blue", col.grid="lightblue", main="scatterplot3d - 2", pch=16,color=as.numeric(Country)) text(t3d$xyz.convert(data[,c(2, 3,6)]), labels = Country, cex= 0.6, col = "steelblue") par(oldpar)

48 Scatterplot3d之五

49 plot3D套組說明 scatter3D(x, y, z, ..., colvar = z, col = NULL, add = FALSE) text3D(x, y, z, labels, colvar = NULL, add = FALSE) points3D(x, y, z, ...) #scatter3D(…, type =“p”) lines3D(x, y, z, ...) #scatter3D(…, type =“l”) scatter2D(x, y, colvar = NULL, col = NULL, add = FALSE) text2D(x, y, labels, colvar = NULL, col = NULL, add = FALSE)

50 plot3D套組說明 x, y, z: vectors of point coordinates
colvar: a variable used for coloring by z ,unless colvar = NULL col: color palette used for coloring colvar variable labels: the text to be written add: logical. If TRUE, then the points will be added to the current plot. If FALSE a new plot is started …: additional persp arguments including xlim, ylim, zlim, xlab, ylab, zlab, main, sub, r, d, scale, expand, box, axes, nticks, tictype.

51 scatter3D之一 library(plot3D) attach(iris)
x=Sepal.Length;y=Petal.Length;z=Sepal.Width oldpar=par() par(mfcol=c(1,2)) #By default, the points are colored automatically using the variable Z scatter3D(x,y,z, clab = c("Sepal", "Width (cm)")) scatter3D(x,y,z, colvar = NULL, col = "blue", pch = 19, cex = 0.5) par(oldpar)

52 scatter3D之一

53 scatter3D之二 #title and axis labels
scatter3D(x, y, z, phi = 0, bty = "g", pch = 20, cex = 2, ticktype = "detailed", main = "Iris data", xlab = "Sepal.Length",ylab ="Petal.Length", zlab = "Sepal.Width") # Create a scatter plot and text scatter3D(x, y, z, phi = 0, bty = "g", pch = 20, cex = 0.5) text3D(x, y, z, labels = rownames(iris),add = TRUE, colkey = FALSE, cex = 0.5)

54 scatter3D之二

55 scatter3D之三 data=read.table("d:\\stella\\R\\protein.txt", header=T)
attach(data) scatter3D(Nuts,Fr.Veg,Fish,phi = 0, bty = "g",colvar = NULL, col = "red",pch = 16, cex = 0.5, main = "6 variables (red & blue)", xlab = "Nuts & Eggs",ylab ="Fr.Veg & WhiteMeat", zlab = "Fish & ReaMeat") text3D(Nuts,Fr.Veg,Fish,labels =rownames(data),add = TRUE, colkey = FALSE, cex = 0.5) scatter3D(Eggs,WhiteMeat,RedMeat,colvar = NULL, col = "blue",pch = 19, cex = 0.5,add=T) text3D(Eggs, WhiteMeat, RedMeat,labels = rownames(data),add=TRUE,colkey = FALSE, cex = 0.5)

56 scatter3D之三

57 scatter3D之四 # 4D and choose suitable ranges
with(USArrests, text3D(Murder, Assault, Rape, labels = rownames(USArrests), colvar = UrbanPop, col = gg.col(100), theta = 60, phi = 20, xlab = "Murder", ylab = "Assault", zlab = "Rape", main = "USA arrests", cex = 0.6, bty = "g", ticktype = "detailed", d = 2, clab = c("Urban","Pop"), adj = 0.5, font = 2)) with(USArrests, scatter3D(Murder, Assault, Rape - 1, colvar = UrbanPop, col = gg.col(100), type = "h", pch = ".", add = TRUE)) plotdev(xlim = c(0, 10), ylim = c(40, 150), zlim = c(7, 25))

58 scatter3D之四

59 Lattice套組 attach(quakes) library(lattice)
plot(xyplot(long~lat| cut(depth,2))) Magnitude=equal.count(mag,6) plot(cloud(depth~lat* long| Magnitude, panel.aspect=0.9)) plot(cloud(depth~lat* long| cut(mag,4),panel.aspect=0.9)) summary(Deep) plot(lat~long,pch='+') symbols(long,lat,circles=depth,inches=0.5,add=T)

60 Lattice兩格

61 Lattice六格

62 Lattice四格

63 Lattice四格

64 Bubble plot

65 多張圖可利用迴圈執行

66 一張紙15個圖形(但有兩張)

67 兩張紙30個圖形輸出到pdf檔 wdbc=read.csv(“d:\\stella\\R\\wdbc.txt”, header=T)
head(wdbc) pdf("d:\\stella\\R\\wdbc.pdf") par(mai=c(0.5,0.5,0.5,0.5),mfrow=c(3,5)) for (i in 3:32) { hist(wdbc[ ,i],main=paste("C",i-2)) } graphics.off()

68 迴圈繪圖(多個檔在同一目錄) # method 1:all files of one directory
(dir=list.files("d:/stella/R/baby",full.name=TRUE)) # infile=dir[grep(“.txt”,dir)] par(mai=c(0.5,0.5,0.5,0.5),mfrow=c(3,4)) lbl=c("~20","20~25","25~30","30~35","35~40","40~") for (i in 1:length(dir)) { file0=dir[i]; data=read.table(file=file0,header=T) hist(data$bwt,main=paste("babies weight hist age=",lbl[i])) plot(data$height,data$weight,main=paste("height and weight plot age=",lbl[i])) }

69 baby1.txt-baby6.txt對應先前用
迴圈繪圖(多個檔在同一目錄) baby1.txt-baby6.txt對應先前用 subset函數輸出的檔grp1-grp6

70 迴圈繪圖(利用循序的檔名) #method 2:seqential file names利用循序檔名
pdf("d:\\stella\\R\\baby\\babies.pdf") # ,family=“GB1” par(mai=c(0.5,0.5,0.5,0.5),mfrow=c(3,2)) lbl=c("~20","20~25","25~30","30~35","35~40","40~") for (i in 1:6) { fn=paste("d:\\stella\\R\\baby\\baby",i,".txt",sep=""); data=read.table(file=fn,header=T) hist(data$bwt,main=paste("babies weight hist age=",lbl[i])) plot(data$height,data$weight,main=paste("height and weight plot age=",lbl[i])) } graphics.off() # or dev.off()

71 迴圈繪圖的結果

72 練習:wdbc盒狀圖 pdf("d:\\stella\\output.pdf")
par(mai=c(0.5,0.5,0.5,0.5),mfcol=c(2,3)) for (i in 3:32) { boxplot(wdbc[,i]~wdbc[,2],main=paste("c",i-2,sep=""),col=c("yellowgreen","gray")) boxplot(wdbc[,i], main=paste("c",i-2,sep=""), col="yellow",boxwex=0.3) boxplot(wdbc[diagnosis==“M”,i],col=“blue”,boxwex= 0.1, at= c(1.3), add=TRUE, axes=F) boxplot(wdbc[diagnosis==“B”,i],col=“pink”,boxwex= 0.1, at= c(0.7), add=TRUE, axes=F) } graphics.off()

73

74 ggplot2套件 ggplot2是以圖層堆疊的概念建構圖形,包含以下幾層:
ggplot函數為主的底圖:data指定資料來源,aes指定X Y軸座標變數,color點線顏色,size點線大小 geom系列函數為主的幾何圖層:geom_point,geom_line, geom_histogram,geom_bar,geom_boxplot,geom+density stat系列函數為主的統計轉換圖層:stat_density facet函數的分層設定圖層:facet_wrap, facet_grid theme函數為主的背景圖層:theme,theme_bw,theme_gray 常用輔助函數:ggtitle,labs,xlab,ylab,xlim,ylim

75 ggplot2套件之一

76 ggplot2套件之一

77 # ggplot-1 library(ggplot2) house2010 = read.csv("d:/stella/R/house2010.csv") summary(house2010) p=ggplot(data=house2010,aes(x=area,y=price)) tLayer = theme(axis.text=element_text(size=20), axis.title=element_text(size=30,face="bold")) p + geom_point() + tLayer p=ggplot(data=house2010,aes(x=area,y=price,color=type)) p=ggplot(data=house2010,aes(x=area,y=price,color=type, size=room)) p + geom_point() + theme_bw() + ggtitle("房價 vs. 坪數") + xlab("坪數")+ ylab("房價")

78 ggplot2套件之二

79 # ggplot-2 tLayer = theme(axis.text=element_text(size=20), axis.title=element_text(size=30,face="bold"), legend.text=element_text(size=20), legend.title=element_text(size=20)) p=ggplot(data=house2010,aes(x=area,y=price, colour=type)) #p + geom_point() + facet_grid(~type) + tLayer p + geom_point() + facet_wrap(~type,nrow=1) + tLayer #p + geom_point() + facet_wrap(~type) + tLayer p + geom_point() + facet_wrap(~type,ncol=2) + tLayer

80 ggplot2套件之三

81 ggplot2套件之三

82 # gglpot2-3 house2010=read.csv("d:\\stella\\R\\house2010.csv",header=T) library(ggplot2) tLayer = theme(axis.text=element_text(size=20), axis.title=element_text(size=30,face="bold"), legend.text=element_text(size=20), legend.title=element_text(size=20)) # position = "identity" means respective probability density # position = “stack" means stack probability density p = ggplot(data=house2010,aes(x=price,fill=type)) p + stat_density(alpha=I(.3),position = "stack") + tLayer # geom_density function is ok too p + stat_density(alpha=I(.3))+facet_wrap(~type) + tLayer

83 ggplot2套件之四

84 ggplot2套件之四

85 # gglpot2-4 house2010=read.csv("d:\\stella\\R\\house2010.csv",header=T) library(ggplot2) tLayer = theme(axis.text=element_text(size=20), axis.title=element_text(size=30,face="bold"), legend.text=element_text(size=20), legend.title=element_text(size=20)) # boxplot p=ggplot(data=house2010,aes(x=area,y=price,color=type)) p + geom_boxplot() + facet_wrap(~type) + tLayer # histogram p=ggplot(data=house2010,aes(x=price)) ntype = nlevels(factor(house2010$type)) p + geom_histogram(bins=20,fill=rep(rainbow(20),times=ntype)) + facet_wrap(~type) + tLayer

86 ggplot2套件之五

87 ggplot2套件之五

88 # gglpot2-5 house2010=read.csv("d:\\stella\\R\\house2010.csv",header=T) tLayer = theme(axis.text=element_text(size=20), axis.title=element_text(size=30,face="bold"), legend.text=element_text(size=20), legend.title=element_text(size=20)) #p=ggplot(data=house2010,aes(x=area,y=price,color=type)) #p + geom_point() + facet_grid(parking~type) + tLayer p=ggplot(data=house2010,aes(x=area,y=price,color=type)) p + geom_point() + facet_wrap(~parking+type,nrow=2) + tLayer p=ggplot(data=house2010,aes(x=price)) ntype = nlevels(factor(house2010$type)) nparking = nlevels(factor(house2010$parking)) p + geom_histogram(bins=20, fill=rep(rainbow(20),times= ntype*nparking))+facet_wrap(~parking+type,nrow=2) + tLayer


Download ppt "R的版面配置與繪圖功能 R的版面配置與繪圖 R附加圖形應用與3D繪圖 ggplot2套組的繪圖功能 4 2 3 1 程式迴圈繪圖."

Similar presentations


Ads by Google