[.設定所需的函式庫(libraries)以及載入資料]

#install.packages("ggplot2")
rm()  #清除所有物件 
library(graphics)
library(ggplot2)

data(iris)
data(cars)#內建Cars的資料集,為1920s年 car 車速與煞車距離
setwd("d:/Rdata Practice/R BasicLab") #設定工作區

[第五部份].劃圖

5-1.Simple Plots-Bar plot

x=sample(1:150,50) #從1~150中隨機挑選50個數字
plot(iris[x,5])

y=table(iris[x,5])
barplot(y,horiz=TRUE,las=1)

5-2.Simple Plots-Pie

pie(y)

5-3.Simple Plots-Scatter Plot

plot(iris[,3:4])

plot(Petal.Width~Petal.Length,data=iris)

plot(iris[,1:3])
plot(~Sepal.Length+Sepal.Width+Petal.Length,data=iris)

5-4.Simple Plots-Box plot

plot(iris[,5],iris[,1])

boxplot(iris[,1]~iris[,5])

boxplot(Sepal.Length~Species,data=iris)

boxplot(iris[,1:2])

5-5.Simple Plots-Histgram

hist(iris[, 1], breaks = 4)

5-6.如果我想要畫很多張圖在一個頁面上

par(mfrow=c(2,2))
plot(iris[x,5])
plot(Petal.Width~Petal.Length,data=iris)
boxplot(Sepal.Length~Species,data=iris)
hist(iris[, 1], breaks = 4)

dev.off()#把圖型清除
## null device 
##           1

[第六部份].R-Markdown Demo

R Markdown 文件的常用語法

6-1. 字型大小:

管理科學

管理科學

管理科學

管理科學

管理科學
管理科學

6-2. 文句強調:

斜體 and 粗體

6-3. 條列語句:

你覺得寫程式___: - 1. 令人開心 - 2. 覺得沮喪 - 3. 超不爽 - 4. 輕鬆愉快

6-4. 表格:

第一組 第二組 第三組 第四組 第五組
樣本1 樣本2 樣本3 樣本4 樣本5
樣本6 樣本7 樣本8 樣本9 樣本10

6-5. 上標:

單位:km2

6-6. 分隔線:


6-7. 插入 R Code Chuncks:

  • 設定所需的函式庫(libraries)以及指定載入資料的變數
rm(list=ls())#清除所有物件

#data(cars)#內建Cars的資料集,為1920s年 car 車速與煞車距離
head(cars)
##   speed dist
## 1     4    2
## 2     4   10
## 3     7    4
## 4     7   22
## 5     8   16
## 6     9   10
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
str(cars)
## 'data.frame':    50 obs. of  2 variables:
##  $ speed: num  4 4 7 7 8 9 10 10 10 11 ...
##  $ dist : num  2 10 4 22 16 10 18 26 34 17 ...
plot(dist ~ speed, data=cars)
grid() #Add dotted lines to the plot to form a background grid
abline(a=-17.6, b=3.9, col="red") #增加趨勢線(a:intercept, b:slope)
title("1920s car stopping distances (from the 'cars' dataset)")