การดึงข้อมูล sample_price.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
การ Plot กราฟตามชื่อ Column ที่เราต้องการ โดยใช้คำสั่ง Plot(X,Y) เราสามารถใส่ชื่อ Column ที่เราต้องการลงใน X,Y ได้
attach(df)
plot(LotFrontage,SalePrice)
เราจะใช้คำสั่ง col ในการเปลี่ยนสี โดยชื่อสีเราสามารถดูจากอินเตอร์เน็ตได้เลยหรือเราจะใช้เป็นโค้ดสีก็ได้เช่นกัน
plot(LotFrontage,SalePrice , col = c("pink", "black"))
เราสามารถค้นหาลักษณะจุดบนกราฟที่เราต้องการได้ตามอินเตอร์เน็ตเลย
plot(LotFrontage,SalePrice , col = c("pink", "black"),pch=8)
โดยจะใช้คำสั่ง cex.lab
plot(LotFrontage,SalePrice , col = c("pink", "black"),cex.lab=1.5)
ใช้คำสั่ง cex.axis
plot(LotFrontage,SalePrice , col = c("pink", "black"),cex.lab=1.5,cex.axis=1.1)
plot(LotFrontage,SalePrice , col = c("pink", "black"),cex.lab=1.5,cex.axis=1.1,cex=1.5)
plot(LotFrontage,SalePrice , col = c("pink", "black"),cex.lab=1.5,cex.axis=1.1,cex=1.5,
xlab = "Lot frontage",ylab = "Lot area")
ก่อนการสร้างกราฟแท่งเราต้องเริ่มจากการสร้างตารางการแจกแจงความถี่ก่อน โดยใช้คำสั่ง table(X,Y) เราจะใส่ชื่อ Column ที่เราต้องการลงใน X,Y
names(data)
## NULL
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("111","312","410"))
การสร้าง barplot ในตอนแรกจะทำให้เราอ่านกราฟได้ยาก เราจึงใช้คำสั่ง beside ในการแยกกราฟแท่งออกมาให้มองง่ายขึ้น
barplot(table(MSZoning,MSSubClass),col = c("111","312","410"),beside = TRUE)
คำสั่งนี้จะทำให้เราเข้าใจว่ากราฟแต่ละแท่งหมายถึงอะไร
barplot(table(MSZoning,MSSubClass),col = c("111","312","410"),beside = TRUE,legend.text = TRUE)
barplot(table(MSZoning,MSSubClass),col = c("111","312","410"),beside = TRUE,legend.text = TRUE,
xlab = "The building class")
โดยจะใช้คำสั่ง mfrow = c(num of row, num of col)
par(mfrow = c(1,2))
plot(LotFrontage,SalePrice,col="pink",pch=8)
plot(LotArea,SalePrice,col="black",pch=8)
par(mfrow = c(1,2))
boxplot(LotFrontage,col = "pink",pch=8)
boxplot(LotArea,col = "pink",pch=8)
par(mfrow = c(1,2))
boxplot(LotFrontage,col = "pink",horizontal = TRUE)
boxplot(LotArea,col = "pink",horizontal = TRUE)
ก่อนสร้างกราฟวงกลม เราต้องทำข้อมูลให้เป็นตารางก่อน
t = table(MSZoning)
t
## MSZoning
## FV RL RM
## 40 177 5
เราจะใช้คำสั่ง pie ในการสร้างกราฟ
pie(t)
t = table(MSZoning)
t
## MSZoning
## FV RL RM
## 40 177 5
pie(t, main = "MSZoning")
เราจะคิดเปอร์เซ็นต์ของข้อมูลแต่ละตัวออกมาก่อน
rownames(t)
## [1] "FV" "RL" "RM"
lab = sprintf("%s (%.2f%%)", rownames(t),t/sum(t)*100)
lab
## [1] "FV (18.02%)" "RL (79.73%)" "RM (2.25%)"
ใช้คำสั่ง labels ในการโชว์เปอร์เซ็นต์บนกราฟวงกลม
pie(t, main = "MSZoning", labels = lab)
### การเปลี่ยนสีในกราฟวงกลม เราจะใช้คำสั่ง col ในการเปลี่ยนสี
โดยชื่อสีสามารถค้นหาได้มาอินเตอร์เน็ตหรือใส่เป็นโค้ดสีก็ได้เช่นกัน
pie(t, main = "MSZoning", labels = sprintf("%s (%.2f%%)", rownames(t),t / sum(t)*100),col = c("black","pink","lightblue"))
เราจะรวบรวมข้อมูลตัวเลขที่ต้องการสร้างกราฟฮิสโตแกรมก่อน จากนั้นใช้คำสั่ง hist ในการสร้างกราฟ
a = c(50,40,53,45,42,51,60,63,39,63,41,48,90)
hist(a)
hist(a, main = "Weight distribution")
ทำให้เราเข้าใจความหมายของกราฟมากขึ้น ว่ากราฟที่เราสร้างเก็บข้อมูลแกน X เป็นอะไร
hist(a, main = "Weight distribution",xlab = "Weight")
เราจะใช้คำสั่ง col ในการเปลี่ยนสี โดยชื่อสีเราสามารถค้นหาไปตามอินเตอร์เน็ตหรือเราจะใส่เป็นโค้ดสีก็ได้เช่นกัน
hist(a, main = "Weight distribution",xlab = "Weight",col = "Pink")
เราจะใช้คำสั่ง border
hist(a, main = "Weight distribution",xlab = "Weight",col = "Pink",border = "Blue")