【问题标题】:Editing hbar graph (Stata)编辑 hbar 图 (Stata)
【发布时间】:2021-02-12 23:35:14
【问题描述】:

我下面的代码生成了附加的图表。但是,我正在尝试添加两个调整但没有运气。 1- 我想组织 Y 轴,所有行业的 11 月都在 12 月之前,而不是像当前图表中那样按哪个月有更多工作来排列。 2- 我还尝试在 Y 轴上添加标签,它只显示“Nov”和“Dec”,没有额外的文本,虽然 Stata 不会产生任何错误,但它不会改变图表。

preserve
drop if total_jobs_industry<15
graph hbar (count) total_jobs_industry, over(month) over(industry, sort(1)) subtitle("Jobs by Industry and month", span) 
restore 

我知道我可以在 Stata 中手动更改带有微小细节的图表,但如果可能的话,我更喜欢自动化该过程。

数据示例:

 Example generated by -dataex-. To install: ssc install dataex
clear
input float total_jobs_industry str39 industry str8 month
11 "Architectural & Engineering Services" "Nov_2020"
11 "Architectural & Engineering Services" "Nov_2020"
11 "Architectural & Engineering Services" "Dec_2020"
11 "Architectural & Engineering Services" "Dec_2020"
11 "Architectural & Engineering Services" "Nov_2020"
11 "Architectural & Engineering Services" "Dec_2020"
11 "Architectural & Engineering Services" "Dec_2020"
11 "Architectural & Engineering Services" "Nov_2020"
38 "Computer Hardware & Software"         "Dec_2020"
12 "Consulting"                           "Dec_2020"
63 ""                                     "Dec_2020"
32 "IT Services"                          "Dec_2020"
32 "IT Services"                          "Nov_2020"
38 "Computer Hardware & Software"         "Nov_2020"
12 "Aerospace & Defense"                  "Nov_2020"
12 "Accounting"                           "Nov_2020"
12 "Accounting"                           "Dec_2020"

当我使用 sum 而不是 count 时,我得到了下图:

preserve
drop if total_jobs_industry<15
graph hbar (sum) total_jobs_industry, over(month) over(industry, sort(1)) subtitle("Jobs by Industry and month", span) 
restore 

此外,这就是我创建变量以计算每个行业的工作数量的方式:

// The variable id contains observation number running from 1 to X and nt is the total number of observations
generate id = _n
generate nt = _N

// Sorting by inudstry. Now n1 is the observation number within each Industry group and total_jobs_industry is the total number of observations for each Industry group.
sort industry 
by industry: generate n1 = _n
by industry: generate total_jobs_industry = _N
order total_jobs_industry, a(industry)

【问题讨论】:

  • 请提供数据示例。请阅读help dataexstata 标签维基。
  • 当心(count) 计数非缺失观察。每个行业和每个月的工作总数真的在 3 到 25 之间吗?
  • 我猜你想要(sum) 而不是(count)。为什么drop 声明?
  • 但是您的图表显示 3 到 25 而您的数据没有。

标签: stata


【解决方案1】:

这是一个非常令人费解的问题。以下原因列表不完整。

  1. 该帖子似乎混合了新旧版本,并且不一致。你不能合理地期望我们可靠地解码这样一个曲折的故事。这里的标准是提供一个最小的可验证示例,并且该线程不符合该标准。见guidance here

  2. 显示的图表均不符合给定的数据。

  3. 我很难相信(count) 对您的数据有意义。如前所述,它计算非缺失值,但您的关键变量似乎是total_count_industry。另一方面,使用 (sum) 进行各种处理,观察次数似乎会混淆不同类型的计算。

  4. 您的示例数据中似乎存在重复的观察结果。

  5. 您声明您还尝试在 Y 轴上添加标签,其中仅显示“Nov”和“Dec”,但您的代码中没有显示任何此类评论尝试。

  6. 您期望Nov_2020 排在Dec_2020 之前,这不会发生,因为就Stata 而言它只是一个字符串变量,因此D 排在N 之前这一事实至关重要.这就是 12 月在 1 月之前排序的原因,这与按行业值排序无关,它只影响条形组的排序。您没有使用 Stata 的日期变量功能。

我怀疑除了最后一个问题,我是否能理解这些问题中的任何一个。 graph hbar 的限制似乎是它忽略了时间变量的显示格式,所以我使用值标签来确保 NovDec 按您希望的顺序排序。

clear
input float total_jobs_industry str39 industry str8 month
11 "Architectural & Engineering Services" "Nov_2020"
11 "Architectural & Engineering Services" "Nov_2020"
11 "Architectural & Engineering Services" "Dec_2020"
11 "Architectural & Engineering Services" "Dec_2020"
11 "Architectural & Engineering Services" "Nov_2020"
11 "Architectural & Engineering Services" "Dec_2020"
11 "Architectural & Engineering Services" "Dec_2020"
11 "Architectural & Engineering Services" "Nov_2020"
38 "Computer Hardware & Software"         "Dec_2020"
12 "Consulting"                           "Dec_2020"
63 ""                                     "Dec_2020"
32 "IT Services"                          "Dec_2020"
32 "IT Services"                          "Nov_2020"
38 "Computer Hardware & Software"         "Nov_2020"
12 "Aerospace & Defense"                  "Nov_2020"
12 "Accounting"                           "Nov_2020"
12 "Accounting"                           "Dec_2020"
end 

duplicates drop 

gen mdate = monthly(month, "MY")

levelsof mdate, local(months)
tokenize "`c(Mons)'" 
foreach m of local months { 
    local month = month(dofm(`m'))
    label def mdate `m' "``month''", modify 
}
label val mdate mdate 

set scheme s1color 
graph hbar (asis) total_jobs_industry, over(mdate) over(industry, sort(1) descending) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多