Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adding Percent Change to Bar Graph

    graph bar (sum) cmr_stress1 ccta1 pet_stress1, over(year) ytitle(Patients) title (Cardiac Imaging Volume Data)
    Hi,
    I am trying to add percent change in addition to "sum" to this graph so that I can see percent increase or decrease every "year" in each of my variables on top of each bar.
    Thank you for your help.

  • #2
    I don't believe you can do this using graph bar (unless you manually edit things with the Graph Editor). But you should be able to do it if you create a bar graph using twoway bar instead (you would then be able to overlay it with a scatter, with invisible marker symbols and labels taken from another variable that tracks the percentage changes you mention).

    Comment


    • #3
      I am not sure what you mean. Would you be able to provide code as an example? Thanks

      Comment


      • #4
        Yosef Cohen sure, I would be happy to help. But you'll need to get the ball rolling by giving us a sample of your data using the -dataex- command, as suggested in the Statalist FAQ.

        Comment


        • #5
          #3 is the wrong way round. Please provide us with a data example! See https://www.statalist.org/forums/help#stata

          It just needs to be realistic, and in fact the results of


          Code:
          collapse (sum) cmr_stress1 ccta1 pet_stress1, by(year)
          dataex
          would be sufficient to give flavour.

          Comment


          • #6
            Code:
            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float year double(cmr_stress1 ccta1 pet_stress1)
            2010   0 22648             39028
            2011  77 23859             47850
            2012 292 20394             49556
            2013 126 18815             69546
            2014 346 20575             85451
            2015 230 22131             80784
            2016   0 25784 86372.09997558594
            2017  39 33632             95179
            2018   0 41894            108637
            2019 327 53357            123049
            2020  64 41768            108846
            2021 733 62461            118473
            end
            ------------------ copy up to and including the previous line ------------------ Listed 12 out of 12 observations
            Thanks

            Comment


            • #7
              Thanks much for your example. It is hard to know what your graph so far looks like -- but the very different magnitudes are a challenge. With some mixture of brute force and trickery I get this. Next time round I would probably round the % change more.


              Code:
              * Example generated by -dataex- 
              clear
              input float year double(cmr_stress1 ccta1 pet_stress1)
              2010   0 22648             39028
              2011  77 23859             47850
              2012 292 20394             49556
              2013 126 18815             69546
              2014 346 20575             85451
              2015 230 22131             80784
              2016   0 25784 86372.09997558594
              2017  39 33632             95179
              2018   0 41894            108637
              2019 327 53357            123049
              2020  64 41768            108846
              2021 733 62461            118473
              end
              
              rename (cmr_stress1 ccta1 pet_stress1) (q1 q2 q3)
              
              reshape long q, i(year) j(which) 
              
              label def which 1 "better" 2 "labels" 3 "needed"
              label val which which 
              
              bysort which (year) : gen toshow = strofreal(100 * (q - q[_n-1])/q[_n-1], "%2.1f") + "%" if _n > 1 & q[_n-1] > 0 
              
              set scheme s1color 
              
              gen nudge = cond(which == 1, 1000, cond(which == 2, 80000, 140000))  
              
              twoway bar q year, base(0) barw(0.8) lcolor(blue) fcolor(blue*0.3) by(which, col(1) legend(off) note("")  yrescale) ytitle("value and % change") subtitle(, fcolor(green*0.1)) xtitle("") || scatter q year, ms(none) mla(toshow) mlabpos(12) mlabcolor(black) xla(2010/2021, tlc(bg)) yla(, ang(h)) || scatter nudge year, ms(none)
              Click image for larger version

Name:	ycohen.png
Views:	1
Size:	37.7 KB
ID:	1685950

              Comment


              • #8
                I am a little confused by your code. What do these two lines do?
                Code:
                 
                 bysort which (year) : gen toshow = strofreal(100 * (q - q[_n-1])/q[_n-1], "%2.1f") + "%" if _n > 1 & q[_n-1] > 0   set scheme s1color   gen nudge = cond(which == 1, 1000, cond(which == 2, 80000, 140000))

                Comment


                • #9
                  That's three lines

                  Code:
                  bysort which (year) : gen toshow = strofreal(100 * (q - q[_n-1])/q[_n-1], "%2.1f") + "%" if _n > 1 & q[_n-1] > 0
                  That calculates % change. As I wanted to (show how to) show percent signs. I needed to make it a string with trailing "%", I also round to 1 decimal place. Percent change can't be calculated for the first value OR if the previous value was zero.
                  Code:
                  set scheme s1color
                  I think you can work this one out.
                  Code:
                  gen nudge = cond(which == 1, 1000, cond(which == 2, 80000, 140000))
                  This is a trick. As otherwise some of the bar labels would be cut off by the top axis, I needed to raise the roof of the house. But here there are three houses and a need to raise three roofs. So, I defined high values separately for each panel. In the event I plot this variable with no symbol, so you don't see it, but that's deliberate I just want the side-effect of extending the range on the y axis.

                  Comment


                  • #10
                    Thank you very much for all your time and help. When I tried the code with 5 variables instead of 3 I get this error:

                    Code:
                    . reshape long q, i(year) j(which)
                    (note: j = 1 2 3 4 5)
                    variable id does not uniquely identify the observations
                        Your data are currently wide.  You are performing a reshape long.  You specified i(year) and j(which).  In the current wide form, variable year should
                        uniquely identify the observations.  Remember this picture:
                    
                             long                                wide
                            +---------------+                   +------------------+
                            | i   j   a   b |                   | i   a1 a2  b1 b2 |
                            |---------------| <--- reshape ---> |------------------|
                            | 1   1   1   2 |                   | 1   1   3   2  4 |
                            | 1   2   3   4 |                   | 2   5   7   6  8 |
                            | 2   1   5   6 |                   +------------------+
                            | 2   2   7   8 |
                            +---------------+
                        Type reshape error for a list of the problem observations.
                    r(9);
                    
                    . reshape error
                    (note: j = 1 2 3 4 5)
                    
                    i (year) indicates the top-level grouping such as subject id.
                    
                    The data are currently in the wide form; there should be a single
                    observation per i.
                    
                    862581 of 862581 observations have duplicate i values:
                    
                            +------+
                            | year |
                            |------|
                         1. | 2010 |
                         2. | 2010 |
                         3. | 2010 |
                         4. | 2010 |
                         5. | 2010 |
                            |------|
                         6. | 2010 |
                         7. | 2010 |
                         8. | 2010 |
                         9. | 2010 |
                        10. | 2010 |
                            |------|
                        11. | 2010 |
                        12. | 2010 |
                        13. | 2010 |
                        14. | 2010 |
                        15. | 2010 |
                            |------|
                        16. | 2010 |
                        17. | 2010 |
                        18. | 2010 |
                        19. | 2010 |
                        20. | 2010 |
                            |------|
                        21. | 2010 |
                        22. | 2010 |
                        23. | 2010 |
                        24. | 2010 |
                        25. | 2010 |
                            |------|
                        26. | 2010 |
                        27. | 2010 |
                        28. | 2010 |
                        29. | 2010 |
                        30. | 2010 |
                            |------|
                        31. | 2010 |
                        32. | 2010 |
                        33. | 2010 |
                        34. | 2010 |
                        35. | 2010 |
                            |------|
                        36. | 2010 |
                        37. | 2010 |
                        38. | 2010 |
                        39. | 2010 |
                        40. | 2010 |
                            |------|
                        41. | 2010 |
                        42. | 2010 |
                        43. | 2010 |
                        44. | 2010 |
                        45. | 2010 |
                            |------|
                        46. | 2010 |
                        47. | 2010 |
                        48. | 2010 |
                        49. | 2010 |
                        50. | 2010 |
                            |------|
                        51. | 2010 |
                        52. | 2010 |
                        53. | 2010 |
                        54. | 2010 |
                        55. | 2010 |
                            |------|
                        56. | 2010 |
                        57. | 2010 |
                        58. | 2010 |
                        59. | 2010 |
                        60. | 2010 |
                            |------|
                        61. | 2010 |
                        62. | 2010 |
                        63. | 2010 |
                        64. | 2010 |
                        65. | 2010 |
                            |------|
                        66. | 2010 |
                        67. | 2010 |
                        68. | 2010 |
                        69. | 2010 |
                        70. | 2010 |
                            |------|
                        71. | 2010 |
                        72. | 2010 |
                        73. | 2010 |
                        74. | 2010 |
                        75. | 2010 |
                            |------|
                        76. | 2010 |
                        77. | 2010 |
                        78. | 2010 |
                        79. | 2010 |
                        80. | 2010 |
                            |------|
                        81. | 2010 |
                        82. | 2010 |
                        83. | 2010 |
                        84. | 2010 |
                        85. | 2010 |
                            |------|
                        86. | 2010 |
                        87. | 2010 |
                        88. | 2010 |
                        89. | 2010 |
                        90. | 2010 |
                            |------|
                        91. | 2010 |
                        92. | 2010 |
                        93. | 2010 |
                        94. | 2010 |
                        95. | 2010 |
                            |------|
                        96. | 2010 |
                        97. | 2010 |
                        98. | 2010 |
                        99. | 2010 |
                       100. | 2010 |
                            |------|
                       101. | 2010 |
                       102. | 2010 |
                       103. | 2010 |
                       104. | 2010 |
                       105. | 2010 |
                            |------|
                       106. | 2010 |
                       107. | 2010 |
                       108. | 2010 |
                       109. | 2010 |
                       110. | 2010 |
                            |------|
                       111. | 2010 |
                       112. | 2010 |
                       113. | 2010 |
                       114. | 2010 |
                       115. | 2010 |
                            |------|
                       116. | 2010 |
                       117. | 2010 |
                       118. | 2010 |
                       119. | 2010 |
                       120. | 2010 |
                            |------|
                       121. | 2010 |
                       122. | 2010 |
                       123. | 2010 |
                       124. | 2010 |
                       125. | 2010 |
                            |------|
                       126. | 2010 |
                       127. | 2010 |
                       128. | 2010 |
                       129. | 2010 |
                       130. | 2010 |
                            |------|
                       131. | 2010 |
                       132. | 2010 |
                       133. | 2010 |
                       134. | 2010 |
                       135. | 2010 |
                            |------|
                       136. | 2010 |
                       137. | 2010 |
                       138. | 2010 |
                       139. | 2010 |
                       140. | 2010 |
                            |------|
                       141. | 2010 |
                       142. | 2010 |
                       143. | 2010 |
                       144. | 2010 |
                       145. | 2010 |
                            |------|
                       146. | 2010 |
                       147. | 2010 |
                       148. | 2010 |
                       149. | 2010 |
                       150. | 2010 |
                            |------|
                       151. | 2010 |
                       152. | 2010 |
                       153. | 2010 |
                       154. | 2010 |
                       155. | 2010 |
                            |------|
                       156. | 2010 |
                       157. | 2010 |
                       158. | 2010 |
                       159. | 2010 |
                       160. | 2010 |
                            |------|
                       161. | 2010 |
                       162. | 2010 |
                       163. | 2010 |
                       164. | 2010 |
                       165. | 2010 |
                            |------|
                       166. | 2010 |
                       167. | 2010 |
                       168. | 2010 |
                       169. | 2010 |
                       170. | 2010 |
                            |------|
                       171. | 2010 |
                       172. | 2010 |
                       173. | 2010 |
                       174. | 2010 |
                       175. | 2010 |
                            |------|
                       176. | 2010 |
                       177. | 2010 |
                       178. | 2010 |
                       179. | 2010 |
                       180. | 2010 |
                            |------|
                       181. | 2010 |
                       182. | 2010 |
                       183. | 2010 |
                       184. | 2010 |
                       185. | 2010 |
                            |------|
                       186. | 2010 |
                       187. | 2010 |
                       188. | 2010 |
                       189. | 2010 |
                       190. | 2010 |
                            |------|
                       191. | 2010 |
                       192. | 2010 |
                       193. | 2010 |
                       194. | 2010 |
                       195. | 2010 |
                            |------|
                       196. | 2010 |
                       197. | 2010 |
                       198. | 2010 |
                       199. | 2010 |
                       200. | 2010 |
                            |------|
                       201. | 2010 |
                       202. | 2010 |
                       203. | 2010 |
                       204. | 2010 |
                       205. | 2010 |
                            |------|
                       206. | 2010 |
                       207. | 2010 |
                       208. | 2010 |
                       209. | 2010 |
                       210. | 2010 |
                            |------|
                       211. | 2010 |
                       212. | 2010 |
                       213. | 2010 |
                       214. | 2010 |
                       215. | 2010 |
                            |------|
                       216. | 2010 |
                       217. | 2010 |
                       218. | 2010 |
                       219. | 2010 |
                       220. | 2010 |
                            |------|
                       221. | 2010 |
                       222. | 2010 |
                       223. | 2010 |
                       224. | 2010 |
                       225. | 2010 |
                            |------|
                       226. | 2010 |
                       227. | 2010 |
                       228. | 2010 |
                       229. | 2010 |
                       230. | 2010 |
                            |------|
                       231. | 2010 |
                       232. | 2010 |
                       233. | 2010 |
                       234. | 2010 |
                       235. | 2010 |
                            |------|
                       236. | 2010 |
                       237. | 2010 |
                       238. | 2010 |
                       239. | 2010 |
                       240. | 2010 |
                            |------|
                       241. | 2010 |
                       242. | 2010 |
                       243. | 2010 |
                       244. | 2010 |
                       245. | 2010 |
                            |------|
                       246. | 2010 |
                       247. | 2010 |
                       248. | 2010 |
                       249. | 2010 |
                       250. | 2010 |
                            |------|
                       251. | 2010 |
                       252. | 2010 |
                       253. | 2010 |
                       254. | 2010 |
                       255. | 2010 |
                            |------|
                       256. | 2010 |
                       257. | 2010 |
                       258. | 2010 |
                       259. | 2010 |
                       260. | 2010 |
                            |------|
                       261. | 2010 |
                       262. | 2010 |
                       263. | 2010 |
                       264. | 2010 |
                       265. | 2010 |
                            |------|
                       266. | 2010 |
                       267. | 2010 |
                       268. | 2010 |
                       269. | 2010 |
                       270. | 2010 |
                            |------|
                       271. | 2010 |
                       272. | 2010 |
                       273. | 2010 |
                       274. | 2010 |
                       275. | 2010 |
                            |------|
                       276. | 2010 |
                       277. | 2010 |
                       278. | 2010 |
                       279. | 2010 |
                       280. | 2010 |
                            |------|
                       281. | 2010 |
                       282. | 2010 |
                       283. | 2010 |
                       284. | 2010 |
                       285. | 2010 |
                            |------|
                       286. | 2010 |
                       287. | 2010 |
                       288. | 2010 |
                       289. | 2010 |
                       290. | 2010 |
                            |------|
                       291. | 2010 |
                       292. | 2010 |
                       293. | 2010 |
                       294. | 2010 |
                       295. | 2010 |
                            |------|
                       296. | 2010 |
                       297. | 2010 |
                       298. | 2010 |
                       299. | 2010 |
                       300. | 2010 |
                            |------|
                       301. | 2010 |
                       302. | 2010 |
                       303. | 2010 |
                       304. | 2010 |
                       305. | 2010 |
                            |------|
                       306. | 2010 |
                       307. | 2010 |
                       308. | 2010 |
                       309. | 2010 |
                       310. | 2010 |
                            |------|
                       311. | 2010 |
                       312. | 2010 |
                       313. | 2010 |
                       314. | 2010 |
                       315. | 2010 |
                            |------|
                       316. | 2010 |
                       317. | 2010 |
                       318. | 2010 |
                       319. | 2010 |
                       320. | 2010 |
                            |------|
                       321. | 2010 |
                       322. | 2010 |
                       323. | 2010 |
                       324. | 2010 |
                       325. | 2010 |
                            |------|
                       326. | 2010 |
                       327. | 2010 |
                       328. | 2010 |
                       329. | 2010 |
                       330. | 2010 |
                            |------|
                       331. | 2010 |
                       332. | 2010 |
                       333. | 2010 |
                       334. | 2010 |
                       335. | 2010 |
                            |------|

                    Comment


                    • #11
                      So, you have other variables which complicate the question mightily. My code in #7 starts from the collapse results. It is not intended to, and will not, work correctly with your full dataset.

                      Comment

                      Working...
                      X