I have a series of graphs made in a loop which are then amalgamated using grc1leg (a wrapper for graph combine). There are a number of these combined graphs that will need to be compared. When running a loop, if there is a graph without observations, state throws up an error and stops. This can be circumnavigated by prefixing capture in front of the catplot command. A further error then manifests when trying to save the graph that has no data. The potential errors are 1) failure to save anything if no graph is in the memory and the code stopping, 2) saving the last graph in the memory and the code continuing. The first of these can also be circumnavigated by introducing an if command and counting the number of non-missing datapoints and opting not to save if there is no graph. However, there is then an absence of file to combine with grc1leg.
For ease of coding and for ease of reading and comparability, I can manually remove the missing graph's code and introduce a hole into the grc1leg matrix where this missing graph would be, however this is a suboptimal method of fixing the problem. Instead it would be easier if the original graph would save as an empty graph with no datapoints, but I am unclear as to how to ask state to comply (if indeed it can).
initial code:
Code:
foreach var of local systemic { colorpalette red yellow, ipolate(4) nograph capture catplot Overall_`var' , over(Armcode) stack asyvars bar(2, col("`r(p4)'")) bar(3, col("`r(p3)'")) bar(4, col("`r(p2)'")) bar(5, col("`r(p1)'")) percent graphregion(color(white)) bgcolor(white) allcategories legend(order(2 3 4)) graph save "`var'.gph", replace } grc1leg "1.gph" "2.gph" "3.gph" "4.gph" "5.gph" "6.gph" "7.gph" "8.gph" graphregion(color(white)) graph save "combine.gph", replace
Code:
foreach var of local systemic { colorpalette red yellow, ipolate(4) nograph capture catplot Overall_`var' , over(Armcode) stack asyvars bar(2, col("`r(p4)'")) bar(3, col("`r(p3)'")) bar(4, col("`r(p2)'")) bar(5, col("`r(p1)'")) percent graphregion(color(white)) bgcolor(white) allcategories legend(order(2 3 4)) qui count if !(missing(Overall_`var')) if(`r(N)'>0) { graph save "`var'.gph", replace } }
Code:
foreach var of local systemic { colorpalette red yellow, ipolate(4) nograph capture catplot Overall_`var' , over(Armcode) stack asyvars bar(2, col("`r(p4)'")) bar(3, col("`r(p3)'")) bar(4, col("`r(p2)'")) bar(5, col("`r(p1)'")) percent graphregion(color(white)) bgcolor(white) allcategories legend(order(2 3 4)) graph save "`var'.gph", replace } grc1leg "2.gph" "3.gph" "4.gph" "5.gph" "6.gph" "7.gph" "8.gph" graphregion(color(white)) holes(1) graph save "combine.gph", replace
Rob
0 Response to Create empty graph in a loop
Post a Comment