Announcement

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

  • Custom labels for violin plots

    I am attempting to create a violin plot (edit: using vioplot command)of four different continuous variables. The variable labels are quite lengthy, and when the graph is created, they overlap each other and are unreadable. I can go into the graph editor and change them by going to: Graph > xaxis1 > axis properties > edit or add individual ticks and labels. I would prefer to do this via a script/do-file as opposed to point-and-click.


    I have searched high and low through the documentation for a way to change the labels, but I cannot seem to find it.

    Any help would be appreciated.
    Last edited by Perry Schlanger; 03 Dec 2021, 09:50.

  • #2
    Not being familiar with this particular plot command, you can relabel the variables prior to plotting.

    Code:
    label var myvar "my short label"
    ...
    make your plot

    Comment


    • #3
      Leonardo's approach is the most straightforward, as I expect all will agree after seeing the following example of changing the labels as part of the vioplot command using the standard option for specifying labels on a twoway graph. The key insight is that the x axis runs from 0.5 to 1.5 and if there are k variables, the violins are plotted at x = 0.5 + 1/(k+1) ... 0.5 + k/(k+1).
      Code:
      // make 3 variables to plot
      set obs 100
      set seed 666
      generate v1 = rnormal(50,10)
      generate v2 = rnormal(70,5)
      generate v3 = rnormal(90,20)
      local l1 = "V One"
      local l2 = "V Two"
      local l3 = "V Three"
      
      // plot 3 variables
      local k 3
      local xl
      forvalues v = 1/`k' {
          local tic = 0.5 + `v'/(`k'+1)
          local xl `xl' `tic' "`l`v''"
      }
      macro list _xl
      vioplot v1 v2 v3, xlabel(`xl') name(v3)
      
      // plot 2 variables
      local k 2
      local xl
      forvalues v = 1/`k' {
          local tic = 0.5 + `v'/(`k'+1)
          local xl `xl' `tic' "`l`v''"
      }
      macro list _xl
      vioplot v1 v2, xlabel(`xl') name(v2)

      Comment

      Working...
      X