使用GGPLOT2包進行數據可視化
-
Introduction簡介
geom_smooth可以用于散點圖,擬合一條直線,可以用于做預測
geom_point(alpha = 0.4)透明度,40%可見,60%透明
dia_plot <- ggplot(diamonds, aes(x = carat, y = price))
dia_plot + geom_smooth(aes(clarity = col), se = FALSE)其中SE = FALSE是取消灰色部分
-
Data數據
lm()函數通常用來擬合回歸模型
ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
geom_smooth(aes(group = 1), method = "lm", se = FALSE, linetype = 2)
facet_grid 將 grid 分成好幾個面
facet_grid(. ~ Measure)按measure分,measure中的變量為character
geom_jitter()調整,消除點的重合
-
Aesthetics
ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point(shape = 2,size = 4)
shape和size可以分別制定點的形狀以及大小
ggplot(mtcars, aes(x = wt, y = mpg, fill = cyl, col = am)) +
geom_point(shape = 21, size = 4, alpha = 0.6)fill是填充,此時再用col分顏色的話,col這邊就是邊框顏色
my_color <- "#4ABEFF"
geom_point(color = my_color)根據顏色編號,改變散點圖中所有點的顏色
geom_text(label = rownames(mtcars), color = 'red')直接將文本信息加入散點圖,即將散點替換為文本信息。
ggplot(mtcars, aes(x = mpg, y = qsec, col = factor(cyl), shape = factor(am))) +
geom_point()此處am和cyl都是num的類型,轉化為factor后才好合理的展示出來。
cyl.am +
geom_bar(position = "dodge") +
scale_x_discrete("Cylinders") +
scale_y_continuous("Number") +
scale_fill_manual("Transmission",
values = val,
labels = lab) 柱形圖
geom_bar(position = "stack")正常柱形圖
geom_bar(position = "fill")堆積柱形圖
geom_bar(position = "dodge") 百分比堆積柱形圖
scale_y_continuous(limits = c(-2,2))
ggplot(diamonds, aes(x = carat, y = clarity, col = price)) +
geom_point(alpha = 0.5, position = "jitter")此處的jitter應該還是為了消除點的重合,不過為什么是用position還是不太明白,后續知道了的話記得補充。
-
Geometries
ggplot(mtcars, aes(x = cyl, y = wt)) +
geom_point(position =position_jitter(0.1))
???jitter到底是啥意思
ggplot(mtcars, aes(x = cyl, y = wt)) +
geom_jitter(width = 0.1)
ggplot(mtcars, aes(mpg)) +
geom_histogram(aes(y = ..density..), binwidth = 1, fill = "#377EB8")
直方圖(僅限X為連續型變量)的構建,binwidth:組距
density(一維密度估計),x(組的中心估計)——默認利用count和x;如若#要引用這幾個變量,則在變量左右加雙圓點,譬如 ..density..
ggplot(mtcars, aes(x = cyl, fill = am)) +
geom_bar(position = position_dodge(width = 0.2))柱狀中,倆相鄰的柱子部分重合
位置調整參數:
dodge:“避讓”方式,即往旁邊閃,如柱形圖的并排方式就是這種。
fill:填充方式, 先把數據歸一化,再填充到繪圖區的頂部。
identity:原地不動,不調整位置
jitter:隨機抖一抖,讓本來重疊的露出點頭來
stack:疊羅漢?
geom_freqpoly()頻數多邊形圖
原理是跟直方圖一樣的,其中binwidth參數必須給出。
ggplot(mtcars, aes(mpg, col = cyl)) +
geom_freqpoly(binwidth = 1, position = "identity")
ggplot(mtcars, aes(x = cyl, fill = am)) +
geom_bar() +
scale_fill_brewer(palette = "Set1")
第三行為顏色的設置
RColorBrewer包提供了3套很好的配色方案。用戶只需要指定配色方案的名稱,就可以用包中的brewer.pal()函數生成顏色
連續型Sequential:生成一系列連續漸變的顏色,通常用來標記連續型數值的大小。
極端型Diverging:生成用深色強調兩端、淺色標示中部的系列顏色,可用來標記數據中的離群點。
離散型Qualitative:生成一系列彼此差異比較明顯的顏色,通常用來標記分類數據。
display.brewer.all(type = "qual")展示離散型的全部顏色
scale_fill_manual()自定義填充顏色,和 scale_fill_brewer對比之。
-
qplot and wrap-up
The old way (shown)舊方法:
plot(mpg ~ wt, data = mtcars) # formula notation
with(mtcars, plot(wt, mpg)) # x, y notation
Using ggplot:使用ggplot2:
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()
Using qplot:使用qplot:
qplot(wt, mpg, data = mtcars)
qplot(wt, mpg, data = mtcars, size = factor(cyl))qplot的格式,會自動選擇圖表類型。
在aes中或者qplot中時,size = ...時,num格式要使用factor()將其轉化為factor格式
geom_dotplot()
ggplot(ChickWeight, aes(x = Time, y = weight, color = Diet)) +
geom_line(aes(group = Chick), alpha = 0.3) +
geom_smooth(lwd = 2, se = FALSE)
折線圖,其中group是分組,但是不是很明白smooth中的擬合線中lwd,se是什么意思
-
Statistics統計
stat_smooth()這是啥意思?——散點圖的擬合線
geom_smooth(method = "lm", se = FALSE)method是規定擬合線的形狀,此處為直線,se是把擬合線周圍灰色部分去除
scale_color_manual(values = ....)手動設置顏色
stat_smooth(method = "loess", aes(group = 1, col = "All"),
se = FALSE, span = 0.7)分組設置的擬合線,其中ALL是匯總在一起的擬合線。
scale_color_gradientn(colors = brewer.pal(9, "YlOrRd"))當那個顏色包只有9個時,可以使用這個設置為漸變的
stat_quantile()分位數回歸
stat_sum()
scale_size()啥意思?怎么用?
stat_summary()
quantile()分位數
-
Coordinates and Facets協作...方面?
scale_x_continuous(limits = c(3, 6), expand = c(0, 0))
coord_cartesian(xlim = c(3, 6))
coord_equal()
coord_fixed()這幾個圖層都是什么意思
stat_bin()
geom_bar()餅圖
coord_polar(theta = "y")
geom_bar(width = .1) 這里的0為什么要省去
facet_grid()
facet_grid 將 grid 分成好幾個面
facet_grid(.~ Name) # 按Name分面并且Name 內容顯示在頂部
fact_grid(Name~.) #按name分面,Name顯示在右邊
facet_grid(Name~Left) #按兩個參數分面 (參數1按行方式進行分面,參數2按列方式分面?)
-
Themes主題
library(ggthemes)
no_panels <- theme(rect = element_blank())
z +
no_panels +
theme(plot.background = element_rect(fill = myPink, color = "black", size = 3))
z +
theme(panel.grid = element_blank(),
axis.line = element_line(color = "red"),
axis.ticks = element_line(color = "red"))
theme(legend.position = )規定圖例的位置
theme_bw()
theme_classic()
theme_gray()
theme_tufte()
-
Best Practices練習
stat_summary繪制匯總數據
ggplot(mtcars, aes(x = cyl, y = wt)) +
stat_summary(fun.y = mean, geom = "bar", fill = "skyblue") +
stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1), geom = "errorbar", width = 0.1)
library(GGally)
ggparcoord()
scale_fill_gradientn(colors = )是啥子意思?
cf facet_wrap與facet_grid
facet_wrap和facet_grid不同在于facet_wrap是基于一個因子進行設置,facets表示形式為:變量(單元格)
而facet_grid是基于兩個因子進行設置,facets表示形式為:變量變量(行列),如果把一個因子用點表示,也可以達到facet_wrap的效果,也可以用加號設置成兩個以上變量
-
Case Study案列教學
geom_histogram()直方圖 是要設置寬度的,沒有y值
ggplot(adult, aes (x = SRAGE_P, fill= factor(RBMI))) +
geom_histogram(binwidth = 1)
table()
library(reshape2)
library(ggthemes)