########0510_tidyy資料格式與ggplot2畫圖####
# Alt+-(用於賦值運算符<-)
####安裝套件####
install.packages("ggplot2")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.1'
## (as 'lib' is unspecified)
install.packages("titanic")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.1'
## (as 'lib' is unspecified)
install.packages("dplyr")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.1'
## (as 'lib' is unspecified)
install.packages("tidyverse")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.1'
## (as 'lib' is unspecified)
# 多行註解 『 Command + Shift + C 』,若是想要取消註解,僅需將段落反白再執行一次Command + Shift + C 即可。
####tidyy資料格式常用的函數####
# 選取tibble中的幾個Variables: select()
# 依照位置選取tibble中的Observations: slice()
# 根據條件選取tibble中的Observations: filter()
# 增加或修改tibble的Variables: mutate()
# 依照某個Variable的資料數值大小,排列Observations: arrange()
# 依照某個Variable的資料數值大小,選出前幾筆Observations: top_n()
# 依照某個Variable的資料數值,將Observations分群: group_by()
# 對Observatiosn進行彙整(加總、平均、…): summarise()
# 刪除NA值:filter(欄位名稱!="NA") 或是 filter(!is.na(欄位名稱))
####ggplot2畫圖文法####
# 資料來源(data):指定原始資料來源的 data frame。
# 美學對應(aesthetic):指定原始資料與圖形之間的對應關係,例如哪一個變數要當作 x 座標變數,而哪一個要當作 y 座標變數,還有資料繪圖時的樣式等。
# 幾何圖案(geometry):要用什麼幾何圖形繪製資料,例如點、線條、多邊形等。
# 繪圖面(facet):指定如何將資料分散在多張子圖形中繪製,以利互相比較。
# 統計轉換(statistical transformation):指定如何以將資料轉換為各種統計量,例如將連續型資料轉為離散型的類別。
# 座標系統(coordinate system):指定繪圖時所使用的座標系統,除了常見的笛卡兒直角座標系統,也可以使用極坐標或地圖投影(map projection)。
# 主題(theme):控制資料以外的繪圖組件,例如座標軸、說明文字等。
#用iris資料畫三張圖
iris <- iris
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
library(ggplot2)
#長條圖geom_bar()
ggplot(iris, aes(x = Species, fill= Species))+
geom_bar()+
labs(x= "品種", y= "數量", title = "iris直條圖geom_bar", subtitle = "洪葦珊")

#長條圖geom_col()
ggplot(iris, aes(x = Species, y= Petal.Length, fill= Species))+
geom_col()+
labs(x= "品種", y= "花瓣長度", title = "iris直條圖geom_col", subtitle = "洪葦珊")

#放射圖coord_polar()
ggplot(iris, aes(x = Species, y= Petal.Length, fill= Species))+
geom_col()+
coord_polar()+ #設定坐標軸為放射狀
labs(x= "品種", y= "花瓣長度", title = "iris放射圖coord_polar", subtitle = "洪葦珊")

#散佈圖
ggplot(iris, aes(x = Sepal.Length, y= Petal.Length, color= Species))+
geom_point()+
labs(x= "花萼長度", y= "花瓣長度", title = "iris散佈圖geom_point", subtitle = "洪葦珊")

ggplot(iris, aes(x = Sepal.Length, y= Petal.Length, size=Sepal.Width, color= Species))+
geom_point()+
labs(x= "花萼長度", y= "花瓣長度", title = "iris散佈圖geom_point_size = Sepal.Width", subtitle = "洪葦珊")

#分組散佈圖facet_grid(.~類別變項)
ggplot(iris, aes(x = Sepal.Length, y= Petal.Length, color= Species))+
geom_point()+
facet_grid(.~Species)+
labs(x= "品種", y= "花瓣長度", title = "iris_facet_grid_geom_point", subtitle = "洪葦珊")

#盒狀圖
ggplot(iris, aes(x = Species, y= Petal.Length, fill= Species))+
geom_boxplot()+
labs(x= "品種", y= "花瓣長度", title = "iris盒狀圖geom_boxplot", subtitle = "洪葦珊")

#盒狀圖
ggplot(iris, aes(x = Petal.Length, fill= Species))+
geom_histogram()+
labs(x= "花瓣長度",y = "數量", title = "irisje 直方圖geom_histogram", subtitle = "洪葦珊")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
iris %>%
group_by(Species) %>%
summarise(avg= mean(Petal.Width)) %>%
ggplot(aes(x=Species, y=avg, fill= Species))+
geom_col()
