กราฟเส้น

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

Rick Scavetta

Founder, Scavetta Academy

ประเภทกราฟที่ใช้บ่อย

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

Beaver

str(beaver)
'data.frame':    101 obs. of  3 variables:
 $ time  : POSIXct, format: "2000-01-01 09:30:00" "2000-01-01 09:40:00" "2000-01-01 09:50:00" ...
 $ temp  : num  36.6 36.7 36.9 37.1 37.2 ...
 $ active: Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

Beaver

ggplot(beaver, aes(x = time, y = temp)) +
  geom_line()

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

Beaver

ggplot(beaver, aes(x = time, y = temp, 
                   color = factor(active))
                   ) +
  geom_line()

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

ชุดข้อมูลการจับปลา

str(fish)
'data.frame':    427 obs. of  3 variables:
 $ Species: Factor w/ 7 levels "Pink","Chum",..: 1 2 3 4 5 6 7 1 2 3 ...
 $ Year   : int  1950 1950 1950 1950 1950 1950 1950 1951 1951 1951 ...
 $ Capture: int  100600 139300 64100 30500 0 23200 10800 259000 155900 51200 ...
การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

Aesthetic linetype

ggplot(fish, aes(x = Year, 
                 y = Capture, 
                 linetype = Species)) + 
  geom_line()

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

Aesthetic size

ggplot(fish, aes(x = Year, 
                 y = Capture, 
                 size = Species)) + 
  geom_line()

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

Aesthetic color

ggplot(fish, aes(x = Year, 
                 y = Capture, 
                 color = Species)) + 
  geom_line()

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

Aesthetic สำหรับตัวแปรเชิงหมวดหมู่

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

Aesthetic fill กับ geom_area()

ggplot(fish, aes(x = Year, 
                 y = Capture, 
                 fill = Species)) + 
  geom_area()

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

การใช้ position = "fill"

ggplot(fish, aes(x = Year, 
                 y = Capture, 
                 fill = Species)) + 
  geom_area(position = "fill")

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

geom_ribbon()

ggplot(fish, aes(x = Year, 
                 y = Capture, 
                 fill = Species)) + 
  geom_ribbon(aes(ymax = Capture, 
                  ymin = 0), 
              alpha = 0.3)

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

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

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

Preparing Video For Download...