使用图形组合将两个堆叠的条形图组合到同一轴上

时间:2018-01-26 20:12:51

标签: stata

我的任务是尝试在同一轴上制作带有两个堆积条形图的图形。虽然通常很容易,但我遇到了麻烦,因为这两个条形图没有一组共同的共同值(例如年份)。我已经包含了下面的数据集。

outcomes                    perct   highcred                hperct
Certificate & Diploma Only  8.33    Certificate & Diploma   8.33
Associate Only              2.93    Associate               14.29
Bachelor Only               11.36   Bachelor                6.93
Certificate + AA            2.2     
Associate + Bachelor        4.33    

我已经通过多种方式对数据进行了重新整理,并尝试使用graph bar, over() stack并尝试使用twoway的不同迭代。我确定使用twoway可以做到这一点,但我找不到解决方案。有什么想法吗?

修改

我已经分别绘制了两个条形图,并尝试使用graph combine将它们组合在一起。我已经明确表示他们都羞耻y轴,但它似乎并没有起作用。我做错了什么?

代码:

*B. Create a seperate variable for each value of outcomes
    levelsof outcomes, local(out)
    tokenize "1 2 3 4 5"
    foreach level of local out {
        gen outcome`1' = .
            replace outcome`1' = perct if outcomes=="`level'"
        mac shift
        }

*C. Create a seperate variable for each value of highgred
    levelsof highcred, local(high)
    tokenize "1 2 3"
    foreach level of local high {
        gen highcred`1' = .
            replace highcred`1' = hperct if highcred=="`level'"
        mac shift
        }   

//2: Create Bar graphs

    *A. Bar 1
        graph bar outcome1-outcome5, stack saving(bar1) 

    *B. Bar 2 
        graph bar highcred1-highcred3, stack saving(bar2)

    *C. Combine graphs
        graph combine bar1.gph bar2.gph, ycommon

2 个答案:

答案 0 :(得分:1)

ycommon选项可正常使用。但是,将条形图堆叠在单独的图形中然后组合它们的解决方案在两个图形共享相同的颜色的意义上是有问题的,这使得无法区分不同的类别。另一个挑战是如何将这些类别合并到单个图例中。

下面您可以找到解决这两个问题的解决方案:

levelsof outcomes, local(out)
levelsof highcred, local(high)
local highcopy "`high'"

local c1: word count `out'
local c2: word count `high'

local colors1 ebblue pink brown
local colors2 `colors1'

forvalues i = 1 / `= `c1' + `c2'' {
    generate outcome`i' = .
    gettoken outc out : out
    if `i' <= `c1' replace outcome`i' = perct if outcomes == "`outc'" 
    if `i' > `c1' {
        gettoken color colors1 : colors1
        local bars1 `bars1' bar(`i', color(`color'))
    }
    if `i' <= `c2' {
        generate highcred`i' = .
        gettoken highcc highcopy : highcopy
        replace highcred`i' = hperct if highcred == "`highcc'"
        gettoken color colors2 : colors2
        local bars2 `bars2' bar(`i', color(`color')) 
    } 
    if `i' <= `c1' local legend `legend' label(`i' "`outc'")
    else {
        gettoken highc high : high
        local legend `legend' label(`i' "`highc'")
    }
}

order outcome* high*

graph bar outcome1-outcome8, stack ///
                             ylabel(, nogrid) ///
                             graphregion(color(white)) ///
                             `bars1' ///
                             name(bar1, replace) ///
                             legend(`legend')

graph bar highcred1-highcred3, stack ///
                               ylabel(, nogrid) ///
                               yscale(off) ///
                               graphregion(color(white)) ///
                               `bars2' ///
                               name(bar2, replace) 

grc1leg bar1 bar2, ycommon graphregion(color(white)) legendfrom(bar1)

enter image description here

在每个blabel(bar, position(base))命令中添加选项graph bar将产生:

enter image description here

请注意,社区贡献命令grc1leg用于创建组合图。

答案 1 :(得分:0)

我真的不了解这些数据。我想值的顺序是值得保留的,尽管我不认为你的代码会这样做。我建议你使用不同的数据结构,水平条和没有堆叠会更好。 graph bar (asis)是一个更好的主意,如果您有一个图例,可以避免在图例中使用手段,但您根本不需要图例。

为此,您需要从 Stata Journal labmask)安装search labmask以获取链接。

您应该能够使用比outcomes highcred更好的文字。

clear 
input str42 outcomes perct str42 highcred hperct 
"Certificate & Diploma Only"  8.33    "Certificate & Diploma"   8.33
"Associate Only"              2.93    "Associate"               14.29
"Bachelor Only"               11.36   "Bachelor"                6.93
"Certificate + AA"            2.2     "" .
"Associate + Bachelor"        4.33    "" . 
end 

rename (outcomes-hperct) (x1 p1 x2 p2) 
gen id = _n 
reshape long x p , i(id) j(which) 

sort which id
replace id = _n
drop in 9/10 
labmask id, values(x)
label def which 1 "outcomes" 2 "highcred"
label val which which

graph hbar (asis) p, over(id) over(which) nofill scheme(s1color) ytitle(percent) ///
bar(1, bfcolor(none)) blabel(total, pos(base) format(%3.2f)) yla(none) ysc(alt) 

enter image description here