Announcement

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

  • Overlaying bar and dot plots in panel

    Hello Everybody,

    I have a data set that looks like that:
    country year unit trade
    USA 2017 dollars 1
    USA 2017 euros 2
    USA 2016 dollars 3
    USA 2016 euros 4
    China 2017 dollars 5
    China 2017 euros 6
    China 2016 dollars 7
    China 2016 euros 8
    I would like to have a bar plot for year 2017 and an overlaying dot plot for year 2016. The real dataset is a large panel (wide and long) so the obvious reshape options are not very convenient. My code looks like that:

    graph (bar trade if year==2017 & unit=="dollars", over(country)) (dot trade if year==2016 & unit=="dollars", over(country) vertical)

    But it does not seem to work.

    Any suggestion on how to go about it?
    Thank you very much for your help!
    Paolo


  • #2
    I like "does not seem to". Did you get a graph? Was it what you wanted? My guess is: It will not work. You will get an error message.

    You're applying twoway-like syntax outside twoway.

    You could superimpose a bar and a dot plot in graph twoway.

    But I would just use different marker symbols in graph dot. Here's a dopey example:

    Code:
    webuse grunfeld, clear 
    
    graph dot (asis) mvalue if inlist(year, 1953, 1954), over(year) over(company, sort(1) desc) ///
    marker(1, ms(Oh)) marker(2, ms(+)) asyvars /// 
    linetype(line) lines(lcolor(gs12) lw(vthin)) ytitle(Market value (units???)) ysc(alt)

    Comment


    • #3
      Thank you very much Nick, and sorry for the lack of clarity. Indeed I got an error message.
      I did not know the "inlist" command. That solves the issue.

      All the best
      Paolo

      Comment


      • #4
        Dear Stataforum,
        Does anyone know how to produce a graph that looks like Nick's using multiple outcome variables but only one "over" variable? The goal is to plot two group means (with different markers for the two groups) on each line of a dot plot that examines the means for multiple variables. the over() and by() options seem to be headed in the right direction, but I can't figure out how to put the group means for each variable on the same line. I have pasted Nick's code below along with two of my failed attempts.
        Thanks,

        Jeremy

        Code:
        webuse grunfeld, clear 
        
        *Nick's code: two means on each horizontal line with different markers for the two years
        graph dot (asis) mvalue if inlist(year, 1953, 1954), over(year) over(company, sort(1) desc) ///
        marker(1, ms(Oh)) marker(2, ms(+)) asyvars /// 
        linetype(line) lines(lcolor(gs12) lw(vthin)) ytitle(Market value (units???)) ysc(alt)
        
        *What if I have multiple variables (mvalue invest kstock) rather than one (mvalue)
        *and care about year differences but not company differences?
        
        *one mean on each horizontal line but with two panels
        graph dot mvalue invest kstock if inlist(year, 1953, 1954), ascat over(year) ///
        marker(1, ms(Oh)) marker(2, ms(+)) asyvars /// 
        linetype(line) lines(lcolor(gs12) lw(vthin)) ytitle(Market value (units???)) ysc(alt)
        
        *one mean on each horizontal line but with two subgraphs
        graph dot mvalue invest kstock if inlist(year, 1953, 1954), ascat by(year) ///
        marker(1, ms(Oh)) marker(2, ms(+)) asyvars /// 
        linetype(line) lines(lcolor(gs12) lw(vthin)) ytitle(Market value (units???)) ysc(alt)

        Comment


        • #5
          I forgot to mention one important point: In the graph I wish to make, all the variables have the same range of possible values so a single axis will work. This is not the case in the example above using mvalue, invest, and kstock, so it may present some difficulties that I will not need to address.
          Jeremy

          Comment

          Working...
          X