fork download
  1. // 清除之前的数据和变量
  2. clear all
  3.  
  4. // 输入数据
  5. input str10 race str15 age_group owns_cat does_not_own_cat
  6. "White" "youth" 65 84
  7. "White" "middle-aged" 92 107
  8. "White" "elderly" 87 120
  9. "Black" "youth" 4 14
  10. "Black" "middle-aged" 3 17
  11. "Black" "elderly" 4 15
  12. "Other" "youth" 7 13
  13. "Other" "middle-aged" 4 19
  14. "Other" "elderly" 4 12
  15.  
  16. // 计算每个组别的拥有猫和不拥有猫的百分比
  17. gen total = owns_cat + does_not_own_cat
  18. gen owns_cat_percentage = (owns_cat / total) * 100
  19. gen does_not_own_cat_percentage = (does_not_own_cat / total) * 100
  20.  
  21. // 重构数据为长格式
  22. gen cat_status = "owns_cat"
  23. gen percentage = owns_cat_percentage
  24. tempfile temp
  25. save `temp', replace
  26.  
  27. gen cat_status = "does_not_own_cat"
  28. gen percentage = does_not_own_cat_percentage
  29. append using `temp'
  30.  
  31. // 将 cat_status 转换为标签变量
  32. replace cat_status = "Owns Cats" if cat_status == "owns_cat"
  33. replace cat_status = "Does Not Own Cats" if cat_status == "does_not_own_cat"
  34. label define cat_status_lbl 1 "Owns Cats" 2 "Does Not Own Cats"
  35. encode cat_status, gen(cat_status_encoded)
  36. label values cat_status_encoded cat_status_lbl
  37.  
  38. // 添加对应的数量
  39. gen population = .
  40. replace population = owns_cat if cat_status == "Owns Cats"
  41. replace population = does_not_own_cat if cat_status == "Does Not Own Cats"
  42.  
  43. // 绘制百分比的堆叠柱状图
  44. graph bar percentage, over(race) over(age_group) by(cat_status_encoded, legend(col(1))) ///
  45. stack title("Cat Ownership Percentage by Race and Age Group") ///
  46. ytitle("Percentage") xtitle("Race and Age Group") ///
  47. blabel(bar, format(%4.1f) position(center) size(small))
  48.  
Success #stdin #stdout 0.04s 25844KB
stdin
Standard input is empty
stdout
// 清除之前的数据和变量
clear all

// 输入数据
input str10 race str15 age_group owns_cat does_not_own_cat
"White" "youth" 65 84
"White" "middle-aged" 92 107
"White" "elderly" 87 120
"Black" "youth" 4 14
"Black" "middle-aged" 3 17
"Black" "elderly" 4 15
"Other" "youth" 7 13
"Other" "middle-aged" 4 19
"Other" "elderly" 4 12
end

// 计算每个组别的拥有猫和不拥有猫的百分比
gen total = owns_cat + does_not_own_cat
gen owns_cat_percentage = (owns_cat / total) * 100
gen does_not_own_cat_percentage = (does_not_own_cat / total) * 100

// 重构数据为长格式
gen cat_status = "owns_cat"
gen percentage = owns_cat_percentage
tempfile temp
save `temp', replace

gen cat_status = "does_not_own_cat"
gen percentage = does_not_own_cat_percentage
append using `temp'

// 将 cat_status 转换为标签变量
replace cat_status = "Owns Cats" if cat_status == "owns_cat"
replace cat_status = "Does Not Own Cats" if cat_status == "does_not_own_cat"
label define cat_status_lbl 1 "Owns Cats" 2 "Does Not Own Cats"
encode cat_status, gen(cat_status_encoded)
label values cat_status_encoded cat_status_lbl

// 添加对应的数量
gen population = .
replace population = owns_cat if cat_status == "Owns Cats"
replace population = does_not_own_cat if cat_status == "Does Not Own Cats"

// 绘制百分比的堆叠柱状图
graph bar percentage, over(race) over(age_group) by(cat_status_encoded, legend(col(1))) ///
    stack title("Cat Ownership Percentage by Race and Age Group") ///
    ytitle("Percentage") xtitle("Race and Age Group") ///
    blabel(bar, format(%4.1f) position(center) size(small))