close

#ggplot2
install.packages("ggplot2")
library(ggplot2)

#探索兩個數值關係: 散佈圖
ggplot(cars, aes(x= speed, y= dist))+ #以"cars"的兩個變數繪製散佈圖,aes()負責將素材綁定到X跟Y軸,aes是aesthetic mappings之縮寫
geom_point() #指定繪圖的形式,底線後面代表加上素材的方式
??geom #查詢可繪製的形式

#調整資料點的形狀跟顏色
ggplot(iris, aes(x= Sepal.Length, y= Sepal.Width)) + 
  geom_point(aes(shape= Species, col= Species))

#探索數值分布: 直方圖
set.seed(123)
norm_nums<- rnorm(1000)
hist_df<- data.frame(norm_nums= norm_nums)
ggplot(hist_df, aes(x= norm_nums)) + geom_histogram() 
#geom_histogram()預設分箱數(bin)= 30,可使用binwidth調整
ggplot(hist_df, aes(x= norm_nums)) + geom_histogram(binwidth = 0.1) #比預設bin多
ggplot(hist_df, aes(x= norm_nums)) + geom_histogram(binwidth = 0.5) #比預設bin少

#加上密度曲線: 在geom_histogram() 中加入 aes(y= ..density..)以及加入 geom_density()
ggplot(hist_df, aes(x= norm_nums)) + 
  geom_histogram(binwidth = 0.5, aes(y= ..density..), alpha= 0.8) +
  geom_density() #alpha設定圖形顏色深度

#探索不同類別與數值分布關係: 盒鬚圖
ggplot(iris, aes(x= Species, y= Sepal.Length)) + geom_boxplot()

#繪製多個圖形: ggplot2本身並沒有繪製多個圖形的函數,需要使用套件 gridExtra的 grid.arrange()達到 par(mfrow= c(m,n))的效果
library(gridExtra)
g1<- ggplot(iris, aes(x= Species, y= Sepal.Length)) + geom_boxplot()
g2<- ggplot(iris, aes(x= Species, y= Sepal.Width)) + geom_boxplot()
g3<- ggplot(iris, aes(x= Species, y= Petal.Length)) + geom_boxplot()
g4<- ggplot(iris, aes(x= Species, y= Petal.Width)) + geom_boxplot()
grid.arrange(g1,g2,g3,g4, nrow= 2, ncol= 2)

#探索數值與日期(時間)關係: 折線圖
x<- seq(from= as.Date("2018-08-01"), to = as.Date("2018-08-31"), by=1)
set.seed(123) #確保得到相同y值
y<- sample(1:1000, size= 31, replace = T)
line_df<- data.frame(x, y)
ggplot(line_df, aes(x,y)) + geom_line() +scale_x_date(date_labels = "%m.%d") #以scale_x_date()調整日期顯示格式,預設為"%b %d",改成以"%m.%d",代表月份與日都以數字表示,且中間已"."隔開。

#探索類別: 長條圖
#紀錄100個人最喜歡的冰淇淋口味

ice_cream_flavor<- rep(NA, times= 100)
for (i in 1:100) {
  ice_cream_flavor[i] <- sample( c("villilla","chocolate","matcha","other"), size = 1)
} #隨機填入每個人喜好的口味
ice_cream_flavor_df <- data.frame(ice_cream_flavor = ice_cream_flavor)
ggplot(ice_cream_flavor_df, aes(x=ice_cream_flavor)) + geom_bar()
#與R內建的Base Plotting System不同在於不需要再使用table()統計各口味人數才能繪圖,ggplot2會自動分類變數並加總

#調整圖形為水平方向: coord_flip(), coordinate flip
ggplot(ice_cream_flavor_df, aes(x=ice_cream_flavor)) + geom_bar() +
  coord_flip()

#若資料已是統計過的資訊,則需指定一個參數 stat= "identity"
ice_cream_flavor<- rep(NA, times= 100)
for (i in 1:100) {
  ice_cream_flavor[i] <- sample( c("villilla","chocolate","matcha","other"), size = 1)
}
flavor<- names(table(ice_cream_flavor))
votes<- as.vector(unname(table(ice_cream_flavor)))
ice_cream_flavor_df<- data.frame(flavor= flavor, votes= votes)
ggplot(ice_cream_flavor_df, aes(x= flavor, y= votes)) + geom_bar(stat= "identity")
#geom_bar()會預設計算類別變數中的相異個數: stat="count",也就是table()的功能

#繪製函數
sin_df<- data.frame(x= c(-pi, pi))
ggplot(sin_df, aes(x=x)) + stat_function(fun= sin, geom = "line") #將sin()函數在-pi到pi之間描繪出來
#自訂函數並繪製
my_sqr<- function(x){
  return(x^2)
}
my_sqr_df<- data.frame(x= c(-3,3))
ggplot(my_sqr_df, aes(x= x)) +stat_function(fun = my_sqr, geom = "line")

#常用自訂元素
ggplot(cars, aes(x=speed, y= dist))+ 
  geom_point() +
  ggtitle("Car speed vs breaking distance") +
  xlab("car speed (mph") +
  ylab("Breakiing distance (ft)")


#隱藏格線: 使用theme()進行格線隱藏設定
#panel.grid.major = element_blank() 隱藏主要格線
#panel.grid.minor = element_blank() 隱藏次要格線
#panel.grid.major.x = element_blank() 隱藏X軸主要格線
#panel.grid.major.y = element_blank() 隱藏Y軸主要格線
#panel.grid.minor.x = element_blank() 隱藏X軸次要格線
#panel.grid.minor.y = element_blank() 隱藏Y軸次要格線

ggplot(cars, aes(x= speed, y= dist)) +geom_point()+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

 

提供R script參考

參考書籍: 輕鬆學習R語言:從基礎到應用,掌握資料科學的關鍵能力

arrow
arrow

    猛犬 發表在 痞客邦 留言(0) 人氣()