|
| 1 | +# --- |
| 2 | +# title: Label Bars |
| 3 | +# id: demo_bar_chart4 |
| 4 | +# description: Label Bars and related keyword arguments |
| 5 | +# cover: assets/bar_chart4.svg |
| 6 | +# --- |
| 7 | + |
| 8 | +using InMemoryDatasets, StatisticalGraphics, DLMReader |
| 9 | + |
| 10 | +svg("assets/bar_chart4.svg", sgplot(Dataset(x=repeat(1:5, inner=2), y=[5,6,7,8,6,3,2,4,2,1], g=repeat(1:2, 5),z=[-5,0,-7,0,-6,0,-4,0,-2,0]), Bar(y=:x, response=:y, group=:g, normalize=true, label=:height, labeld3format=".0%", labelpos=:middle, labelcolor=:auto), nominal=:g, yaxis=Axis(reverse=true, domain=false, titlecolor=:white, labelcolor=:white, tickcolor=:white), xaxis=Axis(domain=true, offset=5, titlecolor=:white, tickcolor=:white, d3format="%"), width=100, height=100, legend=false, groupcolormodel=Dict(:scheme=>:darkred))) #hide #md |
| 11 | + |
| 12 | + |
| 13 | +# Change the location and postion of labels |
| 14 | + |
| 15 | +cars = filereader(joinpath(dirname(pathof(StatisticalGraphics)), |
| 16 | + "..", "docs", "assets", "cars.csv")) |
| 17 | + |
| 18 | +sgplot(cars, Bar(y=:Origin, barwidth=0.7, label=:category, |
| 19 | + labelpos=:start, labelloc=0, barcorner=[0,5,0,5]), |
| 20 | + yaxis=Axis(show=false), |
| 21 | + clip=false, |
| 22 | + height=200, |
| 23 | + width=200) |
| 24 | + |
| 25 | + |
| 26 | +# Using the multiple charts to set multiple labels |
| 27 | + |
| 28 | +sgplot(cars, [ |
| 29 | + Bar(y=:Origin, barwidth=0.7, label=:category, |
| 30 | + labelpos=:start, labelloc=0, barcorner=[0,5,0,5]), |
| 31 | + Bar(y=:Origin, opacity=0, label=:height, labelcolor=:white, |
| 32 | + labeloffset=-10) |
| 33 | + ], |
| 34 | + |
| 35 | + yaxis=Axis(show=false), |
| 36 | + clip=false, |
| 37 | + height=200, |
| 38 | + width=200) |
| 39 | + |
| 40 | + |
| 41 | +# Label bars with negative heights |
| 42 | + |
| 43 | +ds = Dataset(x=1:5, y=[1.2,-3.1,2,4.3,-.1]) |
| 44 | + |
| 45 | +sgplot(ds, Bar(x=:x, response=:y, label=:height, |
| 46 | + labelbaseline=:bottom), |
| 47 | + yaxis=Axis(title="y"), |
| 48 | + width=200 |
| 49 | +) |
| 50 | + |
| 51 | +# Label grouped bar charts |
| 52 | + |
| 53 | +population = filereader(joinpath(dirname(pathof(StatisticalGraphics)), |
| 54 | + "..", "docs", "assets", "population.csv")) |
| 55 | + |
| 56 | +sex(x) = x == 1 ? "male" : "female" # format for :sex |
| 57 | + |
| 58 | +setformat!(population, :sex => sex) |
| 59 | + |
| 60 | +pop2000 = filter(population, :year, by = ==(2000)) |
| 61 | + |
| 62 | +sgplot(pop2000, Bar(y=:age, response=:people, |
| 63 | + group=:sex, normalize=true, label=:height, |
| 64 | + labelpos=:middle, labeld3format=".1%"), |
| 65 | + xaxis=Axis(title="Population", d3format="%"), |
| 66 | + yaxis=Axis(reverse=true)) |
| 67 | + |
| 68 | +# Simulated data |
| 69 | + |
| 70 | +ds = Dataset(rand(1:4, 100, 2), :auto) |
| 71 | + |
| 72 | +sgplot(ds, Bar(x=:x1, group=:x2, label=:height), nominal=:x2) |
| 73 | + |
| 74 | +# Passing `labelcolor=:auto` to assign label color based on the contrast of colors |
| 75 | + |
| 76 | +sgplot(ds, Bar(x=:x1, group=:x2, label=:height, labelcolor=:auto), nominal=:x2) |
0 commit comments