hello
I am a new user who is learning how to utilize stata.
First of all, here is a description of the data I have built.
area : House area categorized by (total 7 legends)
(~20㎡, 20~40㎡, 40~60㎡, 60~85㎡, 85~100㎡, 100~130㎡, 130㎡~)
con_ym : Contract year month (2022.12~2023.12)
trade_volume: Trade volume of the corresponding housing area per month
total_trade_volume : Total housing transaction volume per month
ratio : trade_volume / total_trade_volume
Data Example
The data period is from December 2022 to December 2023, and I built the data for a total of 5 variables as shown above.
First, I utilized the code below to generate the following figure and the resulting graph.
u "data.dta"
gen trade_volume = 1
collapse (count) trade_volume, by(area con_ym)
egen total_trades_volume = total(trade_volume), by(con_ym)
gen ratio = trade_volume / total_trades_volume * 100
format %09.1fc ratio
gen yyyymm = con_ym
tostring yyyymm, replace
gen year_month = substr(yyyymm, 3,2) + "." + substr(yyyymm, 5,2)
* stacked bar graph *
graph bar (asis) ratio, over(area) over(year_month) asyvars stack name(StackedBar, replace) ///
title("2022.12 - 2023.12 Percentage of transactions by size") ///
ylabel(, angle(horizontal)) ///
blabel(bar, position(center) format(%9.1f)) ///
ytitle("Transaction Share (%)") ///
legend(order(1 "~20㎡" 2 "20㎡~40㎡" 3 "40㎡~60㎡" 4 "60㎡~85㎡" 5 "85㎡~100㎡" 6 "100㎡~130㎡" 7 "130㎡~") rows(2) position(6))

However, I want to draw a stacked bar graph using a twoway graph instead of a bar graph, because I want to show the year and month as two x-axes with the boundary between 2022 and 2023 as shown in the following error graph result.
However, when I run the code below, the x-axis is displayed in the desired form, but the stacked bar graph is not displayed properly.
u "data.dta"
gen trade_volume = 1
collapse (count) trade_volume, by(area con_ym)
egen total_trades_volume = total(trade_volume), by(con_ym)
gen ratio = trade_volume / total_trades_volume * 100
format %09.1fc ratio
gen mdate = ym(con_y,con_m)
su mdate if con_y == 2022, meanonly
local m1 = r(mean)
su mdate if con_y == 2023, meanonly
local m2 = r(mean)
levelsof mdate, local(labels)
twoway (bar ratio mdate) ///
, xla(`labels', noticks format(%tmNN)) ///
xmla(`m1' "2022" `m2' "2023", tlength(*6) tlc(none) labsize(medium)) ///
xli(`=ym(2022, 12) + 0.5', lp(dash) lw(thin) lc(gray)) ///
xtitle("")

The above describes my current situation and can be summarized as follows.
1. is there any way to add 2 x-axes and a year separator using a bar graph?
2. is there a way to draw a stacked bar based on the x-axis notation in a twoway graph?
If possible, I would prefer method 2.
Thank you for your expert help.
Best wishes for peace and happiness in your home.
I am a new user who is learning how to utilize stata.
First of all, here is a description of the data I have built.
area : House area categorized by (total 7 legends)
(~20㎡, 20~40㎡, 40~60㎡, 60~85㎡, 85~100㎡, 100~130㎡, 130㎡~)
con_ym : Contract year month (2022.12~2023.12)
trade_volume: Trade volume of the corresponding housing area per month
total_trade_volume : Total housing transaction volume per month
ratio : trade_volume / total_trade_volume
Data Example
area | con_ym | trade_volume | total_trade_volume | ratio |
~20㎡ | 202212 | 3,020 | 97,176 | 3.1 |
~20㎡ | 202301 | 2,560 | 97,918 | 2.6 |
~20㎡ | 202302 | 2,680 | 129,347 | 2.1 |
… | … | … | … | |
~20㎡ | 202312 | 2,301 | 103,627 | 2.2 |
20~40㎡ | 202212 | 10,156 | 97,176 | 10.5 |
20~40㎡ | … | … | … | … |
20~40㎡ | 202312 | 11,817 | 103,627 | 11.4 |
40~60㎡ | 202212 | 34,067 | 97,176 | 35.1 |
40~60㎡ | … | … | … | … |
40~60㎡ | 202312 | 37,657 | 103,627 | 36.3 |
60~85㎡ | 202212 | 40,927 | 97,176 | 42.1 |
60~85㎡ | … | … | … | … |
60~85㎡ | 202312 | 42,325 | 103,627 | 40.8 |
85~100㎡ | 202212 | 1,573 | 97,176 | 1.6 |
85~100㎡ | … | … | … | … |
85~100㎡ | 202312 | 1,530 | 103,627 | 1.5 |
100~130㎡ | 202212 | 5,264 | 97,176 | 5.4 |
100~130㎡ | … | … | … | … |
100~130㎡ | 202312 | 5,633 | 103,627 | 5.4 |
130㎡~ | 202212 | 2,169 | 97,176 | 2.2 |
130㎡~ | … | … | … | … |
130㎡~ | 202312 | 2,364 | 103,627 | 2.3 |
The data period is from December 2022 to December 2023, and I built the data for a total of 5 variables as shown above.
First, I utilized the code below to generate the following figure and the resulting graph.
u "data.dta"
gen trade_volume = 1
collapse (count) trade_volume, by(area con_ym)
egen total_trades_volume = total(trade_volume), by(con_ym)
gen ratio = trade_volume / total_trades_volume * 100
format %09.1fc ratio
gen yyyymm = con_ym
tostring yyyymm, replace
gen year_month = substr(yyyymm, 3,2) + "." + substr(yyyymm, 5,2)
* stacked bar graph *
graph bar (asis) ratio, over(area) over(year_month) asyvars stack name(StackedBar, replace) ///
title("2022.12 - 2023.12 Percentage of transactions by size") ///
ylabel(, angle(horizontal)) ///
blabel(bar, position(center) format(%9.1f)) ///
ytitle("Transaction Share (%)") ///
legend(order(1 "~20㎡" 2 "20㎡~40㎡" 3 "40㎡~60㎡" 4 "60㎡~85㎡" 5 "85㎡~100㎡" 6 "100㎡~130㎡" 7 "130㎡~") rows(2) position(6))
However, I want to draw a stacked bar graph using a twoway graph instead of a bar graph, because I want to show the year and month as two x-axes with the boundary between 2022 and 2023 as shown in the following error graph result.
However, when I run the code below, the x-axis is displayed in the desired form, but the stacked bar graph is not displayed properly.
u "data.dta"
gen trade_volume = 1
collapse (count) trade_volume, by(area con_ym)
egen total_trades_volume = total(trade_volume), by(con_ym)
gen ratio = trade_volume / total_trades_volume * 100
format %09.1fc ratio
gen mdate = ym(con_y,con_m)
su mdate if con_y == 2022, meanonly
local m1 = r(mean)
su mdate if con_y == 2023, meanonly
local m2 = r(mean)
levelsof mdate, local(labels)
twoway (bar ratio mdate) ///
, xla(`labels', noticks format(%tmNN)) ///
xmla(`m1' "2022" `m2' "2023", tlength(*6) tlc(none) labsize(medium)) ///
xli(`=ym(2022, 12) + 0.5', lp(dash) lw(thin) lc(gray)) ///
xtitle("")
The above describes my current situation and can be summarized as follows.
1. is there any way to add 2 x-axes and a year separator using a bar graph?
2. is there a way to draw a stacked bar based on the x-axis notation in a twoway graph?
If possible, I would prefer method 2.
Thank you for your expert help.
Best wishes for peace and happiness in your home.
Comment