Ggplot2 ရှိ stacked bar chart တစ်ခုတွင် ဘားများကို ပြန်စီနည်း


ggplot2 တွင် stacked bar chart တွင် bars များ၏ အနေအထားကို ပြန်လည်စီစဉ်ရန် အောက်ပါအခြေခံ syntax ကို အသုံးပြုနိုင်ပါသည်။

 #specify order of bars (from top to bottom)
df$fill_var <- factor(df$fill_var, levels=c(' value1 ', ' value2 ', ' value3 ', ...))

#create stacked bar chart
ggplot(df, aes(x=x_var, y=y_var, fill=fill_var)) + 
    geom_bar(position=' stack ', stat=' identity ')

အောက်ဖော်ပြပါ ဥပမာသည် ဤ syntax ကို လက်တွေ့တွင် မည်သို့အသုံးပြုရမည်ကို ပြသထားသည်။

ဥပမာ- ggplot2 ရှိ တန်းစီထားသော ဘားဇယားတွင် ဘားများကို ပြန်စီပါ။

မတူညီသော ဘတ်စကက်ဘောကစားသမားများမှ ရမှတ်များကိုပြသသည့် R တွင် အောက်ပါဒေတာဘောင်ရှိသည်ဆိုပါစို့။

 #create data frame
df <- data. frame (team=c('A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'),
                 position=c('G', 'F', 'C', 'G', 'F', 'C', 'G', 'F', 'C'),
                 points=c(22, 12, 10, 30, 12, 17, 28, 23, 20))

#view data frame
df

  team position points
1 AG 22
2 AF12
3 AC 10
4 BG 30
5 BF 12
6 BC 17
7 GC 28
8 CF 23
9 CC 20

အဖွဲ့တစ်ခုစီရှိ ကစားသမားများမှ ရမှတ်များကို မြင်သာစေရန် တန်းစီထားသော ဘားဇယားကို ဖန်တီးပါက၊ ggplot2 သည် ဘားများကို အက္ခရာစဉ်အလိုက် အလိုအလျောက် စည်းပေးပါမည်။

 library (ggplot2)

#create stacked bar chart
ggplot(df, aes(x=team, y=points, fill=position)) + 
    geom_bar(position=' stack ', stat=' identity ') 

တန်းစီထားသောဘားတစ်ခုစီသည် အက္ခရာစဉ်အလိုက် အနေအထား (အပေါ်မှအောက်ခြေ) ကိုပြသသည်ကို သတိပြုပါ။

ဘားများကို တိကျသောနည်းလမ်းဖြင့် ပြန်စီရန်၊ ကျွန်ုပ်တို့သည် အနေအထားပြောင်းနိုင်သော ကိန်းရှင်အဖြစ်သို့ ပြောင်းလဲနိုင်ပြီး တန်းစီထားသော ဘားဇယားတွင် ဘားများ (အပေါ်မှအောက်ခြေ) ဖြစ်သင့်သည့် အစီအစဥ်ကို သတ်မှတ်ရန် အဆင့် အငြင်းပွားမှုကို အသုံးပြုနိုင်ပါသည်။

 library (ggplot2)

#convert 'position' to factor and specify level order
df$position <- factor(df$position, levels=c(' F ', ' G ', ' C '))

#create stacked bar chart
ggplot(df, aes(x=team, y=points, fill=position)) + 
    geom_bar(position=' stack ', stat=' identity ') 

အဆင့် အငြင်းပွားမှုတွင် ကျွန်ုပ်တို့သတ်မှတ်ထားသော အတိအကျအစီအစဥ်အတိုင်း ဘားများကို ယခု (အပေါ်မှအောက်ခြေ) တွင် စုထားသည်။

ထပ်လောင်းအရင်းအမြစ်များ

အောက်ဖော်ပြပါ သင်ခန်းစာများသည် ggplot2 တွင် အခြားဘုံအလုပ်များကို မည်သို့လုပ်ဆောင်ရမည်ကို ရှင်းပြသည်-

ggplot2 တွင် ဝင်ရိုးတံဆိပ်များကို လှည့်နည်း
ggplot2 တွင် ဝင်ရိုးကွဲများကို သတ်မှတ်နည်း
ggplot2 တွင် ဝင်ရိုးကန့်သတ်ချက်များကို မည်သို့သတ်မှတ်မည်နည်း။
ggplot2 တွင် ဒဏ္ဍာရီအညွှန်းများကို မည်သို့ပြောင်းရမည်နည်း။

မှတ်ချက်တစ်ခုထည့်ပါ။

သင့် email လိပ်စာကို ဖော်ပြမည် မဟုတ်ပါ။ လိုအပ်သော ကွက်လပ်များကို * ဖြင့်မှတ်သားထားသည်