Announcement

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

  • Twoway Kdensity Plot – Truncate X-Axis

    Dear Statalisters,

    I am using Stata 15.1 MP on a Windows 10 PC and face the following problem: I want to plot six different density plots in one graph while controlling the range of the x-axis. However, I am unable to do the latter one and found no solution in the web.

    Let me explain you my problem with the following MWE, where I just plot two different densities:

    Code:
    sysuse auto, clear
    
    ** Demeaning, that both new variables are centered around zero
    sum price
    replace price = price - r(mean)
    sum mpg
    replace mpg = mpg - r(mean)
    
    * Graph
    twoway (kdensity price) || (kdensity mpg)
    The “||” operator serves perfectly in plotting two different densities in one graph but my goal is to truncate the plot at for instance the values -5 and 5 of the x-axis. However, neither placing the xscale(range) in combination with the xlabel option after the variable names, i.e.

    Code:
    twoway (kdensity price, xscale(range(-5 5)) xlabel(-5(1)5) ) || (kdensity mpg, xscale(range(-5 5)) xlabel(-5(1)5))
    or at the end of the command, i.e.

    Code:
    twoway (kdensity price) || (kdensity mpg) , xscale(range(-5 5)) xlabel(-5(1)5)
    delivers me the result which I want.

    My Google search pointed me towards the Stata tip https://www.stata-journal.com/sjpdf....iclenum=gr0019 that explains that the range suboption will never decrease but just increase the range.

    Does anybody know how to solve my problem?

    Just as background, before I am being asked why I want to do this: I am comparing different estimators and one of those has a relatively huge spread (similar to the one of the variable price in the MWE above). Due to that, the default range set by Stata explodes and the graph is not intuitive anymore. For the reader of my publication it is sufficient to the see the flat line of the density of the respective estimator in the interval, which I want to choose by truncating the x-axis.

    Many thanks in advance.

    Best,
    Andreas
    Last edited by Andreas Denzer; 25 Nov 2019, 03:19. Reason: twoway kdensity

  • #2
    You could use the -at()- option with -kdensity- :

    Code:
    sysuse auto, clear
    
    ** Demeaning, that both new variables are centered around zero
    sum price
    replace price = price - r(mean)
    sum mpg
    replace mpg = mpg - r(mean)
    
    * Graph
    range atx -5 5
    
    kdensity price, gen(xp densityp) at(atx) nograph 
    kdensity mpg , gen(xm densitym) at(atx) nograph 
    
    line densityp xp  /// 
        || line densitym xm,  yaxis(2)   /// 
        ||,  ylabel(.035(.008).075, axis(2)) xtitle("") legend(pos(6) row(1))



    Click image for larger version

Name:	Graph.png
Views:	1
Size:	31.2 KB
ID:	1526272

    Comment


    • #3
      Thanks Scott for your solution! I appreciate it.

      Comment


      • #4
        kdensity MDEP_1 if year == 2016, addplot(kdensity MDEP_1 if year == 2017 || kdensity MDEP_1 if year == 2018) legend(ring(0) pos(1) label(1 "2016") label(2 "2017") label(3 "2018")) name (MDEP_1, replace)

        I am using the code above to construct kdensity plots for MDEP across three years: 2016, 2017, and 2018. I have been trying to add pattern to this graph. For example, I want to make solid, dash, dot respectively for different years. Could someone help me with how to do that?

        Comment

        Working...
        X