Announcement

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

  • changing the color of specific values in a meta forestplot using code

    Hi All,

    I would like to change the color of specific values to red, as a means of highlighting it using code. I have been doing it manually heretofore, using the graph editor by highlighting the value, right-clicking "object-specific properties" and then changing the color to red.

    PHP Code:

    webuse bcgsetclear
    meta esize npost nnegt nposc nnegcesize(lnrratiostudylabel(studylbl)
    meta forestploteform nullrefline 

    On the forestplot,I'd like to highlight the "treatment (yes)" value for Rosenthal et al., 1960 (equals 3) to red, and the Vandiviere et al., 1973 "Control (no)" (equals 619) to red.

    Thanks in advance!

    Ariel

  • #2
    Code:
    gr_edit .plotregion1.column2.items[3].style.editstyle color(red)
    does not work as expected. You may try https://www.stata.com/support/tech-support/contact/

    Comment


    • #3
      Thanks, Bjarte. I am sure there is a way of doing this. I'll contact support.

      Comment


      • #4
        One way could be to export as SVG -then repeatedly search replace to change color. Below is a basic example. The serset part is not used below, but included to show informations which could be of use.
        Code:
        version 16
        
        webuse bcgset, clear
        meta esize npost nnegt nposc nnegc, esize(lnrratio) studylabel(studylbl)
        meta forestplot, eform nullrefline 
        
        serset dir
        serset set 0
        serset use , clear
        keep _meta_studylabel _meta_a   - _meta_d
        list
        
        graph export fp.svg , replace
        
        confirm file fp.svg
        
        local svgfile  "fp.svg"
        local study "Rosenthal et al., 1960"
        local figcolumn 2 // col 1 == studylabel 
        local textcolor red
        
        mata : 
        
            svgfile = st_local("svgfile")
            study = st_local("study")
            figcolumn = strtoreal(st_local("figcolumn"))  
            textcolor = st_local("textcolor")
            
            svg = cat(svgfile)
            study = select(svg, strpos(svg, study)) 
            ustrregexm(study, "y=(.*? )"); study_y_pos = ustrregexs(1) 
            study = select(svg, strpos(svg, study_y_pos )) 
            from = study[figcolumn]
            to = subinstr(from, "#000000", textcolor ) 
            st_strscalar("s", invtokens(subinstr(svg, from, to)'))
            
        end 
        
        scalar fwrc = filewrite("changed_fp.svg", s, 1)
        
        scalar drop s
        scalar drop fwrc

        Comment


        • #5
          Thank you again, Bjarte!

          By the way, I contacted StataCorp about the issue and they said individual values cannot be changed in a meta forestplot. Obviously they can be changed, but it is not as straightforward as one would hope!
          Ariel

          Comment


          • #6
            In #4 the cat(svgfile) EOL char(s) is stripped off making problems if the code is repeated. Below is a small program which avoid this problem.
            Code:
            version 16.1
            
            ********************************************************************************
            capt prog drop svg_edit_text_color
            prog define svg_edit_text_color , rclass
            
            version 16.1
            
            if ( word(`"`0'"', 1) == "using" ) {
                
                syntax [anything] [using] , /// Stata producted SVG file
                    [study(string)]         /// study label used in mf plot
                    [figcolumn(integer 1)]  /// mf plot column
                    [textcolor(string)]     //  SVG text color specification
                
                local svgfile `2'    
            }
            
            else {
                
                args svgfile study figcolumn textcolor    
            }
                         
            ********************************************************************************
            
            confirm file "`svgfile'"
            qui hexdump "`svgfile'" , analyze
            local windows = ( r(Windows) == r(lnum) )  
            local mac = ( r(Mac) == r(lnum) ) 
            local end_mata = "end"
            
            ********************************************************************************
            
            mata : 
            
                svgfile   = st_local("svgfile")
                study     = st_local("study")
                figcolumn = strtoreal(st_local("figcolumn"))  
                textcolor = st_local("textcolor")
                
                eol_windows = strtoreal(st_local("windows"))  
                eol_mac = strtoreal(st_local("mac"))
                
                svg = cat(svgfile) :+ char(10)  /* cat() strips of EOL chars */
                study = select(svg, strpos(svg, study)) 
                match_y_pos = ustrregexm(study, "y=(.*? )")
                study_y_pos = ustrregexs(1) 
                study = select(svg, strpos(svg, study_y_pos )) 
                from = study[figcolumn]
                to = subinstr(from, "#000000", textcolor ) 
                svg = subinstr(svg, from, to)
                
                if ( eol_windows==1 ) {
                    
                    svg = subinstr(svg, char(10), char(13) + char(10) ) 
                }
                
                if ( eol_mac == 1 ) {
                    
                    svg = subinstr(svg, char(10), char(13) ) 
                }
                
                st_strscalar("svg", invtokens( svg' , "" ) )
                
            `end_mata' 
            
            ********************************************************************************
            
            return local filewrite = filewrite("`svgfile'", svg, 1)
            return local filename =  "`svgfile'"
            return local cmd =  "svgfile " + `"`0'"'  
            
            end /* prog  svg_edit_text_color */
            ********************************************************************************
            
            
            
            ********************************************************************************
            * svg_edit_text_color: example of use
            ********************************************************************************
            
            webuse "bcgset", clear
            
            meta esize npost nnegt nposc nnegc, ///
                esize(lnrratio) studylabel(studylbl)
            
            meta forestplot, eform nullrefline 
            
            gr export "fp.svg" , replace 
            
            
            * used with positional arguments
            
            copy fp.svg fpcopy.svg , replace
            
            svg_edit_text_color fpcopy.svg "Rosenthal et al., 1960" 2 red
            svg_edit_text_color fpcopy.svg "Vandiviere et al., 1973" 5 red
            
            * or with named options
            
            copy fp.svg fpcopy.svg , replace
            
            svg_edit_text_color using fpcopy.svg , ///
                study("Rosenthal et al., 1960")  ///
                figcolumn(2) ///
                textcolor(red)
                
            svg_edit_text_color using fpcopy.svg , ///
                study("Vandiviere et al., 1973") ///
                figcolumn(5) ///
                textcolor(red)
                
            ********************************************************************************
            
            exit
            

            Comment


            • #7
              Thank you so much, Bjarte!

              Ariel

              Comment

              Working...
              X