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

R語(yǔ)言ggplot2

之前下載過(guò)Rstudio,這次直接開(kāi)始學(xué)

準(zhǔn)備工作

  • 安裝并打開(kāi)包

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

用到了一個(gè)mpg數(shù)據(jù)框,不了解時(shí)可以?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))

可以手動(dòng)設(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)

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

統(tǒng)計(jì)變換

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

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

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

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

每個(gè)幾何對(duì)象函數(shù)都有一個(gè)默認(rèn)的統(tǒng)計(jì)變換,每個(gè)統(tǒng)計(jì)變換函數(shù)都又一個(gè)默認(rèn)的幾何對(duì)象(繪圖時(shí)用來(lái)計(jì)算新數(shù)據(jù)的算法叫做統(tǒng)計(jì)變換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)計(jì)變換角度作圖

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會(huì)調(diào)整數(shù)據(jù)在圖上的位置:

ggplot(data = diamonds, mapping = aes(x = cut, colour = clarity)) +
geom_bar(fill = NA, position = "identity") #加上identity會(huì)“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ù)橫著分開(kāi)

jitter讓重合的點(diǎn)抖動(dòng)開(kāi):

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)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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