學(xué)習(xí)小組Day4筆記--李斯亭

R語言ggplot2

之前下載過Rstudio回怜,這次直接開始學(xué)

準(zhǔn)備工作

  • 安裝并打開包

install.packages("tidyverse")
library(tidyverse)

用到了一個mpg數(shù)據(jù)框琅拌,不了解時可以?mpg

ggplot2作圖基本

  • 模板

ggplot(data = <DATA>) +
<GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))

注意+的位置,geom指圖的類型必指,mapping是加圖層,aesthetic是各種顯示的屬性

  • 顏色抚恒、大小温赔、透明度、形狀

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, size = class))

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, alpha = class))

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, shape = class))

可以手動設(shè)置屬性它褪,要放在aes外面:

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), color = "blue")

  • 分面

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ cyl)

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2)

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(. ~ cyl)

  • 分組

ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy, group = drv))

ggplot(data = mpg) +
geom_smooth(
mapping = aes(x = displ, y = hwy, linetype = drv),
)

ggplot(data = mpg) +
geom_smooth(
mapping = aes(x = displ, y = hwy, color = drv),
show.legend = FALSE #不顯示圖例
)

  • 全局映射

ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth(data = filter(mpg, class == "subcompact"), se = FALSE)

局部映射與全局映射沖突時饵骨,服從局部映射; se默認(rèn)顯示標(biāo)準(zhǔn)差

統(tǒng)計變換

這里用到新的diamond數(shù)據(jù)

  • 統(tǒng)計變換函數(shù)和幾何對象函數(shù)

ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))

ggplot(data = diamonds) +
stat_count(mapping = aes(x = cut))

每個幾何對象函數(shù)都有一個默認(rèn)的統(tǒng)計變換,每個統(tǒng)計變換函數(shù)都又一個默認(rèn)的幾何對象(繪圖時用來計算新數(shù)據(jù)的算法叫做統(tǒng)計變換stat)

  • 修改stat

demo <- tribble(
~cut, ~freq,
"Fair", 1610,
"Good", 4906,
"Very Good", 12082,
"Premium", 13791,
"Ideal", 21551
)

ggplot(data = demo) +
geom_bar(mapping = aes(x = cut, y = freq), stat = "identity")

  • 顯示比例

ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))

group把所有鉆石當(dāng)成一組

  • 從統(tǒng)計變換角度作圖

ggplot(data = diamonds) +
stat_summary(
mapping = aes(x = cut, y = depth),
fun.ymin = min,
fun.ymax = max,
fun.y = median
)

R for Data Science原文:stat_summary summarises the y values for each unique x value, to draw attention to the summary that you’re computing

位置調(diào)整

  • color和fill

ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, colour = cut)) #邊框

ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = cut)) #給柱子上色

ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity)) #根據(jù)clarity用fill上色

position會調(diào)整數(shù)據(jù)在圖上的位置:

ggplot(data = diamonds, mapping = aes(x = cut, colour = clarity)) +
geom_bar(fill = NA, position = "identity") #加上identity會“place each object exactly where it falls in the context of the graph”

ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge") #將數(shù)據(jù)橫著分開

jitter讓重合的點抖動開:

ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), position = "jitter")

坐標(biāo)系

  • 翻轉(zhuǎn)坐標(biāo)系

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot()

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()

  • 極坐標(biāo)

bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
show.legend = FALSE,
width = 1
) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)

bar + coord_flip()

bar + coord_polar()

總結(jié)公式

ggplot(data = <DATA>) +
<GEOM_FUNCTION>(
mapping = aes(<MAPPINGS>),
stat = <STAT>,
position = <POSITION>
) +
<COORDINATE_FUNCTION> +
<FACET_FUNCTION>

思維導(dǎo)圖

ggplot2
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末茫打,一起剝皮案震驚了整個濱河市居触,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌老赤,老刑警劉巖轮洋,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異抬旺,居然都是意外死亡弊予,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進(jìn)店門开财,熙熙樓的掌柜王于貴愁眉苦臉地迎上來汉柒,“玉大人,你說我怎么就攤上這事责鳍∧牍樱” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵历葛,是天一觀的道長正塌。 經(jīng)常有香客問我,道長恤溶,這世上最難降的妖魔是什么乓诽? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮宏娄,結(jié)果婚禮上问裕,老公的妹妹穿的比我還像新娘逮壁。我一直安慰自己孵坚,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布窥淆。 她就那樣靜靜地躺著卖宠,像睡著了一般。 火紅的嫁衣襯著肌膚如雪忧饭。 梳的紋絲不亂的頭發(fā)上扛伍,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天,我揣著相機(jī)與錄音词裤,去河邊找鬼刺洒。 笑死鳖宾,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的逆航。 我是一名探鬼主播鼎文,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼因俐!你這毒婦竟也來了拇惋?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤抹剩,失蹤者是張志新(化名)和其女友劉穎撑帖,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體澳眷,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡胡嘿,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了钳踊。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片灶平。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖箍土,靈堂內(nèi)的尸體忽然破棺而出逢享,到底是詐尸還是另有隱情,我是刑警寧澤吴藻,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布瞒爬,位于F島的核電站,受9級特大地震影響沟堡,放射性物質(zhì)發(fā)生泄漏侧但。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一航罗、第九天 我趴在偏房一處隱蔽的房頂上張望禀横。 院中可真熱鬧,春花似錦粥血、人聲如沸柏锄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽趾娃。三九已至,卻和暖如春缔御,著一層夾襖步出監(jiān)牢的瞬間抬闷,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工耕突, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留笤成,地道東北人评架。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像炕泳,于是被迫代替她去往敵國和親古程。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內(nèi)容