Announcement

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

  • Customizing Markers and Colors in Scatter Plots with Distinct Legends for Color and Marker Shape

    Hello,

    I am working on a scatter plot in Stata and need some guidance on how to achieve the following customizations:
    1. Different Markers for Different Data parts: I have two parts in my data, DepVar_hat_change1_* and DepVar_change1_*. I would like to use different marker shapes for each part. For example, circles (O) for DepVar_hat_change1_* and plus signs (+) for DepVar_change1_*.
    2. Consistent Colors for Matching Levels Across Data Sets: Each of these sets is further divided into levels (e.g., DepVar_hat_change1_1, DepVar_hat_change1_2, ..., DepVar_change1_1, DepVar_change1_2, ...). I want to use the same color for matching levels across the two sets (e.g., DepVar_hat_change1_1 and DepVar_change1_1 should have the same color).
    3. Legends: I would like to have two separate legends in my graph:
      • One for colors, indicating the profession or category each color represents.
      • Another for marker shapes, indicating the part of data each shape represents (e.g., O for DepVar_hat_change1 and x for DepVar_change1_*).
    I am struggling with setting this up, especially with ensuring that the colors match across levels and that the legends are clear and non-repetitive. Here's a snippet of the code I am currently working with:

    graph twoway /// (scatter `DepVar'_`change1'_* `xvar'_`change3', sort msymbol(O) msize(small) mlab(`prof') mlabc(black) mlabpos(12) mcolor()) /// (scatter `DepVar'_hat_`change1'_* `xvar'_`change3', sort msymbol(+) msize(small) mlab(`prof') mlabc(black) mlabpos(12)) /// (qfit hat_`DepVar'_`change1' `xvar'_`change3', lcolor("200 0 0")) /// (qfit `DepVar'_`change1' `xvar'_`change3', lcolor("0 200 0") lpattern(dash)) /// and dashed , xt(`: variable label `xvar'_`change3' ', margin(medium) size(small)) /// xsc(noextend noline) xlab(, nogrid) /// yt(`: variable label `DepVar'_`change1' ', size(small)) /// yline(0, lpattern(dash) lcolor(black)) /// ysc(noextend titlegap(1)) /// legend(pos(7) col(4) order(1 "2. Craftsmen, tradesmen and business owners" 2 "3. Executives and higher intellectual professions" 3 "4. Intermediate professions" 4 "5. Employees" 5 "6. Workmen" 6 "Quadratic fit: DepVar_change1_hat" 7 "Quadratic fit: DepVar_change1") size(vsmall))


    Any advice or examples of similar implementations would be greatly appreciated!

    Thank you!

  • #2
    For highly advanced customization, creating the scatterplot from scratch using twoway is necessary.

    Code:
    tw scatter y1 x1 if ..., mc(red) msy(oh) || scatter y2 x2 if ..., mc(blue) msy(+) || ...
    Alternatively, you can explore sepscatter from SSC.

    Code:
    ssc desc sepscatter

    Legends: I would like to have two separate legends in my graph:
    Stata supports a single legend; if you find the need for two, it could indicate that your data presentation is too complex for this setup. If possible, include a data example following FAQ Advice #12 to increase the likelihood of obtaining helpful replies.

    Comment


    • #3
      Thank you for your response. I'd like to clarify that by separate legends I meant organizing legends separately for shapes and colors. My intention is to assign a distinct meaning to each shape and each color. However, I'm encountering an issue where Stata is creating a separate legend entry for each combination of shape and color. This results in each color being listed twice, once for each marker shape, which seems redundant.

      Additionally, although I've manually set the colors to ensure consistency across two different parts of the data, I'm facing challenges with the marker sizes. Despite using the msize option, it appears that the marker size isn't adjusting according to the weights I've specified. here is my code :

      graph twoway ///
      (scatter `DepVar'_`change1'_* `xvar'_`change3', sort msize(Emp_2013) msy(T T T T T) mlab(`prof' `prof' `prof' `prof' `prof') mc("109 175 109" "155 217 149" "242 157 68" "252 207 134" "97 182 198") mlabc(black) mlabpos(12)) ///
      (scatter `DepVar'_hat_`change1'_* `xvar'_`change3', sort msize(Emp_2013) msy(O O O O O) mc("109 175 109%50" "155 217 149%50" "242 157 68%50" "252 207 134%50" "97 182 198%50") mlab(`prof' `prof' `prof' `prof' `prof') mlabc(black) mlabpos(12)) ///
      (qfit hat_`DepVar'_`change1' `xvar'_`change3' [pw = Emp_2013], lcolor("200 0 0")) ///
      (qfit `DepVar'_`change1' `xvar'_`change3' [pw = Emp_2013], lcolor("0 200 0") lpattern(dash)) /// and dashed
      , xt(`: variable label `xvar'_`change3' ', margin(medium) size(small)) ///
      xsc(noextend noline) xlab(, nogrid) ///
      yt(`: variable label `DepVar'_`change1' ', size(small)) ///
      yline(0, lpattern(dash) lcolor(black)) ///
      ysc(noextend titlegap(1)) ///
      legend(pos(7) col(4) order(1 "2. Craftsmen, tradesmen and business owners" 2 "3. Executives and higher intellectual professions" 3 "4. Intermediate professions" 4 "5. Employees" 5 "6. Workmen" 6 "Quadratic fit: DepVar_change1_hat" 7 "Quadratic fit: DepVar_change1") size(vsmall))

      Thank you for your assistance.

      Comment


      • #4
        Originally posted by Walid NABI View Post
        My intention is to assign a distinct meaning to each shape and each color. However, I'm encountering an issue where Stata is creating a separate legend entry for each combination of shape and color. This results in each color being listed twice, once for each marker shape, which seems redundant.
        -legend()- has an -order()- suboption which specifies which keys appear and their order. See

        Code:
        help legend_options
        Previously, I have stated that if you, e.g., specify impossible conditions at the end of the twoway graph, the symbols are rendered in the legend but the graph is itself unaffected. You can specify -mc(black)- for each added plot at the end. See #7:

        https://www.statalist.org/forums/for...-in-the-legend


        Additionally, although I've manually set the colors to ensure consistency across two different parts of the data, I'm facing challenges with the marker sizes. Despite using the msize option, it appears that the marker size isn't adjusting according to the weights I've specified.

        This is addressed in the following Stata tip: https://journals.sagepub.com/doi/10....36867X20931008. All of this is extra work for you, but it will pay off in the future as it will familiarize you with Stata graphics.
        Last edited by Andrew Musau; 23 Jan 2024, 07:08.

        Comment

        Working...
        X