การปรับแต่ง Aesthetics

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

Rick Scavetta

Founder, Scavetta Academy

Positions

การปรับสำหรับข้อมูลที่ซ้อนทับกัน

  • identity
  • dodge
  • stack
  • fill
  • jitter
  • jitterdodge
  • nudge
การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

position = "identity" (ค่าเริ่มต้น)

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) + 
  geom_point()

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

position = "identity" (ค่าเริ่มต้น)

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "identity")

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

position = "jitter"

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter")

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

position_jitter()

posn_j <- position_jitter(0.1)

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 col = Species)) +
  geom_point(position = posn_j)

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

position_jitter()

posn_j <- position_jitter(0.1, 
                          seed = 136)

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = posn_j)
  • กำหนดอาร์กิวเมนต์สำหรับตำแหน่ง
  • ความสอดคล้องกันระหว่างกราฟและ layer

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

ฟังก์ชัน Scale

  • scale_x_*()
  • scale_y_*()
  • scale_color_*()
    • หรือ scale_colour_*()
  • scale_fill_*()
  • scale_shape_*()
  • scale_linetype_*()
  • scale_size_*()
การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

ฟังก์ชัน Scale

  • scale_x_continuous()
  • scale_y_*()
  • scale_color_discrete()
    • หรือ scale_colour_*()
  • scale_fill_*()
  • scale_shape_*()
  • scale_linetype_*()
  • scale_size_*()
การแสดงผลข้อมูลด้วย ggplot2 เบื้องต้น

scale_*_*()

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter") +
  scale_x_continuous("Sepal Length") +
  scale_color_discrete("Species")

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

อาร์กิวเมนต์ limits

ggplot(iris, aes(x = Sepal.Length, 
                  y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter") +
  scale_x_continuous("Sepal Length", 
                     limits = c(2,8)) +
  scale_color_discrete("Species")

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

อาร์กิวเมนต์ breaks

ggplot(iris, aes(x = Sepal.Length, 
                  y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter") +
  scale_x_continuous("Sepal Length", 
                     limits = c(2, 8), 
                     breaks = seq(2, 8, 3)) +
  scale_color_discrete("Species")

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

อาร์กิวเมนต์ expand

ggplot(iris, aes(x = Sepal.Length, 
                  y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter") +
  scale_x_continuous("Sepal Length", 
                     limits = c(2, 8), 
                     breaks = seq(2, 8, 3),
                     expand = c(0, 0)) +
  scale_color_discrete("Species")

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

อาร์กิวเมนต์ labels

ggplot(iris, aes(x = Sepal.Length, 
                  y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter") +
  scale_x_continuous("Sepal Length", 
                     limits = c(2, 8), 
                     breaks = seq(2, 8, 3),
                     expand = c(0, 0), 
                     labels = c("Setosa", 
                               "Versicolor", 
                               "Virginica")) +
  scale_color_discrete("Species")

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

labs()

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) +
  geom_point(position = "jitter") +
  labs(x = "Sepal Length", 
       y = "Sepal Width", 
       color = "Species")

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

มาลองใช้กันเลย!

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

Preparing Video For Download...