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>