Announcement

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

  • How to generate a combination chart

    hello,can you help me to generate a combination graph, thank you !

    Code:
    sysuse auto,clear
    graph matrix mpg weight length price headroom gear_ratio, half // generate a matrix graph
    pwcorr mpg weight length price headroom gear_ratio, star(.01) bonferroni // generate correlation coefficient matrix ,then combine them to one graph
    Through the above command, I have generated a graph and a table. Can the values of the correlation coefficients of the table be placed in the upper right part of the matrix graph? I know that R can do it, but I don’t know how to use stata to achieve this? The following is my target graph, the upper right part is the value of the correlation coefficient matrix, and the lower left part is the correlation matrix graph
    Last edited by fu gang; 01 Feb 2021, 08:06.

  • #2
    Click image for larger version

Name:	add values.png
Views:	1
Size:	171.7 KB
ID:	1592357
    Click image for larger version

Name:	wanted.png
Views:	1
Size:	72.7 KB
ID:	1592358
    target graph, combine the graph and the correlation coefficient matrix

    Comment


    • #3
      I made the corresponding graph through R, but ideally the lower left part is a scatter graph instead of a line graph. I wonder if this kind of graph can be experimented with stata?
      Any tips will be appreciated, Looking forward to your reply, Thank you very much!

      the target graph as follows:
      Click image for larger version

Name:	goal2.png
Views:	1
Size:	13.2 KB
ID:	1592434

      Comment


      • #4
        It's programmable in Stata. Some of the code you need is in corrtable from SSC, which shows the main idea, namely to loop over pairs of variables, produce separate graphs. and then bring them together with graph combine.

        Comment


        • #5
          Thank you very mchu for your reply, Can you elaborate more? I will try the corrtable command right away.

          Comment


          • #6
            I am afraid that it is hard to elaborate helpfully without writing about 100 lines of code. My point is not so much to try corrtable as to look at its code, because corrtable really doesn't do more than some of what you do, although starting with it may be easier than starting without it.

            I don't know what your lower diagonal graphs show.

            Comment


            • #7
              thank you for your reply, my lower diagnal graphs show the Scatter plot

              Code:
               
               graph matrix mpg weight length price headroom gear_ratio, half

              Comment


              • #8
                the upper diagnal graph show the The correlation coefficient matrix graph ,The correlation coefficient matrix graph and code are as shown belowws

                Code:
                corrtable mpg weight length price headroom gear_ratio,  rformat(%9.2f) flag1(r(rho) > 0) howflag1(plotregion(color(blue * 0.2))) flag2(r(rho) < 0) howflag2(plotregion(color(pink*0.2))) rsize(1 + 5 * abs(r(rho)))
                Click image for larger version

Name:	graph by corrtable command.png
Views:	1
Size:	91.4 KB
ID:	1592587

                but when I add half option,it will show just lower diagonal graphs, the graph matrix command also cann't choice the position lower or upper

                Comment


                • #9
                  Code:
                  sysuse auto,clear
                  graph matrix mpg weight length price headroom gear_ratio, half
                  graph save scatter.gph,replace
                  
                  corrtable mpg weight length price headroom gear_ratio,  rformat(%9.2f) flag1(r(rho) > 0) howflag1(plotregion(color(blue * 0.2))) flag2(r(rho) < 0) howflag2(plotregion(color(pink*0.2))) rsize(1 + 5 * abs(r(rho)))
                  graph save cor.gph,replace
                  
                  graph combine cor.gph scatter.gph    //  not my wanted
                  The command graph combine may not do the job, I want scatter.gph overlay cor.gph, Instead of listing two graphs up and down or left and right,
                  The graph at the top does not affect the figure below in the blank space, just like R ggplot2 package, Define and apply layers, layers can be superimposed layer by layer, and the final display is the superimposed effect

                  Comment


                  • #10
                    help corrtable
                    and i find combine option, maybe the command can add another graph,i tried the codes as follows,but itfailed
                    Code:
                    corrtable mpg weight length price headroom gear_ratio, rformat(%9.2f) flag1(r(rho) > 0) howflag1(plotregion(color(blue * 0.2))) flag2(r(rho) < 0) howflag2(plotregion(color(pink*0.2))) rsize(1 + 5 * abs(r(rho))) combine(imargin(zero))       
                    corrtable mpg weight length price headroom gear_ratio,rformat(%9.2f) flag1(r(rho) > 0) howflag1(plotregion(color(blue * 0.2))) flag2(r(rho) < 0) howflag2(plotregion(color(pink*0.2))) rsize(1 + 5 * abs(r(rho))) combine(scatter.gph)
                    the help documents “ combine() specifies options of graph combine() that tune the combination of the graphs. combine(imargin(zero)) is often a good choice.” I can't understand what it means

                    Comment


                    • #11
                      As Nick Cox has explained, you can program this graph in Stata, and his tip was for you to look at the corrtable ado file for a way to do this.

                      Code:
                      viewsource corrtable.ado
                      Before investing the time to write such an elaborate command, programmers will consider how much demand there is out there. My sense is that most people are satisfied with either the matrix graph or the correlation table and you probably have to code this yourself. If modifying the ado file is a challenge, consider doing this using twoway with the -by()- option. Here is a start with 3 variables:

                      Code:
                      sysuse auto, clear
                      gr matrix mpg weight length, half scheme(s1mono) saving(gr1, replace)
                      *MATRIX IS 3 X 3 WITH SCATTERS AT GRIDS 4, 7 & 8
                      *1. RESHAPE VARS LONG
                      local i 1
                      foreach var in mpg length weight{
                          gen var`i'= `var'
                          local ++i
                      }
                      reshape long var, i(make) j(which)
                      *2 SWITCH VARS TO CREATE NEW VARIABLE
                       gen var2= cond(which==1, length, cond(which==2, weight, mpg))
                      *3 ASSIGN POSITION AND CREATE ALL LEVELS
                      recode which (1=7) (2=8) (3=4)
                      set obs `=_N+9'
                      replace which= sum(1) in -9/l
                      *4 SCATTER ON POSITION 4 REVERSES VARIABLES 
                      clonevar var3=var
                      replace var=var2 if which==7
                      replace var2=var3 if which==7
                      *5 GRAPH
                      scatter var var2,  by(which, rescale imargin(zero) style(compact) note("")) ///
                      ylab(none) xlab(none) ytitle("") xtitle("") scheme(s1mono) subtitle("", nobox) ///
                      saving(gr2, replace)
                      gr combiine gr1.gph gr2.gph, scheme(s1mono) col(1)
                      See the following link on how to introduce text specific to each by graph for the correlations and variable names. The axes labels will be tricky, as you will need to turn them on for all by graphs, but I think that they are not crucial with variables measured in different units.

                      Click image for larger version

Name:	Graph.png
Views:	1
Size:	73.2 KB
ID:	1592653


                      Comment


                      • #12

                        Viewing the source code via viewsource command is a very good idea,But it is difficult for me to understand all the meaning of the code, I'll try

                        Comment

                        Working...
                        X