Hello, my data consists of binned observations which contain counts organized by year, engine and costs.
I am trying to merge two graphs with two axes into one. Here is the code I have used to produce the three graphs below:



The result is close to what I want, but I would like the bars next to each other instead of on top of each other. I have tried combining commands like graph (bar x, over y (asyvars) z) with (twoway line) to no avail. Separately, I get graphs that look like they're getting to the right place, but together it doesn't seem to work. A common error I get is '( bargraph_g invalid name' for code similar to the following:
Additionally, I'd like to ask how I should refer to the two y-axes in the code when changing their titles, labels, ticks, ranges etc.
Much thanks in advance!
I am trying to merge two graphs with two axes into one. Here is the code I have used to produce the three graphs below:
Code:
graph twoway (bar counts bin_e if engine == 1 & year == 2015, yaxis(1)) /// (line cost bin_e if engine == 1 & year == 2015, sort yaxis(2) xline(82 110 160 180, lpattern(dash) lc(gray)))
Code:
graph twoway (bar counts bin_e if engine == 2 & year == 2015, yaxis(1)) ///(line cost bin_e if engine == 2 & year == 2015, sort yaxis(2) xline(82 110 160 180, lpattern(dash) lc(gray)))
Code:
graph twoway (bar counts bin_e if engine == 1 & year == 2015, yaxis(1)) ///(bar counts bin_e if engine == 2 & year == 2015, yaxis (1)) ///(line cost bin_e if engine == 1 & year == 2015 , sort yaxis(2) lc(navy) xline(82 110 160 180, lpattern(dash) lc(gray))) ///(line cost bin_e if engine == 2 & year == 2015, sort yaxis(2) lc(maroon)), ///legend ( ///label(1 count a) label(2 count b) label(3 cost a) label(4 cost b))
The result is close to what I want, but I would like the bars next to each other instead of on top of each other. I have tried combining commands like graph (bar x, over y (asyvars) z) with (twoway line) to no avail. Separately, I get graphs that look like they're getting to the right place, but together it doesn't seem to work. A common error I get is '( bargraph_g invalid name' for code similar to the following:
Code:
graph (bar counts if inlist(engine, 1, 2) & year == 2015, over(engine) asyvars over(bin_e)) /// (twoway line costs bin_e, sort yaxis(2) xline(82 110 160 180, lpattern(dash))) if engine == 1 & year == 2015
Much thanks in advance!
Comment