Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vertical bands like vspan() in Plots.jl #1174

Closed
mm3509 opened this issue Jul 11, 2018 · 7 comments
Closed

Add vertical bands like vspan() in Plots.jl #1174

mm3509 opened this issue Jul 11, 2018 · 7 comments

Comments

@mm3509
Copy link

mm3509 commented Jul 11, 2018

I would like to easily plot vertical bands, e.g. as a background for a recession in a time-series plot. Plots.jl has vspan(). In Gadfly, it requires a lot of code and mixes the color of the bands with the color of the time-series line.

For reference, the code is below, copied from StackOverflow:

using Gadfly, DataFrames, Colors

df = DataFrame(year = [1990; 2000], x = [0; 1], color = [1; 1])

x_shade = [1995 1995 1996 1996]
y_shade = [0 1 1 0]

theme = Theme(
    discrete_highlight_color = u -> ARGB(1, 1, 1, 0),
    default_color = colorant"grey")

p = plot(
    layer(
        x = x_shade,
        y = y_shade,
        Geom.polygon(preserve_order = true, fill = true),
        order = 1
    ),
    layer(df,
          y = "x",
          x = "year",
          color = "color",
          Geom.line,
          order = 2
          ),
    theme
)
@bjarthur
Copy link
Member

this produces identical output:

plot(df, y = "x", x = "year", color = "color", Geom.line,
     xintercept=[1995.5], Geom.vline(color="grey",size=[10]))

the only problem is that size can not currently be specified in the plot coordinate system.

@mm3509
Copy link
Author

mm3509 commented Jul 11, 2018

Thanks @bjarthur ! In my specific case, that it is a problem: I am judging the behavior of a time-series depending around the edges of those boxes. So I would need to specify it the size in the original plot, e.g. size = 1. I would rather avoid hacks to convert between the plot coordinate system and the aesthetics system.

@Mattriks
Copy link
Member

Geom.rect can also be used:

using RDatasets, Gadfly

Dp = dataset("ggplot2","presidential")[3:end,:]
De = dataset("ggplot2","economics")
De[:Unemploy] /= 10^3
Dp[:y1] = 0; Dp[:y2] = 12
theme(x) = style(default_color=parse(Colors.Colorant, x))

p = plot(Dp, xmin=:Start, xmax=:End, ymin=:y1, ymax=:y2, Geom.rect, color=:Party,
    layer(De, x=:Date, y=:Unemploy, Geom.line, order=2, theme("black")),
    Scale.color_discrete_manual("deepskyblue", "lightcoral"),
    Coord.cartesian(xmin=Date("1965-01-01"), ymax=12),
    Guide.xlabel("Time"), Guide.ylabel("Unemployment (x10³)"), Guide.colorkey(title=""),
    Theme(key_position=:top)
)

issue1174

@Mattriks
Copy link
Member

And here is similar plot code in R, for comparison:

p = ggplot(presidential)+geom_rect(aes(xmin=start, xmax=end, ymin=-Inf, ymax=Inf, fill=party))+
    geom_line(data=economics, aes(x=date, y=unemploy))+
    scale_fill_manual(values=c("deepskyblue", "lightcoral"))+
    coord_cartesian(xlim=as.Date(c("1965-01-01", "2009-12-31")), ylim=c(0,12000), expand=F)+
    labs(x="Time", y="Unemployment", fill="")+
    theme(legend.position="top")

@Mattriks
Copy link
Member

And another way, which uses Geom.vline (as in @bjarthur's example), but with context units: cx, cy from Compose. Yes, this allows size to be specified in the same units as x, y. In the plot below, note how the bar width matches the point distance = 1 year.

using Compose, DataFrames, Gadfly
srand(123)
df = DataFrame(year=1990:2000, x = rand(11))

p = plot(df, x=:year, y=:x, xintercept=[1993.5, 1995.5, 1997.5], 
    Geom.line, Geom.point, Geom.vline(color="lightgrey", size=[1cx]))

iss1174b
Should add this to the docs!

@bjarthur
Copy link
Member

of course! i should've thought of that!! @miguelmorin if this solves your problem please close this issue

@mm3509
Copy link
Author

mm3509 commented Jul 19, 2018

Thanks @Mattriks ! Your solution with Geom.rect worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants