ฮิสโตแกรม

การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

Rick Scavetta

Founder, Scavetta Academy

ประเภทกราฟที่พบบ่อย

ประเภทกราฟ Geoms ที่ใช้ได้
Scatter plots points, jitter, abline, smooth, count
Bar plots histogram, bar, col, errorbar
Line plots line, path
การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

ฮิสโตแกรม

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram()
  • กราฟแสดงค่าที่แบ่งเป็น bin
    • กล่าวคือ เป็นฟังก์ชันทางสถิติ
`stat_bin()` using `bins = 30`.
Pick better value with `binwidth`.

การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

ค่าเริ่มต้น 30 bin เท่ากัน

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram()
  • กราฟแสดงค่าที่แบ่งเป็น bin
    • กล่าวคือ เป็นฟังก์ชันทางสถิติ
# Default bin width:
diff(range(iris$Sepal.Width))/30
[1] 0.08

การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

ความกว้าง bin ที่สื่อความหมาย

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram(binwidth = 0.1)
  • ควรกำหนดความกว้าง bin ให้เหมาะสมกับข้อมูลเสมอ

  • ไม่มีช่องว่างระหว่างแท่ง

การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

ปรับตำแหน่ง tick marks

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram(binwidth = 0.1,
                 center = 0.05)
  • ควรกำหนดความกว้าง bin ให้เหมาะสมกับข้อมูลเสมอ

  • ไม่มีช่องว่างระหว่างแท่ง

  • ป้ายกำกับแกน X อยู่ระหว่างแท่ง

การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

แยกตามสายพันธุ์

ggplot(iris, aes(x = Sepal.Width, 
                 fill = Species)) + 
  geom_histogram(binwidth = .1, 
                 center = 0.05)

การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

ค่าเริ่มต้นของ position คือ "stack"

ggplot(iris, aes(x = Sepal.Width,
                 fill = Species)) + 
  geom_histogram(binwidth = .1, 
                 center = 0.05,
                 position = "stack") 

การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

position = "dodge"

ggplot(iris, aes(x = Sepal.Width, 
                 fill = Species)) + 
  geom_histogram(binwidth = .1, 
                 center = 0.05, 
                 position = "dodge")

การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

position = "fill"

ggplot(iris, aes(x = Sepal.Width, 
                 fill = Species)) + 
  geom_histogram(binwidth = .1, 
                 center = 0.05, 
                 position = "fill")  

การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

มาฝึกกันเถอะ!

การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

Preparing Video For Download...