Announcement

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

  • twoway plot aspectration/ Overlayplot - help

    Dear all,

    I am currently trying to create a twoway plot using the European Value Study Time Series.
    I have created the following variables for the graph

    - abortion_wave: this is a variable that shows the mean abortion atttitudes (1 to 10) per country (24 European countries)
    - x_yearEVS: this is a time variable at three time points (1990, 1999 and 2008)
    - x_countriesEVS: 24 selected countries.

    While the code below works, the graph doesn't look quite as I hoped. I'll attach the graph.
    I already tried adjusting the scale via aspectration and ysize and xsize but nothing seems to work. Could you help me?
    I'm using Stata 13, if this if of help
    I know this is rather basic question but I'm genuinely stuck and neither YouTube nor ChatGTP have helped so far.

    Best;
    Ruth

    This is my code:
    local countries 40 56 100 203 208 233 246 250 276 826 348 372 380 428 440 470 528 616 620 642 703 705 724 752

    * Define a list of colors
    local colors red blue green orange purple brown gray mint pink purple yellow cyan lavender lime magenta olive bluishgray dkorange dkgreen ltblue orange_red dknavy sand

    * Initialize an empty list for the plot commands
    local plotcmds

    * Counter for colors
    local color_index 1

    * Loop through each country to generate line plot commands
    foreach country of local countries {
    * Use a color from the predefined list, cycling through colors
    local color : word `color_index' of `colors'
    * Append each line command to the plot commands list
    local plotcmds `plotcmds' (line mean_abortion_wave x_yearEVS if x_countriesEVS == `country', lcolor(`color') lpattern(solid) lwidth(thin))
    * Increment the color index, reset if exceeding the number of colors
    local color_index = `color_index' + 1
    if `color_index' > `:word count `colors'' {
    local color_index = 1
    }
    }

    * Generate the overlay plot with all countries
    * Generate the overlay plot with all countries
    twoway `plotcmds', ///
    legend(order(1 "Austria" 2 "Belgium" 3 "Bulgaria" 4 "Czechia" 5 "Denmark" 6 "Estonia" 7 "Finland" 8 "France" 9 "Germany" 10 "Great Britain" ///
    11 "Hungary" 12 "Ireland" 13 "Italy" 14 "Latvia" 15 "Lithuania" 16 "Malta" 17 "Netherlands" 18 "Poland" 19 "Portugal" 20 "Romania" ///
    21 "Slovakia" 22 "Slovenia" 23 "Spain" 24 "Sweden") size(small) rows(5)) ///
    title("Trends of Attitudes towards Abortion Over Time") ///
    ytitle("Attitude towards Abortion") ///
    xtitle("Year") ///
    xlabel(1990 1999 2008, labsize(medium)) ///
    ylabel(1 "Never justifiable" 2 3 4 5 6 7 8 9 10 "Always justifiable", angle(0) nogrid) ///
    yscale(range(1 10)) ///
    aspectratio(2) /// Increase the aspect ratio for a longer y-axis
    ysize(20) /// Increase the y-axis length by setting the graph height
    xsize(15) /// Optionally, adjust the width to maintain balance
    Last edited by Ruth Meier; 20 Aug 2024, 06:18.

  • #2
    Forgot the Graph. Here it is. I'd like the yaxis to be right next to the actual Graph.
    Click image for larger version

Name:	20240820_OverlayPlot_v1.JPG
Views:	1
Size:	80.1 KB
ID:	1762004

    Comment


    • #3
      I suggest that tweaking the aspect ratio is a side-issue. The main issue is whether this design has a half-decent chance of ever working well. Getting 24 distinct colours, having a legend that takes up much of the available real estate, obliging the reader to go back and forth between legend and data: all are just between awkward and obnoxious.

      I used scheme stcolor in Stata 18, fabplot from the Stata Journal to get front-and-back plots and myaxis from the Stata Journal to get a reasonable alternative to alphabetical order.

      I used 24 countries and years 1990(9)2008 for realism but faked the data otherwise.

      See Howard Wainer's 1984 paper on (believe it or not!) Austria first! https://www.jstor.org/stable/2683253

      Code:
      clear 
      
      * fake dataset 
      
      set obs 72 
      set seed 314159
      gen year = 2008 if mod(_n, 3) == 0
      replace year = 1999 if mod(_n, 3) == 2 
      replace year = 1990 if mod(_n, 3) == 1 
      egen country = seq(), block(3)
      
      label def country 1 "Austria" 2 "Belgium" 3 "Bulgaria" 4 "Czechia" 5 "Denmark" ///
      6 "Estonia" 7 "Finland" 8 "France" 9 "Germany" 10 "Great Britain" ///
      11 "Hungary" 12 "Ireland" 13 "Italy" 14 "Latvia" 15 "Lithuania" 16 "Malta" ///
      17 "Netherlands" 18 "Poland" 19 "Portugal" 20 "Romania" ///
      21 "Slovakia" 22 "Slovenia" 23 "Spain" 24 "Sweden"
      
      label val country country 
      
      gen outcome = rnormal(5, 1) if year == 1990
      replace outcome = outcome[_n-1] + rnormal(0,1) if inlist(year, 1999, 2008) 
      
      * graph commands start here 
      
      myaxis country2=country, sort(mean outcome) subset(year==2008)
      
      fabplot line outcome year, by(country2, compact col(6)) frontopts(lw(thick) recast(connected)) xla(1990 1999 2008, labsize(medlarge)) xsc(r(1989, 2009)) xtitle("") yla(, labsize(medlarge))  name(fabplot, replace)
      In essence:

      1. Each country is shown in turn with the other countries' data as backdrop.

      2. Choose any sensible order other than alphabetical. Here we're sorting on the mean of values for 2008, which is just the value for 2008.

      3. Your data are different, but my guess is that is it will be easier to think about contrasts.

      4. I guess you're using a version before Stata 18. If so, I recommend losing the blue backdrop with set scheme s1color

      Click image for larger version

Name:	fabplot.png
Views:	1
Size:	595.4 KB
ID:	1762049

      Comment


      • #4
        You do say that you're using Stata 13.

        Comment


        • #5
          Nick Cox Thank you very much for your input, that's very valuable feedback for me!
          I will try and integrate this in my bachelor thesis, wasn't my aim to display data badly

          Comment


          • #6
            Nick Cox
            I keep getting the error message " sum of expand values exceed 2,147,483,647 - The dataset may not contain more than 2,147,483,647 observations."

            This is the code I'm using:

            Code:
            clear
            
            set obs 123775 /// Stata 13 orders me that "obs must be between 123775 and 2147483646"
            set seed 314159
            
            * Generate year and country variables
            gen year_plot = 2008 if mod(_n, 3) == 0
            replace year_plot = 1999 if mod(_n, 3) == 2
            replace year_plot = 1990 if mod(_n, 3) == 1
            egen country_plot = seq(), block(3)
            
            * Label the country variable
            label def countryplotlbl 1 "Austria" 2 "Belgium" 3 "Bulgaria" 4 "Czechia" 5 "Denmark" ///
            6 "Estonia" 7 "Finland" 8 "France" 9 "Germany" 10 "Great Britain" ///
            11 "Hungary" 12 "Ireland" 13 "Italy" 14 "Latvia" 15 "Lithuania" 16 "Malta" ///
            17 "Netherlands" 18 "Poland" 19 "Portugal" 20 "Romania" ///
            21 "Slovakia" 22 "Slovenia" 23 "Spain" 24 "Sweden"
            label val country_plot countryplotlbl
            
            gen outcome = rnormal(5, 1) if year_plot == 1990
            replace outcome = outcome[_n-1] + rnormal(0,1) if inlist(year_plot, 1999, 2008)
            
            bysort country_plot (year_plot): egen mean2008 = mean(abort_att) if year_plot == 2008
            sort mean2008
            
            * Define the axis for the plot
            myaxis country2 = country_plot, sort(mean abort_att) subset(year_plot == 2008)
            
            * Generate the plot
            fabplot line mean_abortion_country year_plot, by(country2, compact col(6)) ///
                frontopts(lw(thick) recast(connected)) xla(1990 1999 2008, labsize(medlarge)) ///
                xsc(r(1989, 2009)) xtitle("") yla(, labsize(medlarge)) name(fabplot, replace)
            
            * Optional: Set the scheme for better visuals
            set scheme s1color
            abort_att is my dependent variable containing Attitudes towards abortion on a 10-point scale. As a reminder I use Stata 13, but myaxis should only require Stata 8.2 so I should be good?
            I feel weird asking this again but I really don't get whats wrong.

            Best,
            Hannah

            Comment


            • #7
              But why are you setting the dataset size so large? For 24 countries and 3 years, you need 72 observations just as for my code,

              My code fakes data because you've not provided a data example.

              I can't work out what you're doing either. Perhaps it's a side-effect of using a dataset based on individuals, but even so you should be able to get 72 observations from a collapse and just possibly a reshape.

              Comment


              • #8
                Thats what i tought too, but as I have written in the Code whenever I try to set the observations to 72 i get a "obs must be between 123775 and 2147483646"- error message.
                To fix this i tried to include the collaps command but this didn't work.

                What I am trying to do is use your code and adapt it. Country and year is clear for me but i don't understand where to put my outcome variable (abortion attitudes). I've tried several combinations but stata never executes my code due to error message above

                Thank you so much for helping!

                Comment


                • #9
                  Sorry, but that still makes no sense to me. Such a message should never follow clear.

                  Reduce your dataset to one for 24 countries and 3 years and your task should then be straightforward. As said, otherwise I need to see a data example.

                  Comment

                  Working...
                  X