Announcement

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

  • Stack bar chart with multiple bars without categorical variables

    Hi community!

    I am trying to plot a stack bar chart with multiple bars without categorical variables for grouping the data:

    First bar
    • I have a variable Area 1 (how much area a household has in a low elevation surface), and Area 2 (how much area a household has in a high elevation surface). Area1 + Area2 = Total Area.
    • For each observation, I have all three: Total Area, Area 1, and Area 2. So I don't have a categorical variable, they are all numerical ones.
    • Now I want to plot a stacked bar that summarizes how much percentage of the total area is Area 1, and the same for Area 2
    Second bar
    • I have a variable Plot 1 (how many plots a household has in a low elevation surface), and Plot 2 (how many plots a household has in a high elevation surface). Plot 1 + Plot 2 = Total Plots
    • For each observation, I have all three: Total plots, Plot 1 and Plot 2. So I don't have a categorical variable, they are all numerical ones.
    • Now I want to add a stacked bar that summarizes how much percentage of the total plots is Plot 1, and the same for Plot 2
    When doing it in Word it would look like this:


    Click image for larger version

Name:	Screenshot 2023-04-05 155624.png
Views:	1
Size:	14.8 KB
ID:	1708631

  • #2
    You can calculate the percentages after reshaping and collapsing the data. Be sure not to overwrite the original dataset (either do not save the dataset after running the code or keep an extra copy elsewhere). The code below relies on the variable names being as specified in the dataex example. Run in a do-file.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(area1 area2 total1 plot1 plot2 total2)
     50 20  70  300 900  1200
    300 50 350 1000 250 1250
    200 120 320 300 700 1000
    end
    
    gen long hhid=_n
    drop total?
    rename (area? plot?) var=
    reshape long var, i(hhid) j(which) string
    collapse (sum) var, by(which)
    gen cat= ustrregexra(which, "\d", "")
    replace which= ustrregexra(which, "[^\d]", "")
    bys cat: egen pct= total(var)
    replace pct=var/pct*100
    separate pct, by(which) veryshortlabel
    set scheme s1color
    gr bar pct?, over(cat) stack bar(1, lcolor(black) fcolor(navy%30)) ///
    bar(2, lcolor(black) fcolor(navy%80)) blab(bar, format("%3.0f") ///
    pos(inside)) ytitle("Percent") ylab("") ///
    legend(order(1 "Upland" 2 "Lowland") pos(3) col(1))

    Res.:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	22.7 KB
ID:	1708646

    Last edited by Andrew Musau; 05 Apr 2023, 16:25.

    Comment

    Working...
    X