Explore
在探索連續(xù)型數(shù)據(jù)時,sd、var、range等都是常用函數(shù)。先cut,再table則可以將連續(xù)轉(zhuǎn)為離散再進(jìn)行觀察。
在比較兩個多更多等長的numeric vector時,pmin()和pmax()可以輸出再每一個位置最小/最大的值。
> (x=sample(10,10));(y=sample(10,10));(z=sample(10,10))
[1] 5 8 2 4 6 9 1 3 7 10
[1] 4 5 8 7 10 2 6 3 9 1
[1] 10 9 2 1 7 8 3 4 6 5
> pmin(x,y,z)
[1] 4 5 2 1 6 2 1 3 6 1
cummin和cummax接受一個vector,輸入這個vector的最小/最大 so far的值。
> cummax(x)
[1] 5 8 8 8 8 9 9 9 9 10
quantile()函數(shù)則提供了vector的xxx%分位:
> (x=rnorm(20))
[1] -0.97392547 -2.68243940 -0.03796838 -0.65249979 -0.28756329 0.38868737 0.68847986 -0.43226118
[9] 1.55034408 1.30703724 0.01662464 -0.46428297 -0.12325135 1.20905396 -1.03437545 1.26728394
[17] -0.42276944 0.10972380 2.16093479 -0.54529102
> quantile(x)
0% 25% 50% 75% 100%
-2.68243940 -0.48453498 -0.08060986 0.81862339 2.16093479
> quantile(x,c(.1,.3,.5,.7,.9))
10% 30% 50% 70% 90%
-0.97997047 -0.44186772 -0.08060986 0.47862511 1.33136792
fivenum()是quantile的一個速度優(yōu)化版本,特定輸出5個分位(最小,最大,中值,25%,75%)的值。
> fivenum(x)
[1] -2.68243940 -0.50478700 -0.08060986 0.94876691 2.16093479
Plotting
ggplot2在繪制barplot時,要讓bar不堆積(默認(rèn)設(shè)置‘stack’),應(yīng)該如下指令。position的其他參數(shù)還有‘fill’
geom_bar(stat = 'identity' , position = 'dodge')
要轉(zhuǎn)置整個圖像,使用如下命令:
coord_flip()