Announcement

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

  • graph dot command changing labels

    Hello everyone,

    I was using the graph dot command to generate a graph with the means of several ordinal Variables:

    graph dot (mean) tg53121 (mean) tg53122 (mean) tg53123 (mean) tg53111 (mean) ///
    tg53112 (mean) tg53113 (mean) tg53114 (mean) tg53211 (mean) tg53212 (mean) tg53213, ///
    exclude0 ascategory asyvars yscale(range(1(1) 5)) ylabel(1 2 3 4 5)

    This gives me the following output:

    Graph.gph

    As you can (hopefully) see, all my variables (tg53...) are listed on the y-axis. Now, I want to replace the label and have something more meaningful instead. For example, instead of tg53121 it should way "enjoying long walks".
    Unfortunately I was sofar unable to find a command that did this for me (and yes, I did spend all my working day on this so far). Can you tell me the command necessary to alterate the labels (although recommended in many posts, legend, label, xlabel ... didn't do it)?

    Thank you a lot,
    Anne.

  • #2
    Kathrin/Anne: Please post Stata graphs as .png as explained at https://www.statalist.org/forums/help#stata 12.4

    Comment


    • #3
      Sorry for that. Here it is:
      Attached Files

      Comment


      • #4
        Click image for larger version

Name:	image_10496.png
Views:	1
Size:	20.1 KB
ID:	1438727
        I don't see a data example here: please do note FAQ Advice #12. In your case you presumably have variable labels defiined already.

        This may help:

        You're using the default scheme s2color. Fine, if it is to your taste. I find with students and colleagues that its blue backdrop clashes with graphics they produce with other software. Similarly, the default dotted grid lines with graph dot often don't copy over well to other software.

        Code:
        clear
        set scheme s1color
        set obs 10
        set seed 1952
        forval v = 111/114  {
            gen tg53`v' = runiformint(1, 5)
        }
        
        label var tg53111 "Stata"
        label var tg53112 "SAS"
        label var tg53113 "SPSS"
        label var tg53114 "R"
        
        foreach v of var tg* {
            gen label`v' = "`: variable label `v''"
        }
        
        gen id = _n
        reshape long tg labeltg , i(id) j(which)
        
        graph dot tg, over(labeltg, sort(1) descending) linetype(line) lines(lc(gs12) lw(vthin))


        Comment


        • #5
          Here are two other ways to do it. I think the first is better. It goes against the grain somewhat to put a constant in a variable, but it works. I have to think the second is much, much better. You must install statplot (SSC) first.

          Code:
          clear
          set obs 10
          set seed 1952
          forval v = 111/114  {
              gen tg53`v' = runiformint(1, 5)
          }
          
          label var tg53111 "Stata"
          label var tg53112 "SAS"
          label var tg53113 "SPSS"
          label var tg53114 "R"
          
          * alternative 1: start here with other data 
          foreach v of var tg* {
             egen mean`v' = mean(`v')
             label var mean`v' "`: var label `v''"
          }
          
          graph dot (asis) mean* in 1 , ascategory linetype(line) lines(lc(gs12) lw(vthin))
          
          * alternative 2: 
          ssc inst statplot 
          statplot tg53*, recast(dot) linetype(line) lines(lc(gs12) lw(vthin))
          Last edited by Nick Cox; 11 Apr 2018, 04:55.

          Comment


          • #6
            Dear Nick,
            thank you so much for those solutions, they are very elegant. What troubles me though, is that in applying them I would need to "officially" assign new labels to the variables (as happend with the command "label var tg53111 "Stata" " and so on).

            What my colleague and I experimented with yesterday evening was this:

            graph dot (mean) tg53121 (mean) tg53122 (mean) tg53123 (mean) tg53111 (mean) ///
            tg53112 (mean) tg53113 (mean) tg53114 (mean) tg53211 (mean) tg53212 (mean) tg53213, ///
            exclude0 ascategory asyvars yscale(range(1(1) 4)) ylabel(1 2 3 4) ///
            yvaroptions(relabel(1"Kontakte zu Kommilitonen aufgebaut" 2"mit Kommilitonen austauschen" 3"viele Kontakte zu Kommilitonen" ///
            4"von Lehrenden anerkannt" 5"mit Lehrenden zurechtkommen" 6"faire Behandlung durch Lehrende" 7"Lehrende sind interessiert" ///
            8"Leistungen besser als erwartet" 9"Leistungserwartungen erfüllt" 10"mit Leistungen zufrieden")) ///
            ylabel(1"trifftnichtzu" 2 3 4"trifftvölligzu")

            --> I used the "yvaroptins" with the "relabel"-command and then the "ylabel"-command to change the numbers on the x-axis.
            --> I was really satisfied with that result, BUT for one thing: If you look at the last line of the command, you can see that it says "trifftnichtzu". In English that would be "doesnotapply". Ideally, this would be written in separate words as in "does not apply", but this is not possible.
            When I try to execute the following:

            graph dot (mean) tg53121 (mean) tg53122 (mean) tg53123 (mean) tg53111 (mean) ///
            tg53112 (mean) tg53113 (mean) tg53114 (mean) tg53211 (mean) tg53212 (mean) tg53213, ///
            exclude0 ascategory asyvars yscale(range(1(1) 4)) ylabel(1 2 3 4) ///
            yvaroptions(relabel(1"Kontakte zu Kommilitonen aufgebaut" 2"mit Kommilitonen austauschen" 3"viele Kontakte zu Kommilitonen" ///
            4"von Lehrenden anerkannt" 5"mit Lehrenden zurechtkommen" 6"faire Behandlung durch Lehrende" 7"Lehrende sind interessiert" ///
            8"Leistungen besser als erwartet" 9"Leistungserwartungen erfüllt" 10"mit Leistungen zufrieden")) ///
            ylabel(1"trifft nicht zu" 2 3 4"trifft völlig zu")

            ...stata gives me this:

            type mismatch
            r(109);

            Do you have any idea why that happens? This is even more puzzling to me as spaces between the words are no problem for the text on the y-axis:

            Click image for larger version

Name:	Graph_updated.png
Views:	1
Size:	54.3 KB
ID:	1438955
            Again, thank you very much for your help,
            Anne.

            PS: I am sorry for not using the dataex-command but I was unable to execute it. I did indeed read the FAQ. And I am again sorry for not putting my command into one of those nice grey boxes.

            Comment


            • #7
              Sorry; no further ideas. I don't understand your reluctance to use a simpler solution.

              Comment


              • #8
                Dear Nick,
                whether one or the other solution is simpler or not is a matter of perspective, I would say. People think very differently and they also approach problems differently. For me it is much more trouble to change the labels of all the variables I want to use instead of putting a temporary label on them in the graph. And besides that I do think that my question is still relevant, because it remains unclear why Stata does not accept spaces in between the labels on the x-axis. That's in fact a problem that doesn't even touch the issue of who's syntax is more elaborate (both of us know the answer to that question already ^^).

                Comment


                • #9
                  Sure. But variable labels are labile and under your control. Also, you can easily clone variables and have different versions for different purposes. Sometimes you need to tune variable labels for graphics, as here: either too cryptic or too long sometimes, and you can't win.

                  See clonevar

                  When I said that I have no further ideas, that included not understanding the error message in #6. I did look at your code for a while and couldn't see what was wrong. As I'd offered a different solution, sorry, but I didn't feel interested in trying to work out what was wrong. I don't have example data from you in any case. I am not StataCorp and I do show caprice in getting interested or not in pursuing different threads.

                  You remain interested in finding the bug. Understood. So, you can do two things at least.

                  1. Post example data so that people can reproduce the problem and try to diagnose it. FAQ Advice #12 applies as always.

                  2. Send your dataset and code to StataCorp Technical Services.

                  Comment


                  • #10

                    Dear Nick,
                    whether one or the other solution is simpler or not is a matter of perspective, I would say. People think very differently and they also approach problems differently. For me it is much more trouble to change the labels of all the variables I want to use instead of putting a temporary label on them in the graph. And besides that I do think that my question is still relevant, because it remains unclear why Stata does not accept spaces in between the labels on the x-axis. That's in fact a problem that doesn't even touch the issue of who's syntax is more elaborate (both of us know the answer to that question already ^^).
                    I think that Nick has been more than generous here going out of his way to create a data example. In a number of cases, threads of this type without data examples go unanswered. Second, I do not understand how is it more work to label your variables compared to relabeling them on the graph command. Anyway, I see two problems with your code


                    graph dot (mean) tg53121 (mean) tg53122 (mean) tg53123 (mean) tg53111 (mean) ///
                    tg53112 (mean) tg53113 (mean) tg53114 (mean) tg53211 (mean) tg53212 (mean) tg53213, ///
                    exclude0 ascategory asyvars yscale(range(1(1) 4)) ylabel(1 2 3 4) ///
                    yvaroptions(relabel(1"Kontakte zu Kommilitonen aufgebaut" 2"mit Kommilitonen austauschen" 3"viele Kontakte zu Kommilitonen" ///
                    4"von Lehrenden anerkannt" 5"mit Lehrenden zurechtkommen" 6"faire Behandlung durch Lehrende" 7"Lehrende sind interessiert" ///
                    8"Leistungen besser als erwartet" 9"Leistungserwartungen erfüllt" 10"mit Leistungen zufrieden")) ///
                    ylabel(1"trifft nicht zu" 2 3 4"trifft völlig zu")

                    ...stata gives me this:

                    type mismatch
                    r(109);

                    1. You have two ylabel() options specified.
                    2. You need to include a space after the first ylabel value and before the text, i.e., ylabel(1 "trifft nicht zu" 2 3 4"trifft völlig zu")

                    Comment


                    • #11
                      Andrew Musau The two ylabel() options aren't a problem. The second overwrites the first. Also, the graph in #6 showed that the second worked.

                      Comment


                      • #12
                        Thanks Nick, A data example illustrating the problem is needed as you note in #9

                        Comment


                        • #13
                          Andrew Musau was right -- and the graph example in #6 must have been produced differently. This works when spaces are introduced between numbers and labels.


                          Code:
                          clear
                          set obs 10 
                          set seed 2803 
                          
                          foreach s of num 121/123 111/114 211/213 { 
                             gen tg53`s' = runiformint(1,4)
                          }
                           
                          graph dot (mean) tg53121 (mean) tg53122 (mean) tg53123 (mean) tg53111 (mean) ///
                          tg53112 (mean) tg53113 (mean) tg53114 (mean) tg53211 (mean) tg53212 (mean) tg53213, ///
                          exclude0 ascategory asyvars yscale(range(1(1) 4)) ylabel(1 2 3 4) ///
                          yvaroptions(relabel(1 "Kontakte zu Kommilitonen aufgebaut" 2 "mit Kommilitonen austauschen" 3 "viele Kontakte zu Kommilitonen" ///
                          4 "von Lehrenden anerkannt" 5 "mit Lehrenden zurechtkommen" 6 "faire Behandlung durch Lehrende" 7 "Lehrende sind interessiert" ///
                          8 "Leistungen besser als erwartet" 9 "Leistungserwartungen erfüllt" 10 "mit Leistungen zufrieden")) ///
                          ylabel(1 "trifft nicht zu" 2 3 4 "trifft völlig zu")

                          Comment

                          Working...
                          X