顯示具有 ggplot2 標籤的文章。 顯示所有文章
顯示具有 ggplot2 標籤的文章。 顯示所有文章

2023年3月30日 星期四

RWEPA | shiny企業實務應用 第6集-小明算命師(下 ) 【附小明算命師實作成果、投影片與中文字幕】

第6集-小明算命師(下 ) - 第1季完結篇


提供簡單易懂 R shiny 套件教學, 建立互動式網頁服務與企業級儀表板.

本集包括以下特色:

  1. 機器學習預測, 使用 caret 套件
  2. 網頁檔案下載技巧 (Generating downloadable reports from shiny)
  3. WORD檔案下載技巧 (Generating downloadable reports from shiny)


謝謝各位大大點閱,歡迎分享、按訂閱、讚、開啟小鈴鐺。】






主題: 

6.1 shiny小明算命師-六大特性

    🔎整合敘述統計資料分析

    🔎整合推論統計資料分析

    🔎整合機器學習進行人力資源預測

    🔎下載客製化HTML檔案

    🔎下載客製化WORD檔案

    🔎輕鬆更換為企業人力資源料集

6.2 下載檔案與shiny架構

6.3 ui設定

6.4 server設定

6.5 Rmd設定

6.6 結論





# RWEPA | R - shiny企業實務應用 第1集-白話shiny

# RWEPA | shiny企業實務應用 第2集-下拉式選單【附習題、投影片與中文字幕】

# RWEPA | shiny企業實務應用 第3集-佈置【附54萬筆銷售資料習題、投影片與中文字幕】

# RWEPA | 第4集-shiny銷售儀表板【附小明算命師習題、投影片與中文字幕】

# RWEPA | 第5集- shiny小明算命師(上)【附小明算命師實作習題、投影片與中文字幕】

# RWEPA | shiny企業實務應用 第6集-小明算命師(下 ) 中文字幕,包括網頁與WORD檔案下載技巧 (Generating downloadable reports from shiny)

#shiny教學
#shinydashboard
#dplyr
#ggplot2

# end

2023年2月24日 星期五

RWEPA | shiny企業實務應用 第5集-小明算命師(上)【附小明算命師實作習題、投影片與中文字幕】

提供簡單易懂 R shiny 套件教學, 建立互動式網頁服務與企業級儀表板.



【記得按 訂閱、讚、開啟小鈴鐺】
















主題: 

5.1 套件總覽

5.2 Excel檔案匯入

5.3 資料轉換與視覺化

5.4 建立預測模型

5.5 習題-R與小明算命師-shiny實作篇

5.6 結論




# end

2023年2月12日 星期日

RWEPA | shiny企業實務應用 第4集-shiny銷售儀表板【附小明算命師習題、投影片與中文字幕】

提供簡單易懂 R shiny 套件教學, 建立互動式網頁服務與企業級儀表板.



【記得按 訂閱、讚、開啟小鈴鐺】






主題: 

4.1 範例_runExample(“04_mpg”)

4.2 範例_runExample(“05_sliders”)

4.3 銷售儀表板-手繪版

4.4 銷售儀表板-完成版

4.5 習題-R與小明算命師

4.6 結論






# end

2021年4月12日 星期一

2021-R軟體與Shiny Web應用程式設計


課程目標:

以R語言做為Web服務平台,使學員熟悉使用shiny套件建立Web化服務應用。

Shiny套件的主要功能 (revised from: Hadley Wickham, 2021)

1. 建立企業級儀表板

2. 互動式篩選資料分析

3. 使用視覺化與互動式溝通

4. 建立自我資料分析的工作流程

5. 建立互動式統計教學與資料科學概念,調校參數,理解結果之改變.

6. 提供決策者探索式資料分析等企業智慧計算應用.


課程大綱:

1.基礎shiny程式設計

1.1 shiny套件簡介

1.2 建立第一個shiny網頁程式

1.3 資料物件處理(tidyr, dplyr, stringr套件)

1.4 輸入控制項與檔案上傳

1.5 輸出控制項(文字,表報,圖形ggplot2, leaflet)

1.6 案例示範與操作練習(一)

2.進階shiny程式設計

2.1 版面配置

2.2 HTML UI

2.3 反應型函數(Reactive function)

2.4 shape檔案的輸入與處理

2.5 地理資料-shiny進階網頁程式

2.6 案例示範與操作練習(二)

上課日期:

2021年5月8,9日

報名網頁:

https://www.iiiedu.org.tw/courses/msa331t2101/

2021/5/3前報名即可享優惠價!

#shiny

#visualization

#training

#ggplot2

#leaflet

2020年9月26日 星期六

ggplot2-新增數學式-以迴歸分析模型為例 (regression line equation)

ggplot2-regression line equation



感謝Joey提供此問題, 本篇文章說明 ggplot2 繪圖新增數學式-以迴歸分析模型為例 (regression line equation)

主題

1. 使用 group_by 與 do 建立迴歸分析
2. 方法1:使用文字型態建立註釋(annotation)
3. 方法2:使用 expression 建立註釋(annotation),加上 parse = TRUE
4. 使用 annotate 新增迴歸分析數學式標示
5. 使用 ggtitle 建立標題
6. 使用 element_text(hjust = 0.5) 設定標題置中排列

關鍵字

# group_by
# do
# paste0
# round
# ifelse
# geom_point
# geom_smooth
# annotate
# ggtitle
# theme

套件

1. ggplot2 - 使用 ggplot2 套件繪圖
2. dplyr - 使用 %>% 與 group_by 進行資料處理

R程式碼下載


R程式碼


# title        : ggplot2-新增數學式-以迴歸分析模型為例
# author    : Ming-Chang Lee
# email     : alan9956@gmail.com
# RWEPA : http://rwepa.blogspot.tw/
# GitHub  : https://github.com/rwepa
# resource : https://rwepa.blogspot.com/2020/09/ggplot2-equation.html

library(ggplot2)
library(dplyr)

head(iris)

# 計算群組lm
fitted_models <- iris %>%
  group_by(Species) %>% 
  do(model = summary(lm(Petal.Length ~ Petal.Width, data = .)))

# levels:取出Species欄位的所有可能等級
names(fitted_models$model) <- levels(iris$Species)

# 檢視成果
fitted_models

# 查看群組lm結果(全部)
fitted_models$model

# 查看群組lm結果(setosa)
fitted_models$model$setosa

# 方法1:使用文字型態建立註釋(annotation)
mylabel <- c()
for (i in 1:length(fitted_models$model)) {
  mylabel <- c(mylabel, paste0(names(fitted_models$model[i]), ': Petal.Length = ',
                               round(fitted_models$model[[i]]$coefficients[1], 2), " ",
                               ifelse(fitted_models$model[[i]]$coefficients[2] >= 0, '+ ', ''),
                               round(fitted_models$model[[i]]$coefficients[2], 2), ' * Petal.Width,',
                               ' R2 = ', round(fitted_models$model[[i]]$r.squared, 2)))
}
mylabel
gg_color_hue <- function(n) {
  hues = seq(15, 375, length = n + 1) # seq(0, 360, ...)
  hcl(h = hues, l = 65, c = 100)[1:n]
}
# 繪製群組迴歸模型
p <- ggplot(iris, aes(Petal.Width, Petal.Length, group=Species)) +
  geom_point(aes(color=Species), size=2) +
  geom_smooth(aes(color=Species), method=lm, se=FALSE) +
  annotate('text', label = mylabel, x = 0.7, y = c(2.5, 2, 1.5), size = 4, hjust = 0, color = gg_color_hue(n = 3)) +
  ggtitle("iris群組線性模型統計圖") +
  theme(plot.title = element_text(hjust = 0.5)) # 設定標題置中排列
p

# 方法2:使用expression建立註釋(annotation),加上 parse = TRUE
mylabel <- c()
for (i in 1:length(fitted_models$model)) {
  mylabel <- c(mylabel, paste0(names(fitted_models$model[i]), ': ', 'Petal.Length == ',
                               round(fitted_models$model[[i]]$coefficients[1], 2), " ",
                               ifelse(fitted_models$model[[i]]$coefficients[2] >= 0, '+ ', ''),
                               round(fitted_models$model[[i]]$coefficients[2], 2), ' * Petal.Width ', '~', 
                               'R^{2} == ', round(fitted_models$model[[i]]$r.squared, 2)))
}
mylabel
p <- ggplot(iris, aes(Petal.Width, Petal.Length, group=Species)) +
  geom_point(aes(color=Species), size=2) +
  geom_smooth(aes(color=Species), method=lm, se=FALSE) +
  annotate('text', label = mylabel, x = 0.7, y = c(2.5, 2, 1.5), size = 4, hjust = 0, color = gg_color_hue(n = 3), parse = TRUE) +
  ggtitle("iris群組線性模型統計圖-使用 parse參數") +
  theme(plot.title = element_text(hjust = 0.5))
p
# end

2020年9月20日 星期日

ggplot2 套件 - hcl 客製化繪圖顏色 (customized color)










# ggplot2
# scale
# hcl
# ggtitle
# theme

本篇文章說明 ggplot2 繪圖的顏色主題,內容包括:
1. 使用標準單一顏色.
2. 使用ggplot2內建群組顏色.
3. 使用scales套件,檢視繪圖資訊.
4. 使用客製化 hcl {grDevices}

套件:
1. ggplot2 - 使用ggplot2套件繪圖
2. scales - 擷取ggplot2套件的繪圖資訊
3. gridExtra - 進行多列多行 ggplot2 繪圖


R程式碼下載:

R程式碼:
library(ggplot2)

# 方法1 使用標準單一顏色
ggplot(iris, aes(Petal.Width, Petal.Length)) +
  geom_point(size=2) +
  ggtitle("圖1 ggplot2-使用標準單一顏色") +
  theme(plot.title = element_text(hjust = 0.5))

圖1 ggplot2-使用標準單一顏色



# 方法2 使用ggplot2內建群組顏色
ggplot(iris, aes(Petal.Width, Petal.Length, color=Species)) +
  geom_point(size=2) +
  ggtitle("圖2 ggplot2-使用內建群組顏色") +
  theme(plot.title = element_text(hjust = 0.5))

圖2 ggplot2-使用內建群組顏色




















# 方法3 使用scales套件,檢視繪圖資訊.
library(scales)

p <- ggplot(iris, aes(Petal.Width, Petal.Length, group=Species)) +
  geom_point(aes(color=Species), size=2)

# ggplot2使用的繪圖顏色
# ggplot_build {scale} 可擷取ggplot2繪圖的相關資訊等
col <- unique(ggplot_build(p)$data[[1]]$colour)

# 顯示ggplot2使用的繪圖顏色
show_col(col) # "#F8766D" "#00BA38" "#619CFF" (紅綠藍)

# 將所有顏色轉換成 factor, 加上 levels 參數, 以免顏色異常.
mycol <- factor(ggplot_build(p)$data[[1]]$colour, 
                levels = c("#F8766D", "#00BA38", "#619CFF"))

# 使用 gridExtra 套件進行多列,多行繪圖, 類似 par(mfrow=c(1,2))功能
library(gridExtra)

p1 <- ggplot(iris, aes(Petal.Width, Petal.Length)) +
  geom_point(aes(color=Species), size=2) +
  ggtitle("圖3 ggplot2-使用預設顏色") +
  theme(plot.title = element_text(hjust = 0.5))

p2 <- ggplot(iris, aes(Petal.Width, Petal.Length)) +
  geom_point(aes(color=mycol), size=2) +
  ggtitle("圖4 ggplot2-使用scales套件(二者相同") +
  theme(plot.title = element_text(hjust = 0.5))

# 設定1列, 2行繪圖
# 右側圖例有改善空間!
grid.arrange(p1, p2, nrow=1, ncol=2)

圖3, 圖4 ggplot2-使用scales套件(二者相同



# 方法4 使用客製化 hcl {grDevices}

# ggplot2 內部使用 HCL 顏色規範,參考孟塞爾顏色系統 (Munsell Color System)


# 色相(hue)指的是色彩的外相,表示在不同波長的光照射下,人眼所感覺不同的顏色.
# 在HSL和HSV色彩空間中, H指的就是色相,以紅色為0度(360度);黃色為60度;綠色為120度;青色為180度;藍色為240度;品紅色為300度.

# hcl(h = 0, c = 35, l = 85, alpha, fixup = TRUE)

# h(hue 色相): The hue of the color specified as an angle in the range [0,360].
# 0 yields red, 120 yields green 240 yields blue, etc.

# c(chroma 色度): The chroma of the color. The upper bound for chroma depends on hue and luminance.

# l(value 明度): A value in the range [0,100] giving the luminance of the colour. For a given combination of hue and chroma, only a subset of this range is possible.

# hue 色相    : 0~360度表示
# chroma 色度 : 中間為0, 向外擴散增加
# value 明度  : 南北上下軸表示明度(value)的深淺, 從全黑(N0)至全灰(N5)到全白(N10)




gg_color_hue <- function(n) {
  hues = seq(15, 375, length = n + 1) # seq(0, 360, ...)
  hcl(h = hues, l = 65, c = 100)[1:n]
}

n <- 3

gg_color_hue(n) # "#F8766D" "#00BA38" "#619CFF"

cols <- factor(rep(gg_color_hue(n), each = 50),
               levels = c("#F8766D", "#00BA38", "#619CFF"),
               labels = c("setosa", "versicolor", "virginica"))

ggplot(iris, aes(Petal.Width, Petal.Length, color=cols)) +
  geom_point(size=2) +
  ggtitle("圖5 ggplot2-使用客製化hcl函數") +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_colour_discrete("Species") +
  theme(legend.title.align=0.5)



# end

2020年2月23日 星期日

R語言機器學習應用班 Machine Learning in R

R語言機器學習應用班

http://www.asia-analytics.com.tw/tw/edu/e-hot-1090407.jsp

四大主軸:

1. R/RStudio操作與資料匯入

2. 資料處理與視覺化應用

3. 機器學習基礎操作

4. 機器學習進階應用


# R
# visualization
# machine learning
# 關鍵八步(匯入、摘要、樞紐、篩選、排序、NA值、轉換、合併)
graphics
# ggplot2
# arules
# party
# randomForest
# caret
@ 無需程式設計基礎
# end

2019年5月6日 星期一

R軟體開放資料應用-高速公路篇

本篇文章介紹R軟體在開放式資料的應用, 資料來源為交通部統計查詢網. 使用高速公路-交通事故資料進行相關分析.
  • 使用 ggplot2 套件進行資料視覺化.
  • 使用 forecast 套件進行 ARIMA 模型自動預測.



# R
# RStudio
# ggplot2 套件
# forecast 套件

演講地點: 育達科技大學資訊管理系所

演講日期: 2019年5月8日(三)

演講大綱:
  1. 資料分析/視覺化/互動式案例
  2. 大數據分析工具與架構
  3. R/RStudio簡介
  4. 資料物件,套件,輔助說明
  5. 開放資料分析-高速公路

演講投影片, R程式, 交通事故.csv 下載:

# end


2017年9月9日 星期六

網路抓取 R CRAN 套件清單, 使用 ggplot2 套件繪圖, 建立第2個y軸座標.

主題: 網路抓取 R CRAN 套件清單, 使用 ggplot2 套件繪圖, 建立第2個y軸座標.
說明:

# ggplot2
# packages list
# XML
# geom_col
# goem_line
# geom_point
# scale_y_continuous


















# 2024.5.27 
# R程式碼更新ggplot2 - dual y axis標題等語法

R程式碼下載 -->


  • [#1-2] 首先載入 XML, ggplot2 套件.
  • [#3] 使用CRAN網站-依日期排列抓取現有1萬多個套件清單,
    例: http://cran.csie.ntu.edu.tw/web/packages/available_packages_by_date.html
  • [#4] 使用 readHTMLTable {XML} 函數以讀取網站中的套件清單表格, 將結果儲存為mydf資料物件.
  • [#7] 使用 trimws {base} 函數以刪除欄位名稱空白字元.
  • [#8] 原匯入第1欄 Date為字串資料型態, 使用 as.Date {base} 轉換為 日期(Date) 資料型態.
  • [#9] 使用 format {base}並取出套件更新年, 使用 table {base} 以計算各年套件個數.
  • [#10] 使用 cumsum {base}計算累計套件數並新增為 AccumulatedPAckages 欄位.
  • [#11] 使用 names {base} 設定前二欄名稱為 Year, Packages.
  • [#14] 使用 geom_col {ggplot2} 繪製"套件數(年)"長條圖, 另可使用 geom_bar {ggplot2} 繪製.
  • [#15-18] 設定主標題, x軸標題, y軸標題. theme {ggplot2} 可設定標題左右置中.
  • [#19] 使用 scale_y_continuous {ggplot2}可在繪圖區之右側建立y軸第2座標軸. 右側y軸對應長條圖的刻度. ggplot2 採用資料轉換概念, 因此左側y軸第1座標軸的刻度,對應至累計套件數, 其中最大值約11405, 右側最大值約4502, 11405/4502=2.5, 考慮以2倍計算, 即將左側刻度除以2, 轉換為右側刻度, 一般使用 trans = ~. /2 表示.
  • [#21] 使用 annotate {ggplot2} 可加上文字標題.
  • [#22-24] 使用 goem_line {ggplot2} 繪製累計套件數線圖.
  • [#25-27] 使用 goem_point {ggplot2} 繪製累計套件數點圖.
# end