Announcement

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

  • Pie chart

    Hi! I need help with changing the position on the numbers in my pie chart. Preferable could one of the smaller values be moved to the left. Is this possible?
    Attached Files

  • #2
    I guess you need to enter the Graph Editor and drag some text where you prefer.

    I'd recommend a bar chart instead unless someone in power over you is insisting on this.

    Interesting to me that the words are the same in Swedish, Norwegian and Danish. At least that is what I gather from Google translate.

    If these are the answers, what is the question?

    Comment


    • #3
      Yes David, it is certainly possible but it requires kitchen sink coding that most Stata users rather not resort to, that is by using the Graph Editor option from within the graph window.
      The reason why most users would not do this is that it takes some effort and a more deep understanding of how the Editor 'works' to get your result - and not just that - with code as to be able to replicate it without again using the Graph Editor option.

      You did not provide replication data yourself as per FAQ 12.2 What to say about your data, so I provide some code to explain how this works:
      Code:
      clear
      input Ney VarkenEller Ja
      2.54 1.141 96.32
       end
      
      graph pie Ney VarkenEller Ja, plabel(_all per)
      which produces the same plot as yours above.

      Now, we use the Graphics window option File and trigger the option Start Graph Editor, like:
      Next, to be able to save and retrieve the internal code of the Graph Editor (which tracks our behavior) we have to trigger the recording button (red filled circle) which has at this point the tooltip Start Recording, like:
      Click image for larger version

Name:	PIE_2_Editor1_2_Start_Editor.jpg
Views:	1
Size:	65.9 KB
ID:	1775294





      Next you have to select each label separately of the pie segments that you want to change, and move them to the desired position, like:
      Click image for larger version

Name:	PIE_2_Editor3_Move_labels.jpg
Views:	1
Size:	37.1 KB
ID:	1775291



      But, do note that each time that you stop the movement and start moving that label again, that it then will be recorded by the Editor with a new code line (more on that below).

      When you are satisfied with the editing then you stop the recoding by a second trigger of the recording button (red filled circle) which has at this point the tooltip text End recording, like:
      Click image for larger version

Name:	PIE_2_Editor4_End_recording.jpg
Views:	1
Size:	25.6 KB
ID:	1775292





      Stata will prompt you to select a folder and provide a name for the *.grec file, in which all your graph editing actions are recorded, like (on my system):
      After using the button Save you also have to save your editing of the graph itself, like:
      Click image for larger version

Name:	PIE_2_Editor5_6_Save_recording.jpg
Views:	1
Size:	89.1 KB
ID:	1775293





      Because if you do not do that, then you will not keep these changes, which now looks like:
      Click image for larger version

Name:	PIE_2.jpg
Views:	1
Size:	28.5 KB
ID:	1775295




      Now, to be able to (later) replicate all of the above without the heavy lifting, you should load the saved *.grec file in your editor.
      My file looks like:
      Code:
      StataFileTM:00001:01100:GREC:                          :
      00008:00008:00001:
      *! classname: piegraph_g
      *! family: bar
      *! date:  3 Apr 2025
      *! time: 14:29:56
      *! graph_scheme: stcolor
      *! naturallywhite: 1
      *! end
      
      // File created by Graph Editor Recorder.
      // Edit only if you know what you are doing.
      
      .plotregion1.pielabel[2].DragBy 18.0820188986997 7.973173687615625
      // pielabel[2] reposition
      
      .plotregion1.pielabel[1].DragBy 18.65153130495792 .8542686093873487
      // pielabel[1] reposition
      
      .plotregion1.pielabel[1].DragBy -.2847562031290757 .8542686093873487
      // pielabel[1] reposition
      
      
      // <end>
      You can first (again) create the Pie plot and load the *.grec file in the editor and run it.
      That will replicate the changes.

      But, we can do better by carefully picking the code lines that we need, and use them with the command gr_edit, like:
      Code:
      graph pie Ney VarkenEller Ja, plabel(_all per)
      gr_edit .plotregion1.pielabel[2].DragBy 18.08 7.97
      gr_edit .plotregion1.pielabel[1].DragBy 18.65 .85
      gr_edit .plotregion1.pielabel[1].DragBy -.28 .85
      graph export "PIE_2.jpg", width(1000) replace
      Note that I abbreviated the x and y coordinates after the second decimal which usually suffices.
      But, again we can do better.
      Note that I moved the first pielable[1] twice.
      Where necessary we can simply add/substract the coordinates to revise these two lines into one line of code:
      Code:
      graph pie Ney VarkenEller Ja, plabel(_all per)
      gr_edit .plotregion1.pielabel[2].DragBy 18.08 7.97
      gr_edit .plotregion1.pielabel[1].DragBy 18.37 .85
      Either way, this code incorporating the Graph Editors record of your editing will replicate the graph with its two labels repositioned.
      Last edited by ericmelse; 03 Apr 2025, 07:26.
      http://publicationslist.org/eric.melse

      Comment


      • #4
        An alternative approach would be to use the ptext(...) option in graph pie.

        With your example's small number of pie slices and a little trial-and-error this would seem a reasonable approach. Perhaps not recommended with a large number of slices.

        For example:
        Code:
         graph pie Ney VarkenEller Ja, ptext(87 52 "2.54%", size(*.8)) ptext(75 53 "1.41%", size(*.8)) ptext(270 15 "96.32%", size(*.8)) scheme(s2color) leg(rows(1))

        Comment

        Working...
        X