Announcement

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

  • Graph Bar: custom bar colours for stacked bar charts per bar "segment"

    Dear Statalist,

    I am just wondering how to set custom colours (and/or other formatting) for each "segment" that comprises a stacked bar chart. I know these can be manually edited in the graph editor by altering the "object specific properties", however this is tedious to do for multiple instances. I was wondering whether the is a more automated way of doing this?

    An example of the type of chart is the following (with what I would like to achieve):

    Click image for larger version

Name:	chart example.png
Views:	1
Size:	37.8 KB
ID:	1467368


    I've tried the following approach in which I generate a variable colour containing the colour in an RGB format, looping through all of the bar segments and then sending this to the bar chart. However this does not work as the colours are changed for all elements belong to each over(y) category.

    Code:
    local n = _N
    local bars = ""
    
    forvalues i = 1/92 {
        local barcol = colour[`i']
        local recol = "bar(`i', fcolor(" + `"`barcol'"' + "))"
        local bars = `"`bars'"' + " " + `"`recol'"'
    }
    
    graph bar x, over(y) stack asyvars over(z) `bars'
    Is this possible?

    For example, in this previous Statalist discussion on customising bar labels, the internal graphing classes/macros were accessed and edited to achieve this. There must be similar objects storing the bar colours for each "rectangle" in the chart, which can be accessed and modified in a similar way.

    Thank you in advance for any assistance.

    Best wishes,
    Alex

  • #2
    See #2 of the following link for your immediate problem, but Nick Cox summarizes in #3 the cons of stacked bar charts and offers an alternative which addresses some of these shortcomings.

    https://www.statalist.org/forums/for...cked-bar-chart

    Comment


    • #3
      Naturally I agree with Andrew. But -- if you're committed or compelled otherwise -- at least avoid arbitrary colours and put the legend in a column so that 7 is at the top and 1 at the bottom.

      Comment


      • #4
        Both, many thanks for your helpful comments. I will look into your suggestions.

        Comment

        Working...
        X