Announcement

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

  • Overlapping labels of pie chart

    Dear Statalisters,

    I have to report several pie charts in a study. However, I face with the problem of overlapping labels. How can I add line connectors to the pie chart which are outside the pie chart but connects the shares? I am attaching an example of the problem. Thank you in advance.


    Sincerely,
    Attached Files

  • #2
    Please note our requests to give a data example, to show the code you used, and to present graph attachments as .png not .gph -- all explicit at https://www.statalist.org/forums/help#stata.

    I read off the following data. No doubt the text labels could be made more informative. For example, "coal imp" is perhaps coal imports and "asph" may mean "asphalt".

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str11 source float percent
    "renewable" 41.91
    "natural gas" 18.9
    "lignite" 16.09
    "coal imp" 20.8
    "fuel oil" .3191
    "asph coal" .8003
    "coal" 1.178
    end
    It's hard to think of any advantage to pie charts beyond their being widely familiar. The message that components add to 100% is no doubt educational or reassuring for young children, but not otherwise.

    Any problem with placement of labels is solved completely by using a bar chart -- which also does away with the awkward legend. Several small choices remain. For example, for graphics that have table flavour, I often like to put the magnitude axis at the top, but that is personal taste (more on that at https://www.stata-journal.com/articl...article=gr0053).

    The code here is just one of many similar choices. I would for example suggest standardising on 1 or 2 decimal places.

    Code:
    graph hbar (asis) percent, over(source, sort(1) descending) scheme(s1color) blabel(bar) bar(1, lcolor(red) fcolor(red*0.2)) ysc(r(0 44) alt) ytitle(percent of total)
    Click image for larger version

Name:	energy.png
Views:	1
Size:	24.6 KB
ID:	1588236

    Last edited by Nick Cox; 04 Jan 2021, 08:56.

    Comment


    • #3
      How to make a graph similar to this:
      Click image for larger version

Name:	Captura.PNG
Views:	1
Size:	48.7 KB
ID:	1647250


      This is what I have progressed:
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte d_ipcm float ipcm
       1 142.46667
       2  252.4869
       3  344.0508
       4  432.8683
       5  531.2717
       6  651.0728
       7  801.3522
       8 1011.8083
       9 1369.0513
      10  2835.451
      end
      
      
      
      gen pc=.
      sum ipcm
      return list  
      replace pc=(ipcm/`r(sum)')*100
      drop ipcm
      replace pc=round(pc, 0.1)
      gen spc =string(pc)
      
      replace spc = subinstr(spc, ".", ",", .)
      replace spc = subinstr(spc, "-", "", .)
      replace spc=spc+",0" if ~strpos(spc,",")
      replace spc ="0" + spc  if (pc>0 & pc<1)
      replace spc ="0" + spc  if (pc<0 & pc>-1)
      replace spc =spc + "%"
      
      gen var=""
      local la "I II III IV V VI VII VIII IX X"
      forvalues n = 1/10 {
       local nla: word `n' of `la'
       replace var="`nla'" if d_ipcm==`n'
       }
      
      expand 2  /*duplicar observaciones*/
      bysort d_ipcm: gen lgroup = _n
      gen r=1
      gen str20 s = cond(mi(r),var,var + " " + spc) if lgroup == 1
      Last edited by Andrés Lahur Talavera Cuya; 28 Jan 2022, 19:07.

      Comment

      Working...
      X