引言
散點(diǎn)圖是描繪兩個(gè)連續(xù)型變量之間關(guān)系的圖形,,特別是在觀察兩個(gè)變量之間的相關(guān)關(guān)系時(shí)特別好使,。
散點(diǎn)圖基本操作
aes中的x,y值分別表示在x,y軸的變量;geom_point表示增加三點(diǎn)圖圖層,,其中的size控制點(diǎn)的大小,,shape控制形狀,一共25個(gè),,為0-25,。
library(gcookbook)
library(ggplot2)
head(heightweight)
# sex ageYear ageMonth heightIn weightLb
#1 f 11.92 143 56.3 85.0
#2 f 12.92 155 62.3 105.0
#3 f 12.75 153 63.3 108.0
#4 f 13.42 161 59.0 92.0
#5 f 15.92 191 62.5 112.5
#6 f 14.25 171 62.5 112.0
ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point(size=3,shape=21)
分組散點(diǎn)圖
散點(diǎn)圖分組有兩種方式,一種利用shape,以點(diǎn)的形狀來區(qū)分各種,;一種用color,,以點(diǎn)的顏色來區(qū)分.但是得記住,,分組的變量必須為因子變量或者字符串。
ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=sex)) + geom_point()#以顏色區(qū)分
ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex)) + geom_point()#以形狀區(qū)分
解決點(diǎn)覆蓋問題
當(dāng)點(diǎn)的密度大時(shí),,我們可以改變點(diǎn)的透明度來區(qū)分各個(gè)點(diǎn),。當(dāng)然我們可以使用bin的方法來區(qū)分,這種方法是把點(diǎn)的形狀設(shè)定為長方形,,密度越大的長方形區(qū)域越透明,。
ggplot(diamonds, aes(x=carat, y=price))+ geom_point(alpha=.1)
ggplot(diamonds, aes(x=carat, y=price))+ geom_point(alpha=.01)
ggplot(diamonds, aes(x=carat, y=price))+ stat_bin2d()
當(dāng)其中的一個(gè)或者兩個(gè)變量為離散型數(shù)據(jù)時(shí),也會(huì)導(dǎo)致覆蓋的問題,。我們可以使用jitter方法和box的方法,。
ggplot(ChickWeight, aes(x=Time, y=weight))+geom_point()
ggplot(ChickWeight, aes(x=Time, y=weight))+geom_point(position="jitter")
#position="jitter"等價(jià)于geom_jitter,即下面的方法
ggplot(ChickWeight, aes(x=Time, y=weight))+geom_point(position=position_jitter(width=.5, height=0))
ggplot(ChickWeight, aes(x=Time, y=weight))+ geom_boxplot(aes(group=Time))
增加擬合的回歸線
為了更好的看出兩個(gè)變量之間的關(guān)系,,我們還可以擬合回歸線,,用stat_smooth(method=lm)函數(shù)即可。
ggplot(heightweight, aes(x=ageYear, y=heightIn))+geom_point() + stat_smooth(method=lm)
#該回歸直線的置信區(qū)間默認(rèn)的置信度是95%,,我們也可以對(duì)其進(jìn)行修改,。
ggplot(heightweight, aes(x=ageYear, y=heightIn))+ geom_point() + stat_smooth(method=lm, level=0.99)#99%的置信度
#也可以不顯示置信區(qū)間
ggplot(heightweight, aes(x=ageYear, y=heightIn))+ geom_point(colour="grey60") + stat_smooth(method=lm, se=FALSE)
還有很多關(guān)于增回歸線的技術(shù),這里暫時(shí)不涉及,,這本身還需要相關(guān)的計(jì)量知識(shí)來支撐的,,需要了解的大家再查看書籍對(duì)應(yīng)部分即可。
給點(diǎn)增加標(biāo)簽
有時(shí)候,,當(dāng)點(diǎn)數(shù)量不多時(shí),,我們想讓點(diǎn)顯示名稱來進(jìn)行區(qū)分,用geom_text就能做到,,只要給參數(shù)label賦予對(duì)應(yīng)的名字即可,。
ggplot(subset(countries, Year==2009 & healthexp>2000),
aes(x=healthexp, y=infmortality)) +geom_text(aes(label=Name), size=4)
氣泡圖
有時(shí)候我們想展示三個(gè)變量之間的關(guān)系,這時(shí)氣泡圖是一個(gè)很好的工具,。
cdat <- subset(countries, Year==2009 &
Name %in% c("Canada", "Ireland", "United Kingdom", "United States",
"New Zealand", "Iceland", "Japan", "Luxembourg",
"Netherlands", "Switzerland"))
#氣泡大小用size參數(shù)進(jìn)行控制,,把第三個(gè)變量賦值給該參數(shù)就行。
ggplot(cdat, aes(x=healthexp, y=infmortality, size=GDP)) +
geom_point(shape=21, colour="black", fill="cornsilk")
創(chuàng)建散點(diǎn)矩陣圖
散點(diǎn)矩陣圖是展示多個(gè)變量相互之間的一個(gè)極好展示,。
c2009 <- subset(countries, Year==2009,
select=c(Name, GDP, laborrate, healthexp, infmortality))
pairs(c2009[,2:5])
這里我們不用ggplot2包進(jìn)行,,這是因?yàn)樵摪惶m合對(duì)這類圖形進(jìn)行展示。
|