基本格式
ggplot(數(shù)據(jù)集, aes(..............))
+ 圖層 goem_xxxx()
+ 圖層 goem_xxxx()
+ 坐標(biāo)軸設(shè)置(比如xlim, scale_x_continuous)
+ 坐標(biāo)軸標(biāo)題(比如xlab)
每個(gè)圖層其實(shí)就對(duì)應(yīng)傳統(tǒng)的折線(xiàn)圖的折現(xiàn)、散點(diǎn)圖的點(diǎn)、或者其他東西吗垮。
圖層
大部分是以geom_開(kāi)頭的函數(shù)。
另外有一部分是以stat_開(kāi)頭的凛辣。比如:stat_ecdf() 繪制經(jīng)驗(yàn)累積分布函數(shù)抱既。
數(shù)據(jù)集哪個(gè)列做x,哪個(gè)列做y是在哪里定義的扁誓?
在aes(x, y, ....) 防泵。前2個(gè)參數(shù)就是定義x和y蚀之。這個(gè)aes是可以繼承的。在ggplot定義捷泞,也可以在圖層goem_xxx(aes(...))里定義足删。
疑問(wèn):xlim和xmax,xmin是什么關(guān)系?
這兩組東西功能是否重復(fù)呢锁右?
不重復(fù)失受。xlim控制的是坐標(biāo)軸。而xmax,xmin,ymax,ymin是控制geom_rect()繪制的矩形咏瑟。
aes()里面可以裝什么東西拂到?
fill,color等等
可以查下面的參考。搜索“aesthetics”码泞,就能找到比如Position related aesthetics兄旬,Differentiation related aesthetics, Colour related aesthetics, Aesthetics: grouping
scale__ 是什么?
scale是坐標(biāo)軸余寥,能設(shè)置刻度领铐。
| scale_x_continuous | Position scales for continuous data (x & y) |
facet是什么?
分類(lèi)(分組)宋舷。根據(jù)一個(gè)變量或者2個(gè)變量的值绪撵,把原來(lái)的數(shù)據(jù)集分成多個(gè)組,然后為每個(gè)組畫(huà)子圖祝蝠。如果是1個(gè)變量音诈,可以用facet_wrap畫(huà)一維的(但是自動(dòng)換行)。如果是2個(gè)分類(lèi)變量绎狭,則用facet_grid畫(huà)二維的圖改艇。
參考
《A Layered Grammar of Graphics》p6
To be precise, the layered grammar defines the components of a plot as:
? a default dataset and set of mappings from variables to aesthetics,
1 數(shù)據(jù)集、aes
? one or more layers, with each layer having one geometric object, one statistical transformation, one position adjustment, and optionally, one dataset and set of aesthetic
mappings,
2 圖層
? one scale for each aesthetic mapping used,
3 刻度
? a coordinate system,
4 坐標(biāo)系
? the facet specification.
5 分面
ggplot2-wires.pdf
文章只有10頁(yè)坟岔。通過(guò)實(shí)例來(lái)介紹ggplot2。
復(fù)雜度有現(xiàn)成的函數(shù)
Modern Data Visualization with R
比如上面文章做的瀑布圖∷よ耄現(xiàn)在有函數(shù)可以直接調(diào)用社付。
R for data science
包羅各個(gè)方面話(huà)題的書(shū)。不只是繪圖了邻耕。還有數(shù)據(jù)處理鸥咖。模型等等。