Hello,
I am currently using panel data and i would like to generate several (dynamic) line graphs by category.
My dataset looks like:
I would like to make line graphs by each flag variables which contain several lines separated by each category (either size or ind_a7).
I initially used:
preserve
keep $id $yr ind_a7 flag_jump_wage
bys ind_a7 $yr: egen num = total(flag_jump_wage == 1)
bys ind_a7 $yr: egen den = count(flag_jump_wage)
gen share = (num / den) * 100
bys ind_a7 $yr: keep if _n == 1
local baseflag = substr("flag_jump_wage", 5, .) // Remove "flag_"
local gtitle = "Share of firms with {it:flag_jump_wage} by Industry"
local gname = "D_`baseflag'_industry"
line share $yr, c(L) ///
title("`gtitle'") ///
ytitle("Share (%)") name(`gname', replace)
restore
-> this code before making loops. Afterward, I should make loop by category (ind_a7 or size) and for each variables (flag_jump_*).
With the above code, graph generated looks like in the attachment.
Here the problem is that colors are same and there is no label for category.
Can someone help me with generating several graphs (by each variables) by each category over time using loop, please?
fyi, I also generated "average" share of flag using the code below - while this was for generating average share:
ds firm_flag_*
local fflag_vars `r(varlist)'
foreach cat in ind_a7 size {
foreach var of local fflag_vars {
preserve
* Keep relevant variables and drop duplicates by firm and category
keep $id $yr `cat' `var'
quietly bysort $id `cat': keep if _n == 1
* Compute share
bysort `cat': egen num = total(`var' == 1)
bysort `cat': egen den = count(`var')
gen share = 100 * num / den
* === Title and Graph Name Setup ===
* Extract base flag name
local baseflag1 = substr("`var'", 11, .) // Remove "firm_flag_"
local baseflag2 = substr("`var'", 6, .)
//local baseflag = subinstr("`baseflag'", "_", " ", .) // Convert underscore to space for title
* Set category text and graph suffix
if "`cat'" == "ind_a7" {
local catlabel = "industry"
}
else if "`cat'" == "size" {
local catlabel = "size"
}
* Title and file-safe graph name
local gtitle = "Avg. Share of firms with {it:`baseflag2'} by `catlabel'"
local baseflag_clean = subinstr("`baseflag1'", " ", "_", .)
local gname = "B_`baseflag_clean'_`catlabel'"
* === Generate Bar Graph ===
graph bar share, over(`cat', label(angle(45) labsize(small))) ///
blabel(bar, format(%4.1f)) ///
title("`gtitle'") ///
ytitle("Share (%)") name(`gname', replace)
graph export "${output_graph}/`gname'.jpg", as(jpg) replace
restore
}
}
I am not sure if my question and aim of this code was clear ://
And I appreciate it a lot if they are efficiently coded (using loops, local etc)..
+) for generating average share (in fyi), if there is other code that is more efficient than this, I would be happy to have adjustments!
Thank you in advance!
AC
I am currently using panel data and i would like to generate several (dynamic) line graphs by category.
My dataset looks like:
ID | year | size | ind_a7 | flag_wage | flag_mis_L_LC | flag_L_pers | flag_jump_L_empl | flag_jump_labour_cost | flag_jump_wage | flag_jump_gross_output | flag_jump_value_added | flag_jump_K_L | flag_jump_GO_L | flag_jump_VA_L | flag_jump_capital_tan | flag_jump_invest_tan | flag_jump_capital_intan | flag_jump_invest_intan | flag_BG | flag_BG_com |
1 | 2011 | . | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
1 | 2012 | Small (10-19) | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 1 | 0 |
1 | 2013 | Small (10-19) | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
1 | 2014 | Small (10-19) | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 |
1 | 2015 | Small (10-19) | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 |
1 | 2016 | MediumSmall (20-49) | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
1 | 2017 | MediumSmall (20-49) | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 |
1 | 2018 | Medium (50-99) | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 0 |
1 | 2019 | Medium (50-99) | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 0 |
1 | 2020 | Medium (50-99) | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 |
1 | 2021 | Medium (50-99) | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 1 | 0 |
2 | 2002 | . | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
2 | 2003 | . | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
3 | 2010 | . | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 |
3 | 2011 | Micro (5-9) | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
3 | 2012 | . | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
5 | 2012 | . | Non-Financial Market Services [G-N] | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
5 | 2013 | . | Non-Financial Market Services [G-N] | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
5 | 2014 | . | Non-Financial Market Services [G-N] | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
5 | 2015 | . | Non-Financial Market Services [G-N] | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
5 | 2016 | . | Non-Financial Market Services [G-N] | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
8 | 2020 | . | Non-Financial Market Services [G-N] | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 |
9 | 2014 | . | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
9 | 2015 | . | Non-Financial Market Services [G-N] | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
9 | 2016 | . | Non-Financial Market Services [G-N] | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
I would like to make line graphs by each flag variables which contain several lines separated by each category (either size or ind_a7).
I initially used:
preserve
keep $id $yr ind_a7 flag_jump_wage
bys ind_a7 $yr: egen num = total(flag_jump_wage == 1)
bys ind_a7 $yr: egen den = count(flag_jump_wage)
gen share = (num / den) * 100
bys ind_a7 $yr: keep if _n == 1
local baseflag = substr("flag_jump_wage", 5, .) // Remove "flag_"
local gtitle = "Share of firms with {it:flag_jump_wage} by Industry"
local gname = "D_`baseflag'_industry"
line share $yr, c(L) ///
title("`gtitle'") ///
ytitle("Share (%)") name(`gname', replace)
restore
-> this code before making loops. Afterward, I should make loop by category (ind_a7 or size) and for each variables (flag_jump_*).
With the above code, graph generated looks like in the attachment.
Here the problem is that colors are same and there is no label for category.
Can someone help me with generating several graphs (by each variables) by each category over time using loop, please?
fyi, I also generated "average" share of flag using the code below - while this was for generating average share:
ds firm_flag_*
local fflag_vars `r(varlist)'
foreach cat in ind_a7 size {
foreach var of local fflag_vars {
preserve
* Keep relevant variables and drop duplicates by firm and category
keep $id $yr `cat' `var'
quietly bysort $id `cat': keep if _n == 1
* Compute share
bysort `cat': egen num = total(`var' == 1)
bysort `cat': egen den = count(`var')
gen share = 100 * num / den
* === Title and Graph Name Setup ===
* Extract base flag name
local baseflag1 = substr("`var'", 11, .) // Remove "firm_flag_"
local baseflag2 = substr("`var'", 6, .)
//local baseflag = subinstr("`baseflag'", "_", " ", .) // Convert underscore to space for title
* Set category text and graph suffix
if "`cat'" == "ind_a7" {
local catlabel = "industry"
}
else if "`cat'" == "size" {
local catlabel = "size"
}
* Title and file-safe graph name
local gtitle = "Avg. Share of firms with {it:`baseflag2'} by `catlabel'"
local baseflag_clean = subinstr("`baseflag1'", " ", "_", .)
local gname = "B_`baseflag_clean'_`catlabel'"
* === Generate Bar Graph ===
graph bar share, over(`cat', label(angle(45) labsize(small))) ///
blabel(bar, format(%4.1f)) ///
title("`gtitle'") ///
ytitle("Share (%)") name(`gname', replace)
graph export "${output_graph}/`gname'.jpg", as(jpg) replace
restore
}
}
I am not sure if my question and aim of this code was clear ://
And I appreciate it a lot if they are efficiently coded (using loops, local etc)..
+) for generating average share (in fyi), if there is other code that is more efficient than this, I would be happy to have adjustments!
Thank you in advance!
AC
Comment