การดึงข้อมูล

ใส่ชาร์ปเพื่อให้เป็นหัวข้อ การดึงข้อมูล sample_hoiseprice.csv เข้ามาในโปรแกรม R

df = read.csv("sample_houseprice.csv")
head(df)
##   Id MSSubClass MSZoning LotFrontage LotArea Street YearBuilt SalePrice
## 1 12         60       RL          85   11924   Pave      2005    345000
## 2 14         20       RL          91   10652   Pave      2006    279500
## 3 21         60       RL         101   14215   Pave      2005    325300
## 4 26         20       RL         110   14230   Pave      2007    256300
## 5 28         20       RL          98   11478   Pave      2007    306000
## 6 33         20       RL          85   11049   Pave      2007    179900
attach(df)

การสร้าง scatter plot เบื้องต้น

plot(LotFrontage,SalePrice)

วิธีเปลี่ยนสีกราฟ

สามารถดูสีจากในอินเตอร์เน็ตได้เลย จะใส่เป็นโค้ดสีหรือเป็นชื่อสีก็ได้

plot(LotFrontage,SalePrice , col = c("pink", "lightblue"))

วิธีขายจุดกราฟให้ใหญ่ขึ้น

ขยายจุดในกราฟให้ใหญ่ขึ้นโดยใช้คำสั่ง cex

plot(LotFrontage,SalePrice , col = c("pink", "lightblue"), cex = 1.5)

วิธีการเปลี่ยน symbols

ถ้าอยากทำให้กราฟดูน่ารักขึ้น เราสามารถเปลี่ยนลักษณะ symbols ของกราฟได้โดยใช้คำสั่ง pch

plot(LotFrontage,SalePrice , col = c("pink", "lightblue"), cex = 1.5, pch = 8)

วิธีการเปลี่ยนขนาดชื่อของแกน X และแกน Y

plot(LotFrontage,SalePrice , col = c("pink", "lightblue"), cex = 1.5, pch = 8, cex.lab = 1.5)

วิธีการเปลี่ยนชื่อของแกน X และแกน Y

plot(LotFrontage,SalePrice , col = c("pink", "lightblue"), cex = 1.5, pch = 8, cex.lab = 1.5, xlab = "Lot Frontage", ylab = "Sale Price")

เพิ่มขนาดตัวอักษรตรงตัวเลขในแกนx,y

ใช้คำสั่ง cex.axis

plot(LotFrontage,SalePrice , col = c("pink", "lightblue"), cex = 1.5, pch = 8, cex.lab = 1.5, xlab = "Lot Frontage", ylab = "Sale Price", cex.axis = 1.2)

หรือถ้ากราฟแบบนี้ดูยากไป อยากได้เป็นกราฟแท่งเปลี่ยนจาก plot เป็น barplot แต่การจะสร้างได้ต้องเริ่มจากการสร้างตารางแจกแจงความถี่ก่อน

การสร้างกราฟแบบแท่ง

table(MSZoning,MSSubClass)
##         MSSubClass
## MSZoning 20 60 80 120 160 180
##       FV 12 17  0   3   8   0
##       RL 95 52  2  28   0   0
##       RM  0  0  0   2   0   3
barplot(table(MSZoning,MSSubClass), col = c("lightblue","pink", "darkseagreen"))

ปรับให้เป็นกราฟแท่งปกติด้วย beside

barplot(table(MSZoning,MSSubClass), col = c("lightblue","pink","darkseagreen"), beside = TRUE)

เพิ่มคำอธิบายกราฟ โดยใช้คำสั่ง legend.text = TRUE

barplot(table(MSZoning,MSSubClass), col = c("lightblue","pink","darkseagreen"), beside = TRUE, legend.text = TRUE)

การสร้างกราฟ 2 คอลัม 1 แถวในหน้าเดียวกัน

mfrow = c(num of row, num of col)

par(mfrow = c(1,2))
plot(LotFrontage,SalePrice, col = c("pink","lightblue"), cex = 1.5)
plot(LotArea,SalePrice, col = c("pink","lightblue"), cex = 1.5)

การสร้าง boxplot แนวตั้ง

par(mfrow = c(1,2))
boxplot(LotFrontage,SalePrice, col = c("pink","lightblue"), cex = 1.5)
boxplot(LotArea,SalePrice, col = c("pink","lightblue"), cex = 1.5)

การสร้าง boxplot แนวนอน

par(mfrow = c(1,2))
boxplot(LotFrontage,SalePrice, col = c("pink","lightblue"), cex = 1.5,horizontal = TRUE)
boxplot(LotArea,SalePrice, col = c("pink","lightblue"), cex = 1.5,horizontal = TRUE)