Vẽ dữ liệu trong DataFrame

Nhập môn Trực quan hóa Dữ liệu với Julia

Gustavo Vieira Suñe

Data Analyst

Bộ dữ liệu bảo hiểm

  • DataFrame insurance
Age Sex BMI Children Smoker Region Charges
19 female 27.90 0 yes southwest 16884.90
18 male 33.77 1 no southeast 1725.55
28 male 33.00 3 no southeast 4449.46
... ... ... ... ... ... ...

 

  • DataFrame linh hoạt, hiệu quả cho dữ liệu dạng bảng
    • StatsPlots có recipe để vẽ dữ liệu trong DataFrame
    • Giới thiệu ký hiệu @df!
Nhập môn Trực quan hóa Dữ liệu với Julia

Trích xuất mảng từ DataFrame

  • Phí trung bình theo vùng và trạng thái hút thuốc
# Nhóm theo vùng và hút thuốc
grouped = groupby(insurance, [:Region, :Smoker])

# Tính phí trung bình grouped_mean_charges = combine(grouped, :Charges => mean)
  • Mỗi cột là một mảng dữ liệu
    • Ví dụ, grouped_mean_charges.Region trả về mảng chuỗi tên vùng.
Nhập môn Trực quan hóa Dữ liệu với Julia

Vẽ dữ liệu từ mảng

# Biểu đồ cột nhóm
groupedbar(

# Truyền mảng làm đối số grouped_mean_charges.Region, grouped_mean_charges.Charges_mean, group=grouped_mean_charges.Smoker,
color=[:teal :orangered2], linewidth=0, legend_title="Smoker", legend_position=:outertopright) xlabel!("Region") ylabel!("Insurance Premium (USD)")

Biểu đồ cột nhóm hiển thị phí bảo hiểm theo vùng, nhóm theo trạng thái hút thuốc.

Nhập môn Trực quan hóa Dữ liệu với Julia

Vẽ trực tiếp từ DataFrame

# Vẽ từ DataFrame
@df grouped_mean_charges groupedbar(

# Truyền tên cột :Region, :Charges_mean,
group=:Smoker,
color=[:teal :orangered2], linewidth=0, legend_title="Smoker", legend_position=:outertopright) xlabel!("Region") ylabel!("Insurance Premium (USD)")

Biểu đồ cột nhóm hiển thị phí bảo hiểm theo vùng, nhóm theo trạng thái hút thuốc.

Nhập môn Trực quan hóa Dữ liệu với Julia

So sánh cạnh nhau

# Biểu đồ cột nhóm
groupedbar(

# Truyền mảng làm đối số grouped_mean_charges.Region, grouped_mean_charges.Charges_mean, group=grouped_mean_charges.Smoker,
color=[:teal :orangered2], linewidth=0, legend_title="Smoker", legend_position=:outertopright) xlabel!("Region") ylabel!("Insurance Premium (USD)")
# Vẽ từ DataFrame
@df grouped_mean_charges groupedbar(

# Truyền tên cột :Region, :Charges_mean, group=:Smoker,
color=[:teal :orangered2], linewidth=0, legend_title="Smoker", legend_position=:outertopright) xlabel!("Region") ylabel!("Insurance Premium (USD)")
Nhập môn Trực quan hóa Dữ liệu với Julia

Chaining lệnh DataFrame

  • Từ trước

    # Nhóm theo vùng và hút thuốc
    grouped = groupby(insurance, [:Region, :Smoker])
    # Tính phí trung bình
    grouped_mean_charges = combine(grouped, :Charges => mean)
    
  • Dùng chaining thay thế

    using Chain
    # Chain groupby và combine
    grouped_mean_charges = @chain insurance begin
      groupby([:Region, :Smoker])
      combine(:Charges => mean)
    end
    
Nhập môn Trực quan hóa Dữ liệu với Julia

Chuỗi vẽ biểu đồ

# Chuỗi vẽ biểu đồ
@chain insurance begin
    # Xử lý dữ liệu
    groupby([:Region, :Smoker])
    combine(:Charges => mean)

# Vẽ dữ liệu @df groupedbar(:Region, :Charges_mean, group=:Smoker, color=[:teal :orangered2], linewidth=0, legend_title="Smoker", legend_position=:outertopright)
end xlabel!("Region") ylabel!("Insurance Premium (USD)")

Biểu đồ cột nhóm hiển thị phí bảo hiểm theo vùng, nhóm theo trạng thái hút thuốc.

Nhập môn Trực quan hóa Dữ liệu với Julia

Ayo berlatih!

Nhập môn Trực quan hóa Dữ liệu với Julia

Preparing Video For Download...