R語言中基本圖形的繪製
阿新 • • 發佈:2018-12-24
條形圖
簡單的垂直條形圖和水平條形圖
函式barplot()
> library(vcd) 載入需要的程輯包:grid > counts <- table(Arthritis$Improved) > counts None Some Marked 42 14 28 > barplot(counts,main = "Simple Bar Plot",xlab = "Improved",ylab = "Frequency") > barplot(counts,main = "Horizontal Bar Plot",xlab = "Improved",ylab = "Frequency",horiz = TRUE)
堆砌條形圖和分組條形圖
若beside=FALSE(預設值)則每一列都將生成圖中的一個條形,各列中的值將給出堆砌的“子條”的高度。若base=TRUE則矩陣中的每一列都表示一個分組,各列中的值將並列而不是堆砌。
library(vcd) 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)
均值條形圖
條形圖不一定要基於計數資料或頻率資料。可以使用資料整合函式並將結果傳遞給barplot()函式,來建立表示均值、中位數、標準差等的條形圖。
#美國各地區平均文盲率排序的條形圖 states <- data.frame(state.region,state.x77) means <- aggregate(states$Illiteracy,by=list(state.region),FUN=mean) means <- means[order(means$x),] means barplot(means$x,names.arg = means$Group.1) title("Mean Illiteracy Rate")
條形圖的微調
隨著條形圖條數的增多,條形的標籤可能會開始重疊,可以使用cex.names來減小字號,將其指定為小於1的值可以縮小標籤的大小。可選的引數names.arg允許你指定一個字元向量作為條形的標籤名,同樣可以使用圖形引數輔助調整文字間隔。
####為條形圖搭配標籤
#增加y邊界的大小
par(mar=c(5,8,4,2))
#旋轉條形的標籤
par(las=2)
counts <- table(Arthritis$Improved)
barplot(counts,
main = "Treatment Outcome",
horiz = TRUE,
cex.names = 0.8, #縮小字型大小,讓標籤更合適
names.arg = c("No Improvement","Some Improvement", #修改標籤文字
"Marked Improvement"))
棘狀圖
棘狀圖對堆砌條形圖進行了重縮放,這樣讓每個條形的高度為1,每一段的高度即表示比例。可由vcd包中的函式spine()繪製。
library(vcd)
attach(Arthritis)
counts <- table(Treatment,Improved)
spine(counts,main = "Spinogram Example")
detach(Arthritis)
餅圖
餅圖在商業世界中應用廣泛,但是在學術領域往往被否定,這是因為相對於面積,人們對長度的判斷更精確。通過pie(x,labels)函式來建立。
par(mfrow=c(2,2)) #將四幅圖組合為一幅
slices <- c(10,12,4,16,8)
lbls <- c("US","UK","Australia","Germany","France")
pie(slices,labels = lbls,
main = "Simple Pie Chart")
pct <- round(slices / sum(slices) * 100) #為餅圖新增比例數值
lbls2 <- paste(lbls," ",pct,"%",sep = "")
pie(slices,labels = lbls2, col = rainbow(length(lbls2)),
main = "Pie Chart with Percentages")
library(plotrix)
pie3D(slices,labels = lbls,explode = 0.1,
main="3D Pie Chart")
my_table <- table(state.region)
lbls3 <- paste(names(my_table),"\n",my_table,sep = "")
pie(my_table,labels = lbls3,
main = "Pie Chart from a Table\n (with sample sizes)")
扇形圖
通過plotrix包中的fan.plot()函式實現的。
library(plotrix)
slices <- c(10,12,4,16,8)
lbls <- c("US","UK","Australia","Germany","France")
fan.plot(slices,labels = lbls, main = "Fan Plot")
直方圖
通過函式hist()創立。其中引數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$mpg
h <- 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()