library(tidyverse)
基礎(chǔ)作圖
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
分別設(shè)好 x y軸及數(shù)據(jù)集即可
加顏色
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))
透明度和形狀
# 將車型class映射給透明度
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, alpha = class))
# 將車型class映射給形狀
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, shape = class))
(4)手動(dòng)設(shè)置圖形屬性
# 例:所有點(diǎn)設(shè)為藍(lán)色
# (注意:color="blue"在aes()外)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
(5)stroke-輪廓
# 適用于散點(diǎn)圖约啊,21-24號(hào)形狀
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, stroke = 3),shape=21)
5.分面
# (1)依據(jù)單個(gè)變量分面 facet_wrap() #分子圖
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2) #分兩行展示
# nrow指定分面后顯示幾行
# ncol指定分面后顯示幾列
# 注意~分面依據(jù)必須是離散型變量特碳。
(2)依據(jù)兩個(gè)變量分面 facet_grid()
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ cyl)
# 不需要指定nrow和ncol扣猫。
(3)不想在行或列維度中分面晋南,用.代替變量名
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(. ~ cyl)
6.幾何對(duì)象
# 也就是圖的不同類型寄摆,如點(diǎn)圖、折線圖愉粤、直方圖等杀迹。
# (1)理解分組
#
# 將一個(gè)圖形屬性映射為一個(gè)離散型變量,ggplot2就會(huì)自動(dòng)對(duì)數(shù)據(jù)進(jìn)行分組來(lái)繪制多個(gè)幾何對(duì)象校镐。這種形式是隱式分組亿扁,不需要添加圖例和區(qū)分特征。
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy))
# 將線性映射為drv(驅(qū)動(dòng)方式鸟廓,d,f,4)就會(huì)自動(dòng)變成三條線型不同的線从祝。
# 將顏色映射為drv,就會(huì)自動(dòng)變成三條顏色不用的線引谜。
#分組
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 #不顯示圖例
)
(2)同一張圖顯示多個(gè)幾何對(duì)象--局部映射和全局映射
# --這里涉及到圖層啦牍陌。
# 局部映射-映射只對(duì)改圖層有效
#
# 有多個(gè)幾何對(duì)象時(shí),映射語(yǔ)句要重復(fù)多次员咽,又丑又麻煩毒涧。
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
geom_smooth(mapping = aes(x = displ, y = hwy))
![image.png](https://upload-images.jianshu.io/upload_images/19009296-37dad55eb1601c71.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#全局映射--對(duì)所有圖層生效
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point() +
geom_smooth()
![image.png](https://upload-images.jianshu.io/upload_images/19009296-7d66299bb919081e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
# 局部映射與全局映射沖突時(shí),服從局部映射骏融。
例如:
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth(data = filter(mpg, class == "subcompact"), se = FALSE)
![image.png](https://upload-images.jianshu.io/upload_images/19009296-6e61517d84c433fe.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
# 如果出現(xiàn)報(bào)錯(cuò)链嘀,請(qǐng)library(dplyr) 或library(tidyverse)
# Error in class == "subcompact" : comparison (1) is possible only for atomic and list types
# 這個(gè)報(bào)錯(cuò)是因?yàn)閒ilter函數(shù)出自dplyr包,不加載就不能用~
# ps:關(guān)于se=FALSE
#
# se是standard error的縮寫(xiě)档玻,se參數(shù)為擬合曲線添加標(biāo)準(zhǔn)誤差帶怀泊,也就是那個(gè)灰不啦嘰的灰色背景帶,默認(rèn)是TRUE误趴。
參考
[R數(shù)據(jù)科學(xué)--詳解ggplot2](https://mp.weixin.qq.com/s?__biz=MzU4NjU4ODQ2MQ==&mid=2247484642&idx=1&sn=182a3a95a2dca006bdcb977bb8f53763&chksm=fdf84ca0ca8fc5b6a1982341ba0c659554c669146f75438f7a3c39eaac9171679d66059640ab&scene=21#wechat_redirect)