# เรียกใช้ชุดคำสั่งด้วย
library(ggplot2)
# หรือtidyverse ก็ได้
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ tibble  3.1.2     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.1
## ✓ purrr   0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(extrafont)
## Registering fonts with R

ต่อจากตอนที่ 1 จากข้อมูล diamonds

data(diamonds)
str(diamonds)
## tibble [53,940 × 10] (S3: tbl_df/tbl/data.frame)
##  $ carat  : num [1:53940] 0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ...
##  $ cut    : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ...
##  $ color  : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ...
##  $ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ...
##  $ depth  : num [1:53940] 61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ...
##  $ table  : num [1:53940] 55 61 65 58 58 57 57 55 61 61 ...
##  $ price  : int [1:53940] 326 326 327 334 335 336 336 337 337 338 ...
##  $ x      : num [1:53940] 3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ...
##  $ y      : num [1:53940] 3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ...
##  $ z      : num [1:53940] 2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...

การเพิ่มรายละเอียดที่น่าสนใจให้กับกราฟแบบจุด

qplot(data=diamonds,x=carat,y=price,color=clarity)

ถ้าต้องการเพิ่ม เส้นสมการถดถอยเข้าไปสามารถ ทำได้โดยเพิ่มคำสั่ง

qplot(data=diamonds,x=carat,y=price,color=depth,geom=c("point","smooth"))
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'

geom=c(“point”,“smooth”) คือการสร้างกราฟที่เป็นแบบจุดและเพิ่มสมการถดถอยแบบอัตโนมัติโดยเส้นกราฟของช่วงความเชื่อมั่นด้วยถ้าต้องการเลือกใช้สมการถดถอยแบบอื่นๆ สามารถทำได้

qplot(data=diamonds,x=carat,y=price,color=depth,geom=c("point","smooth"),method="lm")
## Warning: Ignoring unknown parameters: method
## `geom_smooth()` using formula 'y ~ x'

method = “lm” ก็คือสมการถดถอยเชิงเส้น ถ้าต้องการสมการถดถอยแบบอื่นๆ เช่น smooth ก็ทำได้โดยเพื่อคำสั่งดังนี้

qplot(data=diamonds,x=carat,y=price,color=depth,
      geom=c("point","smooth"),method="lm",formula= y~poly(x,5))
## Warning: Ignoring unknown parameters: method, formula

สามารถเลือกเป็น y~log(x) หรือสมการรูปแบบอื่นๆ ให้ผู้อ่านทดลองดูด้วยตนเอง

หมายเหตุ poly(x,n) คือสมการโพลีโนเมียลดีกรี n ถ้าต้องการเพียงเส้นสมการถดถอยอย่างเดียวให้คำสั่งดังนี้

qplot(data=diamonds,x=carat,y=price,color = depth,
      geom=c("point","smooth"),
      method="lm",
      formula= y~poly(x,5),
      se=FALSE)
## Warning: Ignoring unknown parameters: method, formula, se

## การปรับแต่งกราฟ

การตั้งชื่อกราฟ

ทำได้โดยใช้คำสั่ง

qplot(data=diamonds,x=carat,y=price,color=depth,
      geom=c("point","smooth"),
      method="lm",
      formula= y~poly(x,5),
      se=FALSE, 
      main="graph")
## Warning: Ignoring unknown parameters: method, formula, se

ลองตั้งชื่อกราฟเป็นภาษาไทย

qplot(data=diamonds,x=carat,y=price,color=depth,
      geom=c("point","smooth"),
      method="lm",
      formula= y~poly(x,5),
      se=FALSE, 
      main="graph น้้ำหนักเพชรกับราคา us$")
## Warning: Ignoring unknown parameters: method, formula, se

จะเห็นว่าไม่สามารถใช้ภาษาไทยได้ ในการใช้ภาษาไทยจริงให้เพิ่มชุดคำสั่ง เพื่อเปลี่ยนฟอนด์ดังนี้

qplot(data=diamonds,x=carat,y=price,color=depth,
      geom=c("point","smooth"),
      method="lm",
      formula= y~poly(x,5),
      se=FALSE, 
      main="graph น้้ำหนักเพชรกับราคา us$")+
      theme(text = element_text(family = "TH Sarabun New"))
## Warning: Ignoring unknown parameters: method, formula, se

ฟ้อนด์ Th Sarabun New เป็นเพียงแค่ตัวอย่าง จริงๆแล้วสามารถเปลี่ยนเป็นฟ้อนด์ใดๆ หรือภาษาใดๆ ก็ได้ ที่เครื่องคอมพิวเตอร์ของท่านมี ในบางกรณีการนำไฟลรูปภาพไปเขียนชื่อของกราฟหรือตกแต่งด้วยโปรแกรมอื่น เช่นโปรแกรมแต่งภาพ หรือโปรแกรมที่เขียนข้อความลงบนไฟล์ pdf ได้อาจจะทำงานได้สะดวกกว่า ก็เป็นได้

การใช้ชุดคำสั่ง LaTeX

ใช้ package latex2exp

library(latex2exp)
qplot(data=diamonds,x=carat,y=price,color=depth,
      geom=c("point","smooth"),
      method="lm",
      formula= y~poly(x,5),se=FALSE, 
      main=TeX('graph น้้ำหนักเพชรกับราคา us: $price=\\alpha_0+\\sum_{i=1}^5\\alpha_i carat^i$'))+
      theme(text = element_text(family = "TH Sarabun New"))
## Warning: Ignoring unknown parameters: method, formula, se

คำสั่ง LaTeX จะมีความแตกต่างจากปกติเพียงเล็กน้อยเท่านั้น สำหรับที่สนใจการใช้งาน LaTeX สามารถศึกษาเพิ่มเติมได้จาก วันชัย ตาบัญโญ

การตั้งชื่อแกน \(x\) และแกน \(y\)

สามารถทำได้โดยการเพิ่มคำสั่งดังนี้

qplot(data=diamonds,x=carat,y=price,color=depth,
      geom=c("point","smooth"),method="lm",formula= y~poly(x,5),se=FALSE, 
      main=TeX('graph น้้ำหนักเพชรกับราคา us: $price =\\alpha_0+\\sum_{i=1}^5\\alpha_i carat^i$'),
  xlab = "น้ำหนักเพชร",   ylab = "ราคาของเพขร")+
      theme(text = element_text(family = "TH Sarabun New"))
## Warning: Ignoring unknown parameters: method, formula, se

คำสั่ง xlab และ ylab คือการตั้งชื่อให้แกน \(x\) และแกน \(y\) ตามลำดับ

การกำหนดของความยาวแของแกน \(x\) และแกน \(y\)

จากรูปจะเห็นว่าเส้นสมการการถดถอยที่ทางซ้ายมือสุดมีค่าสูงมากทำให้กราฟที่ได้อาจจะไม่สวยงาม ถ้าต้องการให้แสดงผลเฉพาะช่วงความยาวที่ต้องการ เช่น แภน \(x\) อยู่ระหว่าง 0 ถึง 5 และแกน \(y\) อยู่ระหว่าง 0 ถึง 20,000 สามารถทำได้โดยใช้คำสั่งเพิ่มเข้าไปดังนี้

qplot(data=diamonds,x=carat,y=price,color=depth,
      geom=c("point","smooth"),method="lm",formula= y~poly(x,5), 
      main=TeX('graph น้้ำหนักเพชรกับราคา us: $price =\\alpha_0+\\sum_{i=1}^5\\alpha_i carat^i$'),
  xlab = "น้ำหนักเพชร",   ylab = "ราคาของเพขร",
  xlim=c(0,5),ylim=c(0,20000))+
      theme(text = element_text(family = "TH Sarabun New"))
## Warning: Ignoring unknown parameters: method, formula
## Warning: Removed 1 rows containing non-finite values (stat_smooth).
## Warning: Removed 1 rows containing missing values (geom_point).
## Warning: Removed 3 rows containing missing values (geom_smooth).

xlim และ ylim เป็นตำสั่งที่ใช้ในการกำหนดช่วงความของแกน ให้มีค่าอยู่ระหว่างค่าต่ำสุดกับค่าสูงสุดเท่าไหร่ c(min,max)

การแยกกราฟออกเป็นกราฟย่อย

ถ้าลองใส่สีโดยใช้ color = clarity ทำเห็นว่า ตัวแปร claity มีกระจายอย่างไร

qplot(data=diamonds,x=carat,y=price,color=clarity)

ถ้าต้องการแยกกราฟนี้ออกเป็นกราฟย่อยโดยใช้ตัวแปรเชิงคุณภาพ สามารถแยกออกได้เป็น 3 แบบคือ ### แยกไปตามจำนวนแถว ถ้าแแบ่งข้อมูลออกด้วย ตัวแปร clarity

qplot(data=diamonds,x=carat,y=price,color=clarity,facets = clarity~.)

### แยกไปตามจำนวนหลัก ถ้าแแบ่งข้อมูลออกด้วย ตัวแปร clarity

qplot(data=diamonds,x=carat,y=price,color=clarity,facets = .~clarity )

## แยกไปท้ังแถวและหลัก จำเป็นต้องใช้ ตัวแปรเชิงปริมาณ 2 ตัว ถ้าเพิ่มตัวแปร cut เข้าไปอีกจะได้ผลดังนี้

qplot(data=diamonds,x=carat,y=price,color=clarity,facets = clarity~cut )

หรือ

qplot(data=diamonds,x=carat,y=price,color=clarity,facets = cut~clarity )

สำหรับชุดคำสั่ง qplot โดยเบื้องต้นเท่านี้ก็น่าจะเพียงพอสำหรับผู้สนใจ ผู้เขียนแนะนำให้ผู้อ่านศึกษาชุดคำสั่ง ggplot2 โดยตรงจะสามารถปรับแต่งกราฟและควบคุมได้มากกว่าว่านี้