Announcement

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

  • Adding multiple plots in one graph

    Hello,

    I have a few different graphs which I would like to display in one. I found the combine function, which is not helpful since the goal is to plot the lines in the same graph. Another helpful function is the addplot function, however it is not working out for me.

    Because I am new to Stata I use the graphics toolbar instead of trying to type the code myself. Using STATA 14 I don't see the option of adding plots together in this toolbar. The following three graphs are created in Stata:

    twoway (line m_currentrate_2 nloandate, sort lcolor(blue) lwidth(medthick)) if originator=="X1" , ytitle(Average monthly) xtitle(Date) title(Average monthly) subtitle(X1) graphregion(fcolor(white))

    twoway (line m_currentrate_2 nloandate, sort lcolor(green) lwidth(medthick)) if originator=="X2" , ytitle(Average monthly) xtitle(Date) title(Average monthly) subtitle(X2) graphregion(fcolor(white))

    twoway (line m_currentrate_2 nloandate, sort lcolor(red) lwidth(medthick)) if originator=="X3" , ytitle(Average monthly) xtitle(Date) title(Average monthly) subtitle(X3) graphregion(fcolor(white))

    Is there an option in the toolbar (within graphics) or another way to add these lines together in Stata?

    Thank you for your help,
    Danny

  • #2
    Lots of ways of doing this, but I have no precise idea what's available via the toolbar (dialogs?).

    Check out xtline for panel data.

    This solution won't be in your toolbar

    Here's one example using sepscatter (SSC).

    Code:
    webuse grunfeld, clear 
    set scheme s1color 
    ssc inst sepscatter
    sepscatter invest year if company <= 3 , sep(company) recast(line) ysc(log) ///
    lc(blue orange black) yla(1000 500 200 100 50, ang(h)) ///
    legend(pos(3) col(1)) xtitle("")

    Click image for larger version

Name:	3lines.png
Views:	1
Size:	11.2 KB
ID:	1336494

    Comment


    • #3
      Thank you Nick,

      However, if I use that code (with grundfeld) I get a few error messages. Looking into the sepscatter function it also seems that it is used for specific categories within a variable. Contrary to this, my goal is to add multiple existing graphs into one.

      My conditional variable is a string (in my code above this is displayed as if originator=="X1", if originator=="X2" etc..) and the idea is to add the different lines into one graph using the different tables that I created before.

      Is it possible to do such a thing with a string variable or do I need to create a variable where each name is replaced by a number and then use the sepscatter command?

      KR,
      Danny

      Comment


      • #4
        sepscatter is a command, not a function.

        Manifestly, the code ran for me and I don't know what you did differently. If you don't show your exact code or give a report more precise than "a few error messages" then unfortunately no diagnosis is possible.

        Contrary to your contrary, my understanding is that your problem resembles the problem I solved. String variables are not a problem.

        As a first approximation your syntax could be

        Code:
        sepscatter m_currentrate_2 nloandate, separate(originator) recast(line) ytitle(Average monthly) xtitle(Date) title(Average monthly)  graphregion(fcolor(white))
        If there are more than 3 categories in the variable originator you'd need to exclude what you didn't want

        I'd advise against using red and green together, as difficulty in distinguishing them is very common. Similarly I advise my students and collaborators against any axis title such as "Date".

        Comment


        • #5
          It is possible to display all 7 originators in one graph! The weird thing is that the lines look fine in the individual graphs, but they seem to go every direction (back and forth) in the sepscatter one. When the lines are "being drawn" you can see them going from the left to the right and back. This should not be possible considering that the observations are over time. How could it be that the individual graphs are fine, but together they display different patterns...

          Thank you for your time and help!

          Comment


          • #6
            sepscatter here calls up graph twoway line to do the work here, so standard advice applies.

            http://www.stata.com/help.cgi?graph_twoway_line explains:

            Do not forget to include the sort option when the data are not in the order of the x variable

            and clicking through links finds

            sort specifies that the data be sorted by the x variable before the points are connected. Unless you are after a special effect or your data are already sorted, do not forget to specify this option. If you are after a special effect, and if the data are not already sorted, you can specify sort(varlist) to specify exactly how the data should be sorted. Understand that specifying sort or sort(varlist) when it is not necessary will slow Stata down a little. You must specify sort if you wish to connect points, and you specify the twoway_option by() with total.

            If this isn't the solution, then please re-read http://www.statalist.org/forums/help#stata and post as much of your code, data and graph as will make the problem clear.
            Last edited by Nick Cox; 21 Apr 2016, 03:46.

            Comment


            • #7
              Solved the problem!

              Comment


              • #8
                Thanks for the good discussion this far! I have an additional question: if you combine graphs by using addplot, and you want to see the combination graphs from two subgroups, how should you use the by() -option? Example here:
                twoway scatter TMGLUC min,sort
                preserve
                collapse (mean) mean_T= TMGLUC (semean) se_T= TMGLUC (sd) sd_T= TMGLUC (count) n_T= TMGLUC (p95) p95_T= TMGLUC,by(min)
                gen lo= mean_T-1.96* se_T
                gen hi= mean_T+1.96* se_T
                graph addplot line mean_T lo hi min,sort legend(off)
                restore

                I, of course, would like to have both the scatter and the collapse -yieldings for both groups, let's say that the group was sex (male=1, female=2). So, where should I add the by(sex)?
                I can add it to the twoway -command, but that leaves the rest of the script not run.

                Thanks in advance!

                Mikko

                Comment


                • #9
                  Hi Nick,

                  I am using command sepscatter to plot my data as following,
                  Click image for larger version

Name:	capture.png
Views:	1
Size:	78.3 KB
ID:	1650922
                  and my codes are : sepscatter timeexdocu timeimdocu if year==2020, by(incomegroup) separate(sw) legend(pos(1) col(1) ring(0)) ysc(log) yla(300 150 50,ang(h))

                  now I want to exclude"Graphs by incomegroup" from the plot, and also I want to highlight one outlier ( see red circle with star note), but it not feasible using command mylabel, not sure if there is any guide that I can refer to.

                  Thanks.

                  Comment


                  • #10
                    Originally posted by Jane Quan View Post
                    Hi Nick,

                    I am using command sepscatter to plot my data as following,
                    [ATTACH=CONFIG]n1650922[/ATTACH]
                    and my codes are : sepscatter timeexdocu timeimdocu if year==2020, by(incomegroup) separate(sw) legend(pos(1) col(1) ring(0)) ysc(log) yla(300 150 50,ang(h))

                    now I want to exclude"Graphs by incomegroup" from the plot, and also I want to highlight one outlier ( see red circle with star note), but it not feasible using command mylabel, not sure if there is any guide that I can refer to.

                    Thanks.
                    I think I have figured out how to solve the problem.
                    firstly, to get rid of the "graphs by" , command ", note("")" can be used.
                    secondly, I used command scatterid to highlight outliers. but I used command twoway scatter instead of sepscatter in the first place.

                    Comment

                    Working...
                    X