Biểu đồ tần suất

Nhập môn trực quan hóa dữ liệu với ggplot2

Rick Scavetta

Founder, Scavetta Academy

Các loại biểu đồ thường dùng

Loại biểu đồ Geom khả dụng
Phân tán points, jitter, abline, smooth, count
Cột histogram, bar, col, errorbar
Đường line, path
Nhập môn trực quan hóa dữ liệu với ggplot2

Biểu đồ tần suất

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram()
  • Biểu đồ các giá trị được chia lớp
    • tức là một hàm thống kê
`stat_bin()` using `bins = 30`.
Pick better value with `binwidth`.

Nhập môn trực quan hóa dữ liệu với ggplot2

Mặc định: 30 lớp đều nhau

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram()
  • Biểu đồ các giá trị được chia lớp
    • tức là một hàm thống kê
# Default bin width:
diff(range(iris$Sepal.Width))/30
[1] 0.08

Nhập môn trực quan hóa dữ liệu với ggplot2

Bề rộng lớp trực quan, có ý nghĩa

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram(binwidth = 0.1)
  • Luôn chọn bề rộng lớp phù hợp với dữ liệu.

  • Không có khoảng trống giữa các cột.

Nhập môn trực quan hóa dữ liệu với ggplot2

Đặt lại vị trí vạch chia

ggplot(iris, aes(x = Sepal.Width)) + 
  geom_histogram(binwidth = 0.1,
                 center = 0.05)
  • Luôn chọn bề rộng lớp phù hợp với dữ liệu.

  • Không có khoảng trống giữa các cột.

  • Nhãn trục X nằm giữa các cột.

Nhập môn trực quan hóa dữ liệu với ggplot2

Các loài khác nhau

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

Nhập môn trực quan hóa dữ liệu với ggplot2

Mặc định: "stack"

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

Nhập môn trực quan hóa dữ liệu với ggplot2

position = "dodge"

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

Nhập môn trực quan hóa dữ liệu với ggplot2

position = "fill"

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

Nhập môn trực quan hóa dữ liệu với ggplot2

Trang cuối

Nhập môn trực quan hóa dữ liệu với ggplot2

Preparing Video For Download...