แนวปฏิบัติที่ดี: Bar plots

การสร้างภาพข้อมูลระดับกลางด้วย ggplot2

Rick Scavetta

Founder, Scavetta Academy

ในบทนี้

  • ข้อผิดพลาดที่พบบ่อยใน Data Viz
  • วิธีนำเสนอข้อมูลที่ดีที่สุด
    • สำหรับกราฟเพื่อการสื่อสาร (Explanatory) และ
    • สำหรับกราฟเพื่อการสำรวจข้อมูล (Exploratory)
การสร้างภาพข้อมูลระดับกลางด้วย ggplot2

Bar plots

  • 2 ประเภท
    • ค่าสัมบูรณ์
    • การกระจาย
การสร้างภาพข้อมูลระดับกลางด้วย ggplot2

การนอนหลับของสัตว์เลี้ยงลูกด้วยนม

Observations: 76
Variables: 3
$ vore  <chr> "carni", "omni", "herbi", "omni", "herbi", "herbi", "carni", …
$ total <dbl> 12.1, 17.0, 14.4, 14.9, 4.0, 14.4, 8.7, 10.1, 3.0, 5.3, 9.4, …
$ rem   <dbl> NA, 1.8, 2.4, 2.3, 0.7, 2.2, 1.4, 2.9, NA, 0.6, 0.8, 0.7, 1.5…
การสร้างภาพข้อมูลระดับกลางด้วย ggplot2

Dynamite plot

d <- ggplot(sleep, aes(vore, total)) +
 # ...

d + 
  stat_summary(fun = mean,
               geom = "bar",
               fill = "grey50") +
  stat_summary(fun.data = mean_sdl,
               fun.args = list(mult = 1),
               geom = "errorbar", 
               width = 0.2)

การสร้างภาพข้อมูลระดับกลางด้วย ggplot2

จุดข้อมูลแต่ละจุด

# position
posn_j <- position_jitter(width = 0.2)

# plot
d +
  geom_point(alpha = 0.6, 
             position = posn_j)

การสร้างภาพข้อมูลระดับกลางด้วย ggplot2

geom_errorbar()

d +
  geom_point(...) +
  stat_summary(fun = mean, 
               geom = "point", 
               fill = "red") +
  stat_summary(fun.data = mean_sdl, 
               fun.args = list(mult = 1), 
               geom = "errorbar", 
               width = 0.2, 
               color = "red")

การสร้างภาพข้อมูลระดับกลางด้วย ggplot2

geom_pointrange()

d +
  geom_point(...) +
  stat_summary(fun.data = mean_sdl, 
               mult = 1, 
               width = 0.2, 
               color = "red")

การสร้างภาพข้อมูลระดับกลางด้วย ggplot2

ไม่แสดงจุดข้อมูล

d +
  stat_summary(fun = mean, 
               geom = "point") +
  stat_summary(fun.data = mean_sdl, 
               fun.args = list(mult = 1), 
               geom = "errorbar", 
               width = 0.2)

การสร้างภาพข้อมูลระดับกลางด้วย ggplot2

ไม่จำเป็นต้องใช้แท่ง

การสร้างภาพข้อมูลระดับกลางด้วย ggplot2

พร้อมทำแบบฝึกหัดแล้ว!

การสร้างภาพข้อมูลระดับกลางด้วย ggplot2

Preparing Video For Download...