Announcement

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

  • Individual line colours in pcspike

    Hello,

    I'm trying to create a parallel axis dot plot where each line has a different colour (I've managed to get everything else I want). The current one I have built has all the lines as one colour, navy (I must admit I usually don't favour multiple colours as they can become confusing, but in this case there aren't excessive observations). I've attached an example of my dataset, the code I've been using to generate the plot and a .png attachment of what it looks like so far.

    I'm using Stata version 16.1 on macOS Big Sur version 11.2.3.

    This is an example of my dataset

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double F byte CaN double G byte CaoN
     3.4 0 10.4 1
     5.8 0   18 1
     5.8 0 15.1 1
     6.4 0 20.4 1
       8 0 13.3 1
    12.5 0   17 1
    13.5 0 18.3 1
     7.1 0 18.3 1
    17.8 0   22 1
     2.8 0 20.9 1
       6 0 12.9 1
    13.3 0 27.1 1
     9.6 0 23.2 1
     4.3 0 11.8 1
     9.5 0 16.8 1
       2 0  8.4 1
     4.7 0 12.8 1
     6.4 0 15.9 1
     2.9 0   24 1
       3 0 13.4 1
     7.1 0 31.5 1
     6.2 0 24.7 1
     2.1 0   26 1
     4.6 0 19.7 1
     2.2 0 41.4 1
     1.9 0 14.5 1
    11.2 0 18.3 1
    13.3 0 20.4 1
     4.3 0 13.6 1
    12.7 0   16 1
    end
    The code I have used to generate my plot is below:

    Code:
    twoway pcspike F CaN G CaoN, title(Parallel axis dot plot) || scatter F CaN, msym(O) pstyle(p2) || scatter G CaoN, msym(O) pstyle(p4) ytitle(EGM Amplitude (mV))  xla( ""  "",notick) legend(order(2 "Cathodal-anodal capture" 3 "Cathodal or anodal capture"))
    Thank you very much for your help,

    Don
    Attached Files

  • #2
    I count 30 different individuals, so if this is an example, how many in your main dataset, or in other groups? How will 30 (or whatever other number of) different colours really help?

    There are some comments in https://www.stata-journal.com/articl...article=gr0041 What seemed especially helpful in examples I tried was to separate out different subsets and/or to separate out changes that were positive and negative (all positive in your example).

    In 2021 I would add a mention of Tufte's term slope graph or slopegraph, and debunk the idea spread by others that he invented this plot.

    Comment


    • #3
      but in this case there aren't excessive observations
      More than 5 lines to me is excessive. I would not recommend this for your graph, but here is how to do it.

      Code:
      sysuse nlswide1
      levelsof occ, local(occupations)
      local command
      local legend
      foreach num of numlist `occupations'{
          local command "`command' (pcspike wage68 ttl_exp68 wage88 ttl_exp8 if occ==`num')"
          local legend "`legend'  `num' "`:label (occ) `num''" "
      }
      
      set scheme s1color
      tw `command', leg(order(`legend'))
      Res.:

      Click image for larger version

Name:	Graph.png
Views:	1
Size:	101.9 KB
ID:	1606956

      Comment


      • #4
        You may consider a subset plot (using -subsetplot- from SSC, created by Nick Cox), and also discussed by Nick in Speaking Stata: Graping subsets from the Stata Journal. This approach can work with not too many subsets to sensibly visualize the spaghetti.

        Code:
        sysuse nlswide1
        levelsof occ, local(occupations)
        subsetplot pcspike wage68 ttl_exp68 wage88 ttl_exp8, by(occ) lcol(gs12) subset(lcol(red))
        Click image for larger version

Name:	subsetplot.jpg
Views:	1
Size:	107.1 KB
ID:	1607030

        Comment


        • #5
          Thank you Nick, Andrew and Leonardo for your comments and help.

          I just had a few follow up questions:

          Nick:

          Thanks for the post to that article, I found it very helpful. One thing that I'm not doing right is that I'm having to remove the axis ticks manually in the graph editor, I've added the 'noixtick' code, but it doesn't seem to be working. Have I put it in the incorrect place or am I mistaken to its function?

          Code:
          destring, replace
          
          
          twoway pcspike F CaN G CaoN || scatter F CaN, msym(O) pstyle(p2) || scatter G CaoN, msym(O) pstyle(p4) ytitle(EGM Amplitude (mV)) legend(order(2 "Cathodal-anodal capture" 3 "Cathodal or anodal capture")) by(Id, note("") noixtick)
          Andrew:

          Thank you for the help with the individual colours. I think I agree now that I have too many observations to make individual colours additive. However, I did want to make the code work for me for future reference. Unfortunately, I'm having trouble getting the legends, axis and graph titles to work for me.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input byte Id double F byte CaN double G byte CaoN
           1  3.4 0 10.4 1
           2  5.8 0   18 1
           3  5.8 0 15.1 1
           4  6.4 0 20.4 1
           5    8 0 13.3 1
           6 12.5 0   17 1
           7 13.5 0 18.3 1
           8  7.1 0 18.3 1
           9 17.8 0   22 1
          10  2.8 0 20.9 1
          11    6 0 12.9 1
          12 13.3 0 27.1 1
          13  9.6 0 23.2 1
          14  4.3 0 11.8 1
          15  9.5 0 16.8 1
          16    2 0  8.4 1
          17  4.7 0 12.8 1
          18  6.4 0 15.9 1
          19  2.9 0   24 1
          20    3 0 13.4 1
          21  7.1 0 31.5 1
          22  6.2 0 24.7 1
          23  2.1 0   26 1
          24  4.6 0 19.7 1
          25  2.2 0 41.4 1
          26  1.9 0 14.5 1
          27 11.2 0 18.3 1
          28 13.3 0 20.4 1
          29  4.3 0 13.6 1
          30 12.7 0   16 1
          end

          Code:
          levelsof Id, local(EGM)
          local command
          foreach num of numlist `EGM'{
              local command "`command' (pcspike F CaN G CaoN if Id==`num')"
              
          }
          
          set scheme s2color
          tw `command'
          
          *I'd like to insert the following
          
          ytitle(EGM Amplitude (mV))  xla( ""  "",notick) legend(order(2 "Cathodal-anodal capture" 3 "Cathodal or anodal capture"))
          Also, is it possible to add the small dots to the ends of the lines that come with the usual pcspike parallel axis dot plots?

          Thank you,

          Don

          Comment


          • #6
            Thanks for the renewed data example. In terms of a question you asked and some other ideas:

            1. noixtick works well for me. Which version of Stata are you using? If removing ticks is hard, then setting their length to zero or their colour to none can help. See also https://journals.sagepub.com/doi/abs...36867X19874264

            2. If the identifiers are arbitrary, then you might as well sort on something more interesting, say the initial or final value, or the change. More at https://www.statalist.org/forums/for...e-or-graph-use

            3. An xlabel at 0.5 would seem to have no interest or value.

            This was the code I ran:

            Code:
            gen change = G - F 
            * ssc install myaxis: see reference in #2 above 
            myaxis Order=Id, sort(mean change) descending 
            twoway pcspike F CaN G CaoN || scatter F CaN, msym(O) pstyle(p2) || scatter G CaoN, msym(O) pstyle(p4) ytitle(EGM Amplitude (mV)) legend(order(2 "Cathodal-anodal capture" 3 "Cathodal or anodal capture")) by(Order, note("") noixtick) xla(0 1)
            See also https://www.stata-journal.com/sjpdf....iclenum=gr0023

            Comment


            • #7
              Andrew:

              Thank you for the help with the individual colours. I think I agree now that I have too many observations to make individual colours additive. However, I did want to make the code work for me for future reference. Unfortunately, I'm having trouble getting the legends, axis and graph titles to work for me.
              I do not think you get anything close to 32 colors. For illustration, I use the first 10 observations.

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input byte Id double F byte CaN double G byte CaoN
               1  3.4 0 10.4 1
               2  5.8 0   18 1
               3  5.8 0 15.1 1
               4  6.4 0 20.4 1
               5    8 0 13.3 1
               6 12.5 0   17 1
               7 13.5 0 18.3 1
               8  7.1 0 18.3 1
               9 17.8 0   22 1
              10  2.8 0 20.9 1
              11    6 0 12.9 1
              12 13.3 0 27.1 1
              13  9.6 0 23.2 1
              14  4.3 0 11.8 1
              15  9.5 0 16.8 1
              16    2 0  8.4 1
              17  4.7 0 12.8 1
              18  6.4 0 15.9 1
              19  2.9 0   24 1
              20    3 0 13.4 1
              21  7.1 0 31.5 1
              22  6.2 0 24.7 1
              23  2.1 0   26 1
              24  4.6 0 19.7 1
              25  2.2 0 41.4 1
              26  1.9 0 14.5 1
              27 11.2 0 18.3 1
              28 13.3 0 20.4 1
              29  4.3 0 13.6 1
              30 12.7 0   16 1
              end
              
              levelsof Id if Id<=10, local(EGM)
              local command
              local legend
              foreach num of numlist `EGM'{
                  local command "`command' (pcspike F CaN G CaoN if Id==`num')"
                  local legend "`legend'  `num' "`num'" "
              }
              
              set scheme s2color
              tw `command', legend(order(`legend'))
              
              tw  (scatter F CaN if Id<=10, msym(O) pstyle(p2)) ///
              (scatter G CaoN if Id<=10, msym(O) pstyle(p4) ///
              ytitle(EGM Amplitude (mV))  xla( ""  "",notick) ///
              legend(order(1 "Cathodal-anodal capture" 2 ///
              "Cathodal or anodal capture"))) `command'
              Res.:
              Click image for larger version

Name:	Graph2.png
Views:	1
Size:	52.8 KB
ID:	1607256



              Click image for larger version

Name:	Graph.png
Views:	1
Size:	57.3 KB
ID:	1607258

              Comment


              • #8
                Thank you both for your help

                In answer to your question Nick, I'm using version 16.1. Thank you for the links, but I'm afraid when I run my code (or yours), the ticks are still there. I set the colour to background as well (and tried setting the tlength to 0), but to no avail. It's not a big issue for this project as I can manually delete the ticks, just annoying.

                Code:
                 twoway pcspike F CaN G CaoN || scatter F CaN, msym(O) pstyle(p2) || scatter G CaoN, msym(O) pstyle(p4) ytitle(EGM Amplitude (mV)) legend(order(2 "Cathodal-anodal capture" 3 "Cathodal or anodal capture")) by(Order, note("")) 
                 xlabel(tlength(0))
                Code:
                 twoway pcspike F CaN G CaoN || scatter F CaN, msym(O) pstyle(p2) || scatter G CaoN, msym(O) pstyle(p4) ytitle(EGM Amplitude (mV)) legend(order(2 "Cathodal-anodal capture" 3 "Cathodal or anodal capture")) by(Order, note("")) 
                 xla( "" "", noticks)
                Code:
                 twoway pcspike F CaN G CaoN || scatter F CaN, msym(O) pstyle(p2) || scatter G CaoN, msym(O) pstyle(p4) ytitle(EGM Amplitude (mV)) legend(order(2 "Cathodal-anodal capture" 3 "Cathodal or anodal capture")) by(Order, note("")) 
                 xlabel(tlcolor(bg))

                Comment


                • #9
                  That's odd. In Stata 16.1 noixtick appears to suppress the ticks. If I leave that out they come back.. If I then set the length to zero, they disappear.

                  Code:
                  twoway pcspike F CaN G CaoN || scatter F CaN, msym(O) pstyle(p2) || scatter G CaoN, msym(O) pstyle(p4) ytitle(EGM Amplitude (mV)) legend(order(2 "Cathodal-anodal capture" 3 "Cathodal or anodal capture")) by(Id, note("")) xla(0 1, tlength(0))

                  Comment

                  Working...
                  X