Hi @empet,
Thanks for helping. I used your code and my data set (long version), but I am making some mistakes because the plot I got is not what I was looking for. The figure I get is the following:
And your code is (I am using PlotlyBase inside Pluto, so the function Plot
instead of plot
):
fig = Plot(long_data, kind="bar", x=:Year, y=:Percent, color=:Status,
Layout(barmode="stack" ))
In case you want to have a go and replicate that figure, my data is the following (I think you can save it into a txt file, I did it with your data example):
Year,Levels,Status,Percent
2011,Less than a High School Diploma,Employment,53.2
2011,High School Diploma ,Employment,66.9
2011,"Some College, No Degree ",Employment,70.4
2011,Associate Degree,Employment,76.2
2011,Bachelor’s Degree or Higher,Employment,81.5
2016,Less than a High School Diploma,Employment,56.1
2016,High School Diploma ,Employment,68.3
2016,"Some College, No Degree ",Employment,72.7
2016,Associate Degree,Employment,77.6
2016,Bachelor’s Degree or Higher,Employment,82.9
2021,Less than a High School Diploma,Employment,54.8
2021,High School Diploma ,Employment,66.6
2021,"Some College, No Degree ",Employment,71.3
2021,Associate Degree,Employment,75.9
2021,Bachelor’s Degree or Higher,Employment,82.9
2011,Less than a High School Diploma,Unemployment,9
2011,High School Diploma ,Unemployment,7.1
2011,"Some College, No Degree ",Unemployment,6.7
2011,Associate Degree,Unemployment,5.5
2011,Bachelor’s Degree or Higher,Unemployment,3.6
2016,Less than a High School Diploma,Unemployment,4.6
2016,High School Diploma ,Unemployment,3.8
2016,"Some College, No Degree ",Unemployment,3.4
2016,Associate Degree,Unemployment,2.9
2016,Bachelor’s Degree or Higher,Unemployment,2.1
2021,Less than a High School Diploma,Unemployment,5.1
2021,High School Diploma ,Unemployment,4.5
2021,"Some College, No Degree ",Unemployment,4.2
2021,Associate Degree,Unemployment,3.6
2021,Bachelor’s Degree or Higher,Unemployment,2.5
2011,Less than a High School Diploma,Not_in_LF,37.8
2011,High School Diploma ,Not_in_LF,26
2011,"Some College, No Degree ",Not_in_LF,22.9
2011,Associate Degree,Not_in_LF,18.3
2011,Bachelor’s Degree or Higher,Not_in_LF,14.9
2016,Less than a High School Diploma,Not_in_LF,39.3
2016,High School Diploma ,Not_in_LF,27.9
2016,"Some College, No Degree ",Not_in_LF,23.9
2016,Associate Degree,Not_in_LF,19.6
2016,Bachelor’s Degree or Higher,Not_in_LF,15
2021,Less than a High School Diploma,Not_in_LF,40.2
2021,High School Diploma ,Not_in_LF,28.9
2021,"Some College, No Degree ",Not_in_LF,24.5
2021,Associate Degree,Not_in_LF,20.4
2021,Bachelor’s Degree or Higher,Not_in_LF,14.6
The plot I was looking for is something like the following one:
Despite accomplishing the figure I needed, I wouldn’t say I liked the entire process because my code was inefficient. For example, if I want to insert the direct labels into the plot, I have to insert them manually; or if I want to change the nature of the groups (from Years to Levels), I have a lot of work to do. This is because to produce the figure above, I had to generate three individual plots (one for 2011, another for 2016, and one for 2021), and then merge them into a 1 x 3 subplot grid. This leads to another major trouble: I do not know how to impose a single set of legends that operates on all three subplots in the final output.
The code I used to produce the individual plots is the following (e.g., for 2021), and the data set is wide in this case (I am using Pluto and PlotlyBase.jl):
begin
y1c = [54.8, 66.6, 71.3, 75.9, 82.9] # to let direct labels
y2c = [ 5.1, 4.5, 4.2, 3.6, 2.5] # to let direct labels
y3c = [40.2, 28.9, 24.5, 20.4, 14.6] # to let direct labels
fig24 = Plot([bar(filter(row -> row.Year == 2021, data),
x="Years", y=y, name=string(y)) for y in [:Employment, :Unemployment, :Not] ])
relayout!(fig24, barmode="stack", title_text="2021", title_x=0.5, xaxis=attr(
tickmode = "array",
tickvals = [0, 1, 2, 3, 4],
ticktext = ["HS-", "HS", "Col-", "Pro", "Col+"]
))
restyle!(fig24, marker_color=["teelBlue", "darkblue", "Salmon"], text= [y1c, y2c, y3c], textposition="auto", showlegend=true)
fig24
end
Thanks for your concerns.