[讀書筆記] R語言實戰 (六) 基本圖形方法

來源:互聯網
上載者:User

標籤:拼接字串   旋轉   一個   nta   library   marked   參數   sep   有序   

1.  橫條圖 barplot()

#載入vcd包library(vcd)#table函數提取各個維度計數counts <- table(Arthritis$Improved)counts#繪製簡單Improved橫條圖#1行2列par(mfrow=c(1,2))barplot(counts, main = "simple Bar plot", xlab = "Improved",ylab = "Frequency")#繪製水平橫條圖 horiz = TRUEbarplot(counts,main = "simple Bar plot", xlab = "Frequency",ylab = "Improved",horiz=TRUE)#如果繪製的是一個有序因子,可以使用plot()函數快速建立一幅垂直橫條圖#1行1列par(mfrow=c(1,1))plot(Arthritis$Improved,xlab = "Frequency",ylab = "Improved",horiz=TRUE)#如果要繪製的變數是一個矩陣而不是一個向量,將會繪製堆砌橫條圖或者分組橫條圖#產生Improved和Treatment列聯表counts <- table(Arthritis$Improved,Arthritis$Treatment)counts#繪製堆砌圖barplot(counts,main = "Stacked Bar plot",xlab = "Treatment",ylab = "Frequency",col = c(‘red‘,‘yellow‘,‘green‘),legend = rownames(counts))#繪製分組橫條圖barplot(counts,main = "Grouped Bar plot",xlab = "Treatment",ylab = "Frequency",col = c(‘red‘,‘yellow‘,‘green‘),legend = rownames(counts),beside = TRUE)#橫條圖微調#增加y邊界大小par(mar = c(5,8,4,2))#las=2旋轉條形標籤par(las = 2)counts <- table(Arthritis$Improved)#cex.names= 0.8縮小字型的大小barplot(counts,main="Treatment Outcomes",horiz = TRUE,cex.names=0.8,names.arg = c("No Improvement","Some Improvement","Marked Imporvement"))

2. 餅圖:餅圖在商業世界中無所不在,然而多數統計學家,包括R相應文檔的編寫者,都對它持否定態度。相對於餅圖,他們更推薦使用橫條圖或點圖,因為相對於

面積,人們對長度的判斷更為精確 pie() pie3D()

par(mfrow=c(2,2))slices <- c(10,12,12.4,16,8)lbls <- c("US","UK","Austrialia","Germany","France")pie(slices,labels=lbls,main="simple Chart")pct <- round(slices/sum(slices)*100)#拼接字串和比例數值lbls2 <- paste(lbls," ", pct,"%",sep = "")lbls2pie(slices,labels=lbls2,col=rainbow(length(lbls2)),main="Pie chart with percentage")#載入ploirix包library(plotrix)#畫簡單3D圖pie3D(slices,labels=lbls,explode = 0.4,main = "3D pei chart")#從表格建立餅圖#table取得region的計數表mytable <- table(state.region)#names 函數取得列名,然後將列名和相應的計數拼接在一起lbls3 <- paste(names(mytable),‘\n‘,mytable,sep="")pie(mytable,labels=lbls3,main="pie chart from a table\n{with sample sizes}")

3. 長條圖 :可以展示連續變數的分布. hist(x,breaks=, freq = )

x是一個由資料值組成的數值向量,參數freq = FALSE表示根據機率密度而不是頻數繪製圖形,參數breaks用於控制數組的數量.

par(mfrow = c(2,2))#簡單長條圖hist(mtcars$mpg)#指定組數和顏色hist(mtcars$mpg,breaks=12,col=‘red‘,xlab="Miles Per Gallon",main="Colored histogram with 12 bins")hist(mtcars$mpg,freq=FALSE,breaks=12,col=‘red‘,xlab="Miles Per Gallon",main="Histogram,rug plot, density curve")rug(jitter(mtcars$mpg))lines(density(mtcars$mpg),col=‘blue‘,lwd=2)x <- mtcars$mpgh <- hist(x,breaks=12,col=‘red‘,xlab="Miles Per Gallon",main="Histogram with normal curve and box")#設定橫軸範圍和分度xfit <- seq(min(x),max(x),length=40)yfit <- dnorm(xfit,mean=mean(x),sd=sd(x))yfit <-yfit*diff(h$mids[1:2]*length(x))lines(xfit,yfit,col=‘blue‘,lwd=2)box()

  

 

[讀書筆記] R語言實戰 (六) 基本圖形方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.