【问题标题】:Adding sample size to box plot将样本量添加到箱线图
【发布时间】:2021-03-23 02:39:02
【问题描述】:

我们测量了不同年龄组的抗体水平,每个组的样本量不同。

我想在箱形图顶部添加相应组的样本量(即幼儿男孩的样本量)。所附照片显示了我的一个条形图。

我创建条形图的代码:

graph box igg1ugml_log10, over(female_sex) over(age_groups) ///
bar(samplesize) graphregion(color(white)) /// 
title(Anti-EPEC IgG1 (ug/ml) in boys and girls) asyvars ///
ylabel(2.69897 "500" 3 "1,000" 3.3 "2000" 3.69 "5000" 3.95 "9000")

bar graph example

【问题讨论】:

    标签: size stata sample graphing


    【解决方案1】:

    要向graph box 添加文本,请使用记录在案的text() 选项。这是一个可重现的例子。除了使用图表编辑器之外,除了摆弄看起来足够好的东西之外,我没有任何计算文本位置的方法。

    sysuse auto, clear
    gen logprice = log10(price)
    ssc install mylabels 
    su price
    mylabels 3000(2000)15000, myscale(log10(@)) local(yla)
    
    graph box logprice, over(foreign) yla(`yla', ang(h)) ///
    text(4.25 21.2 "{it:n} = 52") text(4.25 79.8 "{it:n} = 22") ///
    ysc(r(. 4.3)) scheme(s1color) ytitle(Price (USD))
    

    注意。要正确显示微克的 mu,请参阅 Stata 中的help graph text 并搜索希腊字母。

    编辑

    来自 SSC 的stripplot 也可以生成箱线图,尽管它的默认值和可能性都与graph box 不同。这是一个可重现的例子。

    sysuse auto, clear
    egen count = count(mpg), by(rep78)
    
    gen where = 10.5 
    
    stripplot mpg , box vertical ms(none) pctile(5) over(rep78) ///
    yla(12 41 15(5)40, ang(h)) ///
    addplot(scatter where rep78, mla(count) ms(none) mlabpos(0) ///
    mlabsize(medsmall)) scheme(s1color)
    

    同样,尽管这是可重现的代码,但选择 10.5 是由于与此处未显示的其他值一起使用的结果。您可以尝试通过基于样本最大值和最小值的计算来自动选择,当然还有您对它应该在哪里的偏好。如果您要生产数十个这样的产品,那将是一个好主意。对于论文或演示文稿的单一情节,我只会玩。

    【讨论】:

    • 是否可以从我的数据中获取“n=”?而不是为每个条手动输入 n=
    • 当然可以,但是您必须对其进行编程。在为特定图形编写临时代码和尝试编写更通用的东西之间,这是一个艰难的决定。我添加了一个不同的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多