ggplot2 繪圖總結

圖形屬性

x

y

xmin

xmax

ymin

ymax

xend

yend

weight

color:輪廓色

fill:填充色

shape:點形狀

linetype: dotted dashed

size:點大小,線條大小(粗細)

alpha:透明度,0-1:完全透明-完全不透明

width:寬度(條形圖等)

binwidth:組距(直方圖等)

label:名稱(如x,y,legend等)

angle:角度

hjust:水平平移

vjust:垂直平移

lower

middle

upper

map_id

group分組

position位置調整

點圖

ggplot(data=mpg,aes(x=displ,y=hwy))+

geom_point(color='grey')#輪廓色為灰色(實質效果:填充部分也為灰色)

ggplot(data=mpg,aes(x=displ,y=hwy))+

geom_point(fill='blue')#點圖無填充色選項,所以等于空設置;而color默認黑色

ggplot(data=mpg,aes(x=displ,y=hwy))+

geom_point(aes(color=cyl),alpha=I(0.6)) #設置透明度,alpha范圍是0到1,全透明到不透明;I()表示設定,跟映射函數aes()相對

ggplot(data=mpg,aes(x=displ,y=hwy))+

geom_point(aes(color=factor(cyl)),alpha=0.6) #cyl為整數,系統識別#默認是連續變量,所以生成的圖例是連續型顏色圖例;factor使之因子化

ggplot(data=mpg,aes(x=displ,y=hwy))+

geom_point(aes(color=factor(cyl),shape=factor(cyl)),alpha=0.6)

將cyl映射至形狀屬性

折線圖

ggplot(data=mpg,aes(x=displ,y=hwy))+

geom_line(color='grey',size=2)

條形圖

ggplot(data=mpg,aes(x=factor(displ),y=hwy))+

geom_bar(stat='identity',width=0.8,color='green',fill='grey')

stat是指統計變換;#stat='identity'是指不進行統計變換即hwy就是縱

坐標值因為displ出現眾多的重復值,所以分組更多,縱坐標值不斷累加

width為組距,color為輪廓色(可以看到y值累加上去),fill為填充色

ggplot(data=mpg,aes(x=displ))+

geom_bar(stat='density')

統計變換為密度即y為displ的密度分布;注意,這里不需要給y映射變量

統計變換為bin,中文翻譯是bin封箱,其過程是生成變量count(對x計數),density(一維密度估計),x(組的中心估計)——默認利用count和x;如若#要引用這幾個變量,則在變量左右加雙圓點,譬如 ..density..

?

直方圖:

僅限于x為連續型變量,如果x為離散型則該函數報錯——此時可以利用條線圖來繪制直方圖

ggplot(data=mpg,aes(x=displ,fill=fl))+

geom_histogram(binwidth=0.2,position=”stack”)

binwidth 為The width of the bins;不同于條形圖的width

position是指位置調整,stack是堆疊即同組幾何對象堆疊

?ggplot(data=mpg,aes(x=displ,fill=fl))+

geom_histogram(binwidth=0.4,position='dodge')

position是指位置調整,dodge是同組幾何對象并列

位置調整的參數還有:fill jitter identity

箱線圖:

ggplot(data=mpg,aes(x=factor(fl),y=hwy))+

geom_boxplot(color='grey')

ggplot(data=mpg,aes(x=1,y=hwy))+

geom_boxplot(fill='grey',color='blue') #hwy不分組

ggplot(data=mpg,aes(x=1,y=hwy))+

geom_boxplot(fill='grey',color='blue',outlier.colour= "red", outlier.shape = 1) #高亮異常值并賦予特定的幾何對象

曲線密度圖

使用geom_density

ggplot(data=mpg,aes(x=displ,fill=fl))+geom_density(color='white',size=0.1,alpha=I(0.3))

ggplot(data=mpg,aes(x=displ,y=..density..))+geom_histogram(fill='grey',binwidth=0.18,alpha=I(0.3))+geom_density(color='white',size=0.8)

幾乎看不到密度曲線。原因:直方圖和密度圖結合在一起。直方圖中bin變換生成y變量有count和density,默認使用前者,這樣由于count很大,density很小(總是小于1),就會值得密度線處于低位,難以看到,所以需要y=..density..(引用bin變換的數據必須前后加雙圓點)

?

ggplot(data=mpg,aes(x=displ))+geom_histogram(fill='grey',binwidth=0.18,alpha=I(0.3))+geom_density(color='white',size=0.8)

???

餅圖

ggplot(data=mpg,aes(x=1,fill=fl))+geom_bar()+

coord_polar(theta='y')

?coord_polar是極坐標的意思,區別以往的笛卡爾坐標。coord_polar()作用是把把笛卡爾坐標變換為極坐標。該函數有theta,start,direction三個參數,后者者頂多是圖的微調,需要了解可以查看幫助文件,theta才是關鍵。極坐標參數theta有兩個指標半徑和角度,就餅圖而言,各部分內容的角度不同,半徑相同;而默認theta=”x”,即將x映射為角度,剩下的y映射為半徑。這些為前期準備,下面一步步分解這個過程。

First step: 生成條形圖,其中各部分的比例關系用y反映。

ggplot(data=mpg,aes(x=1,fill=fl))+geom_bar()

?

second step: 將坐標極坐標化,y映射為角度(此時x恒等于1,即半徑相同)

ggplot(data=mpg,aes(x=1,fill=fl))+geom_bar()+

coord_polar(theta='y')

ggplot(data=mpg,aes(x=1,fill=fl))+geom_bar()+

coord_polar(theta=”x”)

如果x映射為角度,則y映射為半徑,生成眼圖

?

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容