Announcement

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

  • How to structure a dataset for desired bar chart

    Hello,

    I have a dataset of brain volumes with variables named by structure and side, e.g., cerebellum_bilateral, cerebellum_left, cerebellum_right. I also have a grouping variable, patientgroup. I would like to generate a set of bar charts of mean volumes that are split first by patientgroup, then by side, and finally by structure. I'm not sure how I should restructure the dataset in order to do this. I'm using Stata/IC 15.1.

    Any recommendations would be greatly appreciated!

    Regards,
    Linda

  • #2
    I'm not sure about Stata 15, but I believe Stata 16 onward supports this without needing to restructure the data. Just look at the documentation with

    Code:
    help graph bar
    and look at the over() option. Note that you can use the over() option more than once. Is brain volume a categorical variable, or is it numerical? If it's numerical, then I think you actually want a histogram. If this answer isn't sufficient, please generate a data example with the -dataex- command, then paste your example into a follow up post. Please also be sure to surround your data example with CODE tags (see the # symbol in the editor). This will make it much easier for us to answer your question.
    Last edited by Daniel Schaefer; 03 Jul 2023, 11:23. Reason: clarity

    Comment


    • #3
      Thank you for your reply, Daniel. I've included the first 100 observations for a selection of the variables. I've tried using over() for patientgroup, but as I don't have a variable indicating bilateral/left/right, I don't have anything else to use over() with. That's why I wonder if a bit of restructuring might be needed first, sorry if I wasn't clear earlier!



      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte patientgroup float(totalgm_b totalgm_l totalgm_r caudate_b caudate_l caudate_r cerebellum_b cerebellum_l cerebellum_r frontal_b frontal_l frontal_r insula_b insula_l insula_r)
      0 60.2968 62.3638 58.3956 51.0046 48.4388 53.8116 51.1893 55.6138   46.21  88.9669  90.9571  87.1599 75.0593 73.7124  76.0684
      0 54.8497 59.9587 50.3777 53.7638 58.4125 49.1969 43.8094 50.7023 38.0096  74.5923  78.2482  70.4639 63.2051 67.1451  59.5413
      0  66.666  66.348 66.9676 61.7752 65.8175 57.5908 50.4237 51.4406 49.2276  89.7686  89.0138  90.3851 80.4068 80.4986  79.9069
      0   59.63 63.2532 56.2687 45.4513 43.8583 46.9541 46.2925 52.2569 41.0916  80.9317  82.2129  79.7418 71.9451 71.7072  71.9213
      0 47.3349 49.0585 45.8125 45.0729 49.1778 40.3469 39.3895 44.7514  35.859  63.4902  64.3441  62.7722 56.2198 51.6591  59.4027
      0 65.7441 66.0777 65.3786 66.0882 58.0334 73.6047 58.1303 58.6924 57.4869  77.2447  78.6405  76.0108 67.9035 67.6675  67.8436
      0 68.4884 72.4033 65.1446 59.2212 65.0452 53.7339 56.6968 61.2996  52.384  85.0159  85.7559  84.4587 80.4644 78.1379  81.4324
      0 78.6871 82.4249 75.2937 73.5505 69.4693 77.0134 63.6698 73.9224 53.3281 101.8944 100.8275 102.9664 87.0471 90.8433  83.2156
      0 72.7723 72.7052 72.8432  62.264 63.4638 61.0026 64.0897 63.6621 64.5512  94.7116  98.1507  91.9456 80.3425 80.7614  79.8567
      0 63.8106 66.0361 61.6428 56.1636 57.3653 54.9942 48.3676 52.2504 44.3745  94.0665  95.6337  92.6256 75.6631   85.65  65.2955
      0 61.9808 61.0865 62.7159 57.8461  61.384 53.8493 47.7321 46.8955  48.316  89.9105  93.3817  87.3064  77.952 79.4519   76.137
      0 59.4774  59.742 59.1813 52.4531 53.7486 51.2224 53.0418 50.2475 55.5598  72.4664  75.2081  70.1609 54.1491 56.3316  52.2852
      0 63.5598 63.4861 63.6055 52.3412 55.2275 48.9676 62.8102 64.1459 61.4762  74.3979  74.7729  74.0245 63.6662 61.2132  66.8579
      0 54.5539 53.5489 55.5269 51.8295 48.5753 54.9482  41.833 43.5171 39.8278  73.1329  73.8069   72.485  55.746  52.808  59.0276
      0 61.6989 62.9118 60.5539 53.6162 57.6116 49.0664 52.1393 54.3613 49.8455  75.9708  80.2024  72.6116 70.7297 64.1224  76.9624
      0 62.6578 64.2485  61.113 57.8774 54.6825 60.9164 53.6767 55.2108 52.2676  81.2678  81.7308  80.8548 69.4874 68.6911  70.2345
      0  67.013 67.2667 66.7659 64.7319 61.3116 67.8052 54.5213 51.9383 56.8751   97.213 102.6606  92.3478 79.5496  83.594  75.7271
      0 68.1519 67.2726 69.0754 56.7116 60.5679 52.9669 58.8888 56.2141 61.6682  96.7493   102.13  91.4625 83.1361 79.0818  85.3371
      0 61.5444 64.5011 58.8634  34.214  38.286 30.5997 55.6284 62.1344 50.7391  81.8922  85.1876   78.654  57.932 60.9352  53.2287
      0 57.2512 59.1768 55.4449 50.2151  46.585 54.3813 50.2913 54.8106  46.034  74.7569  75.7703  73.7324 59.2611 64.7647  55.3168
      0 60.1338 60.7404 59.5351 60.2498 66.9569 53.9289 51.4738 50.6263 52.4067  75.7188  78.2653  73.3367 63.4307 65.5126  60.9883
      0 54.1146 56.8812  51.704 47.3089 48.1705 46.3843 47.7598  49.653 46.1981  73.7643  76.3905   71.509 62.6152 60.2047  63.8948
      0  60.604 60.0061 61.0793  57.208 54.8097 60.2016 52.5468 50.6703 54.2947  74.8174  76.7591  73.2886 62.9037 59.1399  65.4032
      0 61.3253 61.9689 60.7965  63.424 68.7049 58.8744 52.9369 51.2678 54.3478  78.0553  80.5814  75.8044 59.4204 59.5387  59.3127
      0   55.58 56.5986 54.6415 48.6215 49.6166 47.7991 44.7138 45.9917 43.6633  72.0046   74.079  69.8273 60.8199 59.7085  61.5622
      0 61.7597 63.2524 60.4085 50.1412 49.5808 50.7139 54.4649    57.8  51.081  75.4858  77.7905  73.4665 69.4096  65.921  72.8072
      0 84.8242 86.8345 83.0974 69.0515 68.8729 69.4818 75.6036 76.1517 75.1697  95.5879 102.1221  90.0432  80.345 79.9541   79.964
      0 50.5292 50.5811 50.4645 41.4554 41.0237  41.189  47.273 46.7847 47.1598   59.037  59.2221  58.8008 44.3068 42.0125   48.394
      0 49.4656 52.9358 46.2528 50.6624 54.2415 46.4778 40.2311 45.3109 35.1652  67.7001  73.3012  63.1995 50.3984 48.1968  52.7031
      0 59.0405 57.1067 60.7692 47.4884  49.264 45.7657 50.4304 46.0311 55.4313  70.8059  71.6186  70.1065 59.6165 63.5839  55.8565
      0 52.4999 52.0502 52.8984 44.5814 43.3599 45.6727 48.5304 50.8012 46.6849  63.7643  62.8189  64.5326 56.9054 56.1335  57.4364
      0 81.3498 78.5637 84.2455 72.4601 71.4974 73.4793 65.4318 62.2369 70.2761  106.157 103.9442 108.1343 92.0607 87.6741  97.3738
      0 50.5289 49.6604 51.2986 50.2272 52.1773 48.2174 43.1453 38.8997 46.7428  65.3306  66.2433  64.4732 60.3413 62.7369  58.2678
      0 65.5414 65.6649 65.4365 54.9964 55.5548 54.6973 59.6378 58.8883 60.3316  84.4778  84.6564  84.3794 68.9135 66.2131  71.6229
      0 74.3188 75.5244 73.1756 62.2504 64.2134 60.4017 64.4487 66.9802 61.4026  93.8444   97.614  90.8225 82.9249  89.085  78.0854
      0 68.9017 72.3338 65.8533 56.6845 57.9995 55.3326 59.8477 68.1937 52.8116  86.9651  88.3778   85.716 78.9481 79.2795  78.3893
      0 54.0239 52.8243 55.1134 51.3122 44.2892 57.9104 43.8664 43.0942 44.8119  79.1768  80.5523  78.0004 61.8638 61.5548  62.0903
      0 58.6252 60.0478 57.4971   56.78 55.7642 57.9191    47.2 47.9438 46.6157  71.3383  75.5038  68.1954 64.3338 61.9192  65.9207
      0 60.7741 63.0515 58.6508 57.6083 53.7243  61.689 49.3306 53.2044 44.3811  77.6332  81.5898  74.2282 61.3162 65.5207  57.1728
      0 73.5421 75.4717 71.7382 62.2934 61.0522 63.2769 66.7457 68.6333 64.5441  89.0783   93.878  85.0928 76.2389  75.263  76.9481
      1 52.2144  52.355 52.0619  48.701 47.8294 49.7433 46.1549 44.9848 47.8272  68.2306  73.3039  63.4997 52.8501 57.7682  49.7426
      0 63.8912 62.7873 64.8225 48.2983 45.6042 51.3823 55.2621 53.8165 56.3618  77.6026  80.2372  75.2521 66.4785 62.5039  68.8661
      0  57.461 55.0288 59.6298  46.357 44.7511 47.9446 51.6488 48.7552 54.1021  71.6856  68.2983  74.9744 63.8767 60.0665  67.4194
      0 82.9509 87.3757 78.6911 71.6712 78.7004 64.7355 71.8943 77.3443 66.0104 110.8597 110.9263 110.7892 98.1611 94.5896 101.5414
      0 68.0128 68.4778  67.569 59.7284 55.0898 64.5961 49.7618 52.7172 47.2491  88.9714  90.9907  87.0657 77.1964 78.1843   76.355
      0 47.8828 48.7876 46.9892 45.8626  50.999 41.1809 36.0221 38.7493 33.2537  67.2499  71.2831  63.9053 58.6009 60.0283  56.4568
      0 69.1938   71.28 67.2668 50.1098 54.3445 46.6155 69.1642 70.9668 67.6598  85.6514  87.4398  83.6803 72.1321 70.7535  72.5136
      0 56.8503 61.2747 52.7926 45.5845 48.1233 43.0849 56.2912  59.219 53.0934  67.1101   72.598  62.3046 59.4047 64.1689  54.8218
      0 60.6073 61.1638 60.0964 48.2319 43.1766 53.1633 54.8522  56.909 53.0177  72.7663  73.3483   72.215 57.7683 57.5211  58.1377
      1 62.5936 64.9998 60.5349  48.179 45.5476 50.6182 64.1967 66.0398 61.6853  68.5246  76.7469  62.3378 56.5435 61.4212  53.0453
      0 52.1761 53.1752 51.2898 49.3402   44.98 52.9494 42.7293 45.6624 40.6865  67.7508  69.8093  65.8003 55.6794 58.9427  51.7742
      0  68.932  70.732 67.2077  53.414 50.6118  56.378 51.9364 54.9256 49.1204  94.5649  96.5584  92.6842 82.8298 82.9392  82.2209
      0 57.1026 58.3753  55.959 45.1658 47.3456 42.7267 55.0872 52.4975 57.2286  67.8941  70.8697  65.1634 60.7356 64.1612  57.8282
      0  49.821 51.2221  48.534 39.6502 41.8081 37.9162 44.5494 46.7139 42.6264  58.4572   59.239  57.7779 44.9499 46.9785  42.8196
      0 47.8806 46.0129 49.5617 47.5409 43.7123 50.9458 39.2671  39.403 39.1268  60.3191  57.2078  63.4644   57.61 50.7086  65.0234
      0 59.6178 61.1387 58.1874 51.7826 50.7272 52.6424 51.1545 53.8388 48.5773  80.9256  81.1147  80.7044 62.5605 61.1518  64.0085
      0 44.3024 46.5044 42.3267 45.4412 42.7045 47.4938 37.9404 40.7609 35.4377  59.8988  58.5664  61.1476 60.0114 59.4921  59.7998
      0 66.1013 69.4641  63.133 57.5139 53.3611 61.3194 55.4987 60.0017 52.2011  87.9676  92.8304  83.8814  74.808 75.4924  73.7245
      0 51.7909 52.8715 50.8415 46.1008 48.4235 44.0214 43.7431 43.5525 43.9803  62.3151  66.1432  58.9622 57.7573 61.3659  54.2799
      0 69.0865 68.8706 69.2765 60.5594 58.9522 62.1455 53.8372 57.8046 49.7566  85.8303  85.0379  86.5575 70.9685 68.9663  73.4228
      0 58.6036 56.9913 60.0081 49.1578 38.9178 58.4149 46.6107 52.2228 41.9521  73.2841  68.8459  77.1881 58.7578 54.7029  62.3339
      1 63.8447 61.1757 66.4424  59.393 58.3871 59.9321 77.5869 73.9979 80.6082  66.0892  68.5641  63.8845 55.4773 56.0317  54.7318
      1 53.4236 56.7282 50.3414 43.0746  39.245 47.5039  44.598 50.7409 37.7006  77.3151   81.733  73.5954 61.2047 67.1504  57.1123
      0  57.677 58.0383 57.3383 46.0856 43.9329 48.0914 50.7781 51.1437 50.4203  72.1772  73.4357  71.0068 61.4876  63.246  59.4987
      0  64.276 64.2278 64.3238  50.411   46.66 54.6714 55.2147 57.0005 53.5602  93.3855  96.6656  90.3683 76.6076 78.6294   74.555
      0 61.8666 66.4299 58.0374 55.0627 53.5294 56.4525 57.0192 59.5876 54.8299  68.6595  73.1864  64.6058 56.7301 61.0257  53.0698
      0 64.1897 64.5758 63.7884 57.6972 58.9603 56.5359 54.7337 54.6748  54.735  81.9391  84.2753  79.6479 67.2583 69.8132  64.9289
      0 54.0567  56.245 51.9684 40.0486 39.3442 40.8271 48.7484 48.7502 48.7295  65.0298  67.9715  62.3278 55.9217 65.0885  46.0752
      0 70.0881 70.5795 69.5973 57.7483 56.2293 59.2373  70.305  71.931 68.2752  80.5741  81.4745  79.7483 62.5699 61.1087  63.6697
      0 65.1994  67.153  63.305 56.0198 59.6472 52.5831 56.9426   62.96 51.8527  81.7041  82.5085  81.0145  80.087 77.2531  83.5473
      0 70.2659 70.9767 69.5304 57.2545 55.8663 58.6628 63.9125 62.1295 65.8022  82.4032  84.7179  80.2625 67.4685 68.5175  66.4099
      0 47.9396 48.9076 47.0857 50.2734 49.2332 51.3043 36.5831 38.6422  34.625  60.9349   64.656  58.1419 50.0254 50.4941  49.1869
      0 72.4439 78.7053 66.6231 71.4848 75.4322 67.5694 62.0489 71.1863 53.7893  89.2539  95.1624  84.2079 73.4683 78.1493  69.1333
      0 60.0835 59.9375 60.2665 50.9977 47.0455 54.4527 45.4676 44.5741 46.4777  85.7044  88.3611  82.7449 70.7052 75.8916   65.989
      0 61.3945 64.5577  58.548 60.1599 63.1402 57.3907 54.3381 58.4873 50.1923  78.6649  77.4019  79.8885 56.9974 60.6121  53.4003
      0 70.5079  68.943 72.1451 57.0124 55.1394 59.1044  67.262 66.3123 68.3033  83.2843  82.0923   84.104 73.8741 67.5442  80.3483
      0 42.3699 41.8764 42.8625 36.8144 37.4474 36.1688 34.2418 34.8307 33.5403   56.841   55.844  57.7945 48.2662 45.7671  49.4758
      0 55.5132 58.3386 53.1712 42.1147 46.1958 38.6458 50.1143 55.8443 44.9298  72.3719  73.3817  71.4458 59.9694   57.26  62.6285
      1 53.8197 53.9745 53.6712 48.5372 59.5233 38.5705 49.5729 50.2894 48.8311   68.736   68.585  68.8219 53.0205 50.6256  55.1918
      0 50.7015 52.0815 49.3889 40.7774 41.3696 40.1308 43.4273 46.2574 39.4752  68.8822  71.9666  65.5892 58.8967 63.3925  54.0033
      0   64.67 66.1995 63.4302 55.6119 55.9648 55.1195 50.8247 50.5928 51.2678  84.2146  84.9186  83.6382 69.3244  72.907  65.8613
      0 67.0063 67.8769 66.1873  58.125 58.9526 57.3707  59.072 56.3684 61.3511   86.986  91.8077  81.8405 67.4376 68.4057  65.9721
      0 56.1876 54.6405 57.6938 54.4861 56.9499 52.3706 50.5383 47.6389 53.9014  79.6339   79.963   79.263 49.3571 53.1304  46.1783
      0 83.4882 81.2075 85.5404 65.1528 60.4504  69.545 75.6433 76.3567 74.7413  98.6973  93.7027 102.0962  87.072 82.1469  92.1169
      0 57.1806 58.6064 55.7765 63.3835 65.3269  61.798 58.7926 62.9451 54.9044   61.708  61.3554  62.1743 54.7715   57.84   52.327
      0 59.5422  61.669 57.3274  56.062 57.0088 55.3125  50.893  53.163 48.7258  82.6109  87.1199  78.2388 61.9181 65.1292  56.8495
      0 48.0284 48.1374 47.8575 38.4666  41.178 35.2529 41.7001 41.4523 42.0358  64.5187  66.1854  62.8873 45.7989 50.8113  43.4414
      0 55.2707 56.6343 54.0135 46.0005 45.8195 45.8503  54.987 55.9405 53.1782  71.0229  75.0989  67.8798 50.7813 54.9062  47.1667
      0 52.7083 53.9704 51.5109 47.8622 50.1493 45.6358 39.9837 40.5847 39.4448  69.0765  71.2492  67.0837 58.2143 60.2162  55.9131
      1 51.4115 52.0238 50.7821 51.6927 54.8362 48.2193 43.9049 43.6051 44.4575  64.2236  64.0615  64.3426 47.9304 48.9504  46.8636
      0 65.3925 67.7637  63.213 60.8015 63.8635 57.8684 53.2898 54.9214  51.821  92.0756  99.1559  86.2317 70.1299 77.1642  63.5765
      0  76.411 75.3514 77.4798 61.9528 65.1928 58.6571 63.1855 63.6612 62.7306 108.5613 108.5621 108.5977  75.659 80.5752  70.3849
      0   68.81 70.9466 66.7589 56.3883 57.7912 55.0425  58.783  61.422 56.4019  86.6838  90.5447  82.9475 79.3961 76.6327  82.0757
      0 63.7167 61.5113 65.7548  52.491 51.6856 53.3406 59.2178 54.0753 63.9747  87.4176  87.1353  87.5619 64.0107 60.9733  66.8287
      1 67.2214 66.3063 68.1643 63.8272 63.9322 63.7484 53.4472 54.6507 52.0552  93.1035  91.5612  94.4695  69.239 65.4067  72.2991
      1 48.5012 50.2738 46.7946 53.3145 56.7048 48.7067  38.868 41.6934 36.3926   68.104  67.5512  68.6651 63.1604 57.2202  67.6735
      0 51.1471 51.9286 50.3793  44.944 41.8054 47.8862 49.1107 51.4157 46.7747  65.3529  65.7549  64.9919 55.2574 52.8833  58.4595
      0 62.1203 60.7554 63.6257 51.7884 49.1205 53.9153  53.638 50.4446 58.8222  74.6792  76.2467  73.2691 62.5684 66.4958  59.0685
      0 66.6505 67.8522 65.4428 63.6841 67.6092 59.7056 54.7729 57.5886 51.8815  86.0996  85.1123  87.2105  77.626 75.4497   78.794
      0  57.443 56.8629 57.9708 52.0261 48.3029 55.8428 39.5098 41.5964 36.5028  77.8593  77.9443   77.608 64.4837 59.3039  69.0069
      end

      Comment


      • #4
        From your data example, I note that means of the volumes for two patient groups are about the same. It follows that bar charts won't work well, as they conventionally compare results (here means) with zero, not each other. So, I would look at a dot chart and in particular I find that the version accessible using statplot (SSC) is preferable to the default. As statplot temporarily changes the data structure, it's a choice between statplot doing that for you and your doing that yourself.

        I guess side and structure would be clear to a medic or medical statistician familiar with this kind of data, but I don't qualify as either.

        Whether you should be showing more than means is an open question.

        Code:
        label var cerebellum_b "bilateral"
        label var cerebellum_l "left"
        label var cerebellum_r "right"
        statplot cerebellum_? , over(patientgroup) asyvars recast(dot) exclude0 linetype(line) lines(lw(vthin)) ysc(alt) ytitle(Means of cerebellum volume (units) by patient group) marker(1, msize(large) ms(Oh)) marker(2, msize(large) ms(+))
        Click image for larger version

Name:	cerebellum1.png
Views:	1
Size:	20.5 KB
ID:	1719341

        Last edited by Nick Cox; 04 Jul 2023, 09:45.

        Comment


        • #5
          Thank you for the recommendation, Nick. I will certainly try that with the other structures.

          I've included below an example of what I originally had in mind, where HC, WML-VCIND, and WML-VAD would be the equivalent of my patientgroup. I had also wanted to group each structure together (so ATR.L and ATR.R in the example below would labelled as Left and Right, closer together, over an ATR label), but perhaps this isn't something that can be done easily. Thanks again for your input!


          Click image for larger version

Name:	eg.png
Views:	1
Size:	38.9 KB
ID:	1719422

          Comment


          • #6
            Bars that don't start at zero occasionally can be defended, but I've never heard of a rationale for bars starting at 4.30. That was a point I previously made.

            Otherwise we are also, I regret, at cross-purposes because I can't gather anything new from your graph in #5 that bears on your question.

            statplot, recast(dot) is a wrapper for graph dot with a different data structure.

            Comment

            Working...
            X