Announcement

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

  • Legends and titles always start from the centre of the figures

    I got this really weird problem after migrating my old code to a different places and change the color scheme. Basically, all my legends and graph titles start from the middle of the figure. I tried different command: span, position, ring, justification style, and none of them worked. I tried everything I can and now I am frustrated, please help me.
    Here is an example of the my graph: I am sorry if the graph is too large, I am very new to this forum. And sorry I am not able to share the entire data, as it is survey data and for many reasons we cannot publish it.
    In my code, I tried to make a graph to provide summary statistics for some of my variables. I make them binary variables, and generate one graph for each subgroups. I have three groups, but I make four graphs and use one of them purely as lengend. I hope this is enough background information you need.
    And here is my code:
    Code:
    clear
    set more off
    est clear
    macro drop _all
    graph drop _all
    matrix drop _all
    set autotabgraphs on
    set matsize 10000
    set scheme s2color
    
    capture log close  
    
    /************************************************************/
    /* Table 1: Summary statistics */
    /************************************************************/
    * One row per respondent
    preserve
    bysort ID: keep if _n == 1
    
    
    foreach v in age gender currentstatus_n currentfieldofstudy ///
                 undergraduatemajor countryaffiliation countryofbirth ///
                 englishproficiency mainarearesearch political_score {
    
        * Case 1: string variable → encode to numeric with labels
        capture confirm string variable `v'
        if !_rc {
            encode `v', gen(`v'_cat)
        }
                 }
    
    
    dtable i.gender i.age_cat i.currentstatus_n ///
           i.currentfieldofstudy i.undergraduatemajor ///
           i.countryaffiliation_cat i.countryofbirth_cat ///
           i.englishproficiency i.mainarearesearch ///
           i.political_score, ///
           factor(, statistics(fvfrequency fvpercent)) ///
           fvlabel ///
           export("$tables/Question_0/TableA1_like.xlsx", as(xlsx) replace)
    restore  
    
    /************************************************************/
    /* Summary: Differences in social economics views (Binary questions) */
    /************************************************************/
    **Generaing a local template of the graph
    local baropts "(mean) plot2_1 (mean) plot2_2 spacer1 (mean) plot3_1 (mean) plot3_2 spacer2 (mean) plot4_1 (mean) plot4_2 spacer3 (mean) plot9_1 (mean) plot9_2 spacer4 (mean) plot6_1 (mean) plot6_2 spacer5 (mean) plot5_1 (mean) plot5_2 spacer6 (mean) plot8_1 (mean) plot8_2 spacer7 (mean) plot7_1 (mean) plot7_2, bar(1, fcolor(black)) bar(2, fcolor(black) lcolor(red) lwidth(medium) lpattern(dash)) bar(4, fcolor(olive)) bar(5, fcolor(olive) lcolor(red) lwidth(medium) lpattern(dash)) bar(7, fcolor(green)) bar(8, fcolor(green) lcolor(red) lwidth(medium) lpattern(dash)) bar(10, fcolor(blue)) bar(11, fcolor(blue) lcolor(red) lwidth(medium) lpattern(dash)) bar(13, fcolor(orange)) bar(14, fcolor(orange) lcolor(red) lwidth(medium) lpattern(dash)) bar(16, fcolor(khaki)) bar(17, fcolor(khaki) lcolor(red) lwidth(medium) lpattern(dash)) bar(19, fcolor(navy)) bar(20, fcolor(navy) lcolor(red) lwidth(medium) lpattern(dash)) bar(22, fcolor(olive_teal)) bar(23, fcolor(olive_teal) lcolor(red) lwidth(medium) lpattern(dash)) blabel(bar, format(%4.2f) pos(outside) color(black))"
    
    local legendopts legend(on cols(1) span position(6) rowgap(0.5) size(small) symysize(3) symxsize(7) ///
        order(1 2 4 5 7 8 10 11 13 14 16 17 19 20 22 23) ///
        label(1 "Pair 1_1: Government regulation is necessary to protect the public interest") ///
        label(2 "Pair 1_2: Government regulation usually does more harm than good") ///
        label(4 "Pair 2_1: Hard work and determination are no guarantee of success for most people") ///
        label(5 "Pair 2_2: Most people who want to get ahead can make it if they're willing to work hard") ///
        label(7 "Pair 3_1: Discrimination is the main reason why disadvantaged people can't get ahead") ///
        label(8 "Pair 3_2: Disadvantaged people who can't get ahead are mostly responsible for their own condition") ///
        label(10 "Pair 4_1: Business corporations make too much profit") ///
        label(11 "Pair 4_2: Most corporations make a fair and reasonable amount of profit") ///
        label(13 "Pair 5_1: Rise in income inequality has significant consequences and requires serious attention") ///
        label(14 "Pair 5_2: Focus on income inequality is wrongheaded. What matters is poverty not inequality") ///
        label(16 "Pair 6_1: Too much power is concentrated in the hands of a few large companies") ///
        label(17 "Pair 6_2: The largest companies do NOT have too much power") ///
        label(19 "Pair 7_1: It is NOT necessary to believe in God in order to be moral and have good values") ///
        label(20 "Pair 7_2: It is necessary to believe in God in order to be moral and have good values") ///
        label(22 "Pair 8-2: A Claim should be rejected only based on the substance of the argument itself") ///
        label(23 "Pair 8-3: A Claim should be rejected based on the author's view and the argument substance") )
    
    
    *generating plots and spacers for all the graphs
    foreach i of numlist 2 3 4 5 6 7 8 9 {
        tab addqn`i', gen(plot`i'_)
    }
    
    foreach i of numlist 1 2 3 4 5 6 7{
        gen spacer`i' = .
    }
    
    **generating a graph for views among all economics students
    preserve
    bysort ID: keep if _n == 1
    graph bar `baropts' ///
        ytitle("Fraction") title("Sociopolitical Views", size(medium)span) ///
        legend(off) ///
        name(G_all, replace) ysize(7) xsize(16)
    restore
    
    *generating a graph just for legend
    preserve
    bysort ID: keep if _n == 1
    graph bar `baropts' ///
        ytitle("Fraction") title("Sociopolitical Views", size(medium) span) ///
       `legendopts' ///
        name(G_all_2, replace) ysize(7) xsize(16)
    restore
    
    **generating a graph for views among all undergraduate economics students
    preserve
    bysort ID: keep if _n == 1 & currentstatus_n == 1
    graph bar `baropts' ///
        ytitle("Fraction") title("Sociopolitical Views of undergraduate students", size(medium)span) ///
        legend(off) ///
        name(G_ug, replace) ysize(7) xsize(16)
    restore
    
    
    **generating a graph for views among all master economics students
    preserve
    bysort ID: keep if _n == 1 & currentstatus_n == 2
    graph bar `baropts' ///
        ytitle("Fraction") title("Sociopolitical Views of Master students", size(medium) span) ///
        legend(off) ///
        name(G_ma, replace) ysize(7) xsize(16)
    
    restore
    
    
    **generating a graph for views among all PhD economics students
    preserve
    bysort ID: keep if _n == 1 & currentstatus_n == 3
    graph bar `baropts' ///
        ytitle("Fraction") title("Sociopolitical Views of PhD students", size(medium)span) ///
        legend(off) ///
        name(G_phd, replace) ysize(7) xsize(16)
    
    restore
    
    *Stack all graphs together    
    grc1leg2 G_all G_all_2 G_ug G_ma G_phd, rows(4) cols(1) ///
        legendfrom(G_all_2) hidelegendfrom position(6) ring(2) ///
        ysize(100) xsize(85) ///
        imargin(0 0 0 0) xcommon ycommon name(G_all_stack, replace) ///
        span
    Here is my data
    Code:
    ID    question    addqn2    addqn3    addqn4    addqn5    addqn6    addqn7    addqn9    addqn8
    1    2    Government regulation is necessary to protect the public interest    Most people who want to get ahead can make it if they're willing to work hard    Disadvantaged people who can't get ahead are mostly responsible for their own condition    Business corporations make too much profit    Too much power is concentrated in the hands of a few large companies    A claim should be rejected only based on the substance of the argument itself    Rise in income inequality has significant consequences and requires serious attention    It is NOT necessary to believe in God in order to be moral and have good values
    1    3    Government regulation is necessary to protect the public interest    Most people who want to get ahead can make it if they're willing to work hard    Disadvantaged people who can't get ahead are mostly responsible for their own condition    Business corporations make too much profit    Too much power is concentrated in the hands of a few large companies    A claim should be rejected only based on the substance of the argument itself    Rise in income inequality has significant consequences and requires serious attention    It is NOT necessary to believe in God in order to be moral and have good values
    1    8    Government regulation is necessary to protect the public interest    Most people who want to get ahead can make it if they're willing to work hard    Disadvantaged people who can't get ahead are mostly responsible for their own condition    Business corporations make too much profit    Too much power is concentrated in the hands of a few large companies    A claim should be rejected only based on the substance of the argument itself    Rise in income inequality has significant consequences and requires serious attention    It is NOT necessary to believe in God in order to be moral and have good values
    1    9    Government regulation is necessary to protect the public interest    Most people who want to get ahead can make it if they're willing to work hard    Disadvantaged people who can't get ahead are mostly responsible for their own condition    Business corporations make too much profit    Too much power is concentrated in the hands of a few large companies    A claim should be rejected only based on the substance of the argument itself    Rise in income inequality has significant consequences and requires serious attention    It is NOT necessary to believe in God in order to be moral and have good values
    1    11    Government regulation is necessary to protect the public interest    Most people who want to get ahead can make it if they're willing to work hard    Disadvantaged people who can't get ahead are mostly responsible for their own condition    Business corporations make too much profit    Too much power is concentrated in the hands of a few large companies    A claim should be rejected only based on the substance of the argument itself    Rise in income inequality has significant consequences and requires serious attention    It is NOT necessary to believe in God in order to be moral and have good values
    1    12    Government regulation is necessary to protect the public interest    Most people who want to get ahead can make it if they're willing to work hard    Disadvantaged people who can't get ahead are mostly responsible for their own condition    Business corporations make too much profit    Too much power is concentrated in the hands of a few large companies    A claim should be rejected only based on the substance of the argument itself    Rise in income inequality has significant consequences and requires serious attention    It is NOT necessary to believe in God in order to be moral and have good values
    1    15    Government regulation is necessary to protect the public interest    Most people who want to get ahead can make it if they're willing to work hard    Disadvantaged people who can't get ahead are mostly responsible for their own condition    Business corporations make too much profit    Too much power is concentrated in the hands of a few large companies    A claim should be rejected only based on the substance of the argument itself    Rise in income inequality has significant consequences and requires serious attention    It is NOT necessary to believe in God in order to be moral and have good values
    3    3    Government regulation is necessary to protect the public interest    Most people who want to get ahead can make it if they're willing to work hard    Discrimination is the main reason why disadvantaged people can't get ahead    Most corporations make a fair and reasonable amount of profit    Too much power is concentrated in the hands of a few large companies    A claim should be rejected only based on the substance of the argument itself    Rise in income inequality has significant consequences and requires serious attention    It is NOT necessary to believe in God in order to be moral and have good values
    3    4    Government regulation is necessary to protect the public interest    Most people who want to get ahead can make it if they're willing to work hard    Discrimination is the main reason why disadvantaged people can't get ahead    Most corporations make a fair and reasonable amount of profit    Too much power is concentrated in the hands of a few large companies    A claim should be rejected only based on the substance of the argument itself    Rise in income inequality has significant consequences and requires serious attention    It is NOT necessary to believe in God in order to be moral and have good values
    3    8    Government regulation is necessary to protect the public interest    Most people who want to get ahead can make it if they're willing to work hard    Discrimination is the main reason why disadvantaged people can't get ahead    Most corporations make a fair and reasonable amount of profit    Too much power is concentrated in the hands of a few large companies    A claim should be rejected only based on the substance of the argument itself    Rise in income inequality has significant consequences and requires serious attention    It is NOT necessary to believe in God in order to be moral and have good values
    3    11    Government regulation is necessary to protect the public interest    Most people who want to get ahead can make it if they're willing to work hard    Discrimination is the main reason why disadvantaged people can't get ahead    Most corporations make a fair and reasonable amount of profit    Too much power is concentrated in the hands of a few large companies    A claim should be rejected only based on the substance of the argument itself    Rise in income inequality has significant consequences and requires serious attention    It is NOT necessary to believe in God in order to be moral and have good values
    3    13    Government regulation is necessary to protect the public interest    Most people who want to get ahead can make it if they're willing to work hard    Discrimination is the main reason why disadvantaged people can't get ahead    Most corporations make a fair and reasonable amount of profit    Too much power is concentrated in the hands of a few large companies    A claim should be rejected only based on the substance of the argument itself    Rise in income inequality has significant consequences and requires serious attention    It is NOT necessary to believe in God in order to be moral and have good values
    3    15    Government regulation is necessary to protect the public interest    Most people who want to get ahead can make it if they're willing to work hard    Discrimination is the main reason why disadvantaged people can't get ahead    Most corporations make a fair and reasonable amount of profit    Too much power is concentrated in the hands of a few large companies    A claim should be rejected only based on the substance of the argument itself    Rise in income inequality has significant consequences and requires serious attention    It is NOT necessary to believe in God in order to be moral and have good values
    Click image for larger version

Name:	image_37955.png
Views:	1
Size:	923.7 KB
ID:	1785373

  • #2
    Never mind, I found this post https://www.statalist.org/forums/for...-in-all-graphs, turned out stata changed my font to something weird after some updates. After changing font, everything is normal now. Who would think that's the problem!?

    Comment

    Working...
    X