Gadfly: adding plots to an existing plot

3,122 views
Skip to first unread message

Sven Mesecke

unread,
Sep 18, 2013, 9:29:47 AM9/18/13
to julia...@googlegroups.com
I would like to use Gadfly to plot some of my data, either as a subplot or by adding multiple lines to a plot (see http://sveme.org/julia-for-matlab-users-i.html and http://sveme.org/julia-for-matlab-users-ii.html) for how I did it using Winston. I seem to remember that layer() and compose() have been suggested, but I really struggle to get it to work right.
Anyone with a suggestion on how to get something like (in matlab):

plot(t,x1, t,x2, t,x3)

done in Gadfly?

I tried

1=layer(data,x="time",y="R",Geom.line)

where `data` is a dataframe array, however, I seem to have a thorough misunderstanding of how Gadfly works.

Thanks a lot,

Sven

Daniel Jones

unread,
Sep 19, 2013, 4:25:05 AM9/19/13
to julia...@googlegroups.com
Hi Sven,

There are few ways to go about this depending what you want.

If you want multiple lines sharing the same axis, you can do something like this:

plot(dataframe, layer(x="x1", y="y1", Geom.line), layer(x="x2", y="y2", Geom.line))

Where "dataframe" is a DataFrame that has columns x1, y1, x2, y2.

If you want to put two entirely separate plots side by side, you can use the "hstack" or "vstack" functions like so:

hstack(plot(data, x="x1", y="y1", Geom.line), plot(data, x="x2", y="y2", Geom.line))

If you want to draw multiple similar plots grouped by one or two categorical variables in a DataFrame, you can use Geom.subplot_grid, which is actually documented, unlike layer and hstack/vstack.

Sven Mesecke

unread,
Sep 19, 2013, 7:43:28 AM9/19/13
to julia...@googlegroups.com
Hi Daniel,

thanks for the help. Is it possible to influence the color being used for the plots? Or should I rather change my way of thinking and stack the data on top of each other so that I can use grouping variables instead? I.e., use a DataFrame that contains only "Time" and "y", then "observable" as a grouping variable? I assume this is how it is intended to be used, right?

Thanks again,

Sven

Daniel Jones

unread,
Sep 19, 2013, 3:06:19 PM9/19/13
to julia...@googlegroups.com

Grouping data with categorical variables is the more idiomatic way of doing this. It's possible possible to manually change things like colors, but I don't really make it easy (partly because I don't want to encourage that, but mainly because I just haven't gotten around to it).

You can can also draw separate plots, each with a separate color. Here's an example of that.

Stéphane Laurent

unread,
Jun 10, 2014, 5:00:04 AM6/10/14
to julia...@googlegroups.com
Hello is there something in Gadfly like the geom_abline() function in the ggplot2 R package to add a single line to an existing plot ?

Stéphane Laurent

unread,
Jun 10, 2014, 5:56:19 AM6/10/14
to julia...@googlegroups.com
Mmm no really need in fact, it's easy with layer().
However I don't find how to control the color of a layer ?

Johan Sigfrids

unread,
Jun 10, 2014, 6:13:46 AM6/10/14
to julia...@googlegroups.com
I think you can do it with: layer(..., Theme(default_color=color("red")))

Stéphane Laurent

unread,
Jun 10, 2014, 8:40:40 AM6/10/14
to julia...@googlegroups.com
Right, it works. Thanks !

AVF

unread,
Dec 31, 2014, 3:26:49 AM12/31/14
to julia...@googlegroups.com
So, if I created a plot in a block of code, and then want to add another array of data to it in another block of code, is that possible? (I.e., I find it inconvenient that I have only one chance to plot all the layers... or did I misunderstand how it works?)

Daniel Jones

unread,
Dec 31, 2014, 12:01:36 PM12/31/14
to julia...@googlegroups.com

It's not really supported. Adding a "push!" function to add layers to plots was proposed (https://github.com/dcjones/Gadfly.jl/issues/332), but I haven't done so yet. In fact, adding layers to a plot p will usually work with "push!(p.layers, layer(...))", but that's kind of an unofficial solution.

AVF

unread,
Dec 31, 2014, 8:58:15 PM12/31/14
to julia...@googlegroups.com
Sorry, doesn't seem to work:

a = rand(10)
b = rand(10)
c = rand(10)
d = rand(10)

p = plot(x=a, y=b)
push!(p.layers, layer(x=c, y=d))
`convert` has no method matching convert(::Type{Layer}, ::Array{Layer,1})
while loading In[185], in expression starting on line 9

 in push! at array.jl:457

As an aside, is there a way to do something like:

[a b c d] = rand(10,4)
 

Daniel Jones

unread,
Dec 31, 2014, 9:25:47 PM12/31/14
to julia...@googlegroups.com
My mistake, it should work with append! instead of push!.

For your second question, here's one option:
a, b, c, d = [rand(10) for _ in 1:4]

AVF

unread,
Jan 1, 2015, 4:28:14 PM1/1/15
to julia...@googlegroups.com
Thanks!
Reply all
Reply to author
Forward
0 new messages