R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

par(mfrow = c(1, 2)) # 一列兩圖

成績資料

statistics <- c(68, 85, 74, 88, 63, 78, 90, 80, 58, 63)

math <- c(85, 91, 74, 100, 82, 84, 78, 100, 51, 70)

Statistics 成績直方圖

hist(statistics,

 main = "Statistics 成績直方圖",

 xlab = "成績",

 ylab = "人數",

 col = "skyblue",

 border = "black")

Math 成績直方圖

hist(math,

 main = "Math 成績直方圖",

 xlab = "成績",

 ylab = "人數",

 col = "lightgreen",

 border = "black")

清空畫面設定為 1 列 3 圖

par(mfrow = c(1, 3))

成績資料

statistics <- c(68, 85, 74, 88, 63, 78, 90, 80, 58, 63)

math <- c(85, 91, 74, 100, 82, 84, 78, 100, 51, 70)

Statistics 成績直方圖

hist(statistics,

 main = "Statistics 成績直方圖",

 xlab = "成績",

 ylab = "人數",

 col = "skyblue",

 border = "black")

Math 成績直方圖

hist(math,

 main = "Math 成績直方圖",

 xlab = "成績",

 ylab = "人數",

 col = "lightgreen",

 border = "black")

散佈圖

plot(statistics, math,

 main = "Statistics vs Math 散佈圖",

 xlab = "Statistics 成績",

 ylab = "Math 成績",

 pch = 19,

 col = "darkorange")

清空畫面設定為 2 列 2 圖(方便顯示)

par(mfrow = c(2, 2))

成績資料

statistics <- c(68, 85, 74, 88, 63, 78, 90, 80, 58, 63)

math <- c(85, 91, 74, 100, 82, 84, 78, 100, 51, 70)

students <- paste0(“Student_”, 1:10)

計算總分

total <- statistics + math

Statistics 成績直方圖

hist(statistics,

 main = "Statistics 成績直方圖",

 xlab = "成績",

 ylab = "人數",

 col = "skyblue",

 border = "black")

Math 成績直方圖

hist(math,

 main = "Math 成績直方圖",

 xlab = "成績",

 ylab = "人數",

 col = "lightgreen",

 border = "black")

散佈圖

plot(statistics, math,

 main = "Statistics vs Math 散佈圖",

 xlab = "Statistics 成績",

 ylab = "Math 成績",

 pch = 19,

 col = "darkorange")

圓餅圖

pie(total,

labels = students,

main = "每位學生總分圓餅圖",

col = rainbow(length(total)))

# 清空畫面,設為 2 列 3 圖

par(mfrow = c(2, 3))

成績資料

statistics <- c(68, 85, 74, 88, 63, 78, 90, 80, 58, 63)

math <- c(85, 91, 74, 100, 82, 84, 78, 100, 51, 70)

students <- paste0(“Student_”, 1:10)

總分

total <- statistics + math

Statistics 成績直方圖

hist(statistics,

 main = "Statistics 成績直方圖",

 xlab = "成績",

 ylab = "人數",

 col = "skyblue",

 border = "black")

Math 成績直方圖

hist(math,

 main = "Math 成績直方圖",

 xlab = "成績",

 ylab = "人數",

 col = "lightgreen",

 border = "black")

散佈圖

plot(statistics, math,

 main = "Statistics vs Math 散佈圖",

 xlab = "Statistics 成績",

 ylab = "Math 成績",

 pch = 19,

 col = "darkorange")

圓餅圖

pie(total,

labels = students,

main = "每位學生總分圓餅圖",

col = rainbow(length(total)))

棒棒糖圖(Lollipop Chart)

plot(total,

 type = "n",

 xaxt = "n",

 main = "總分棒棒糖圖",

 xlab = "學生",

 ylab = "總分",

 ylim = c(0, max(total) + 10))

加線段 + 點

segments(x0 = 1:10, y0 = 0, x1 = 1:10, y1 = total, col = “steelblue”, lwd = 2)

points(1:10, total, pch = 16, col = “dark