queryverse / VegaLite.jl

Julia bindings to Vega-Lite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

spaghetti chart with selected colors

ericqu opened this issue · comments

I hope this is the right place to ask a question.

I want to use vega-lite to make a chart using Julia.
With Plots.jl I do the following:
@df s_treat plot(:time, :value, group=(:ID), legend=false, colour=:color)
and this gives me the lines in the color I already put in the DataFrame per ID.
plots

I tried the following in vega:
s_treat |> @vlplot( :line, x=:time, y=:value, color="ID" )
vega

But vega tries to infer a color from the "ID" field.
Instead, I would like vega to use the color in the "color" field; how could I achieve this?


I forgot to mention that I am using:

  • Julia 1.6.1
  • VegaLite v2.4.1

Have you just tried s_treat |> @vlplot( :line, x=:time, y=:value, color=:color )?

I am not sure I understand the question, though, primarily because I don't know what the structure of the input data is. If you could show us a bit of the data in your input table, it would help :)

Certainly, the original data is described here
I modified it so that there is a line per data point and a column time containing the number of days since the exposition.

ID	Treatment	variable	value	time	color
Int64	String		String		Float64	Int64	Symbol
1	"P"		"0"		30.8	0	:royalblue
4	"P"		"0"		24.7	0	:royalblue
7	"P"		"0"		28.6	0	:royalblue
8	"P"		"0"		33.7	0	:royalblue
9	"P"		"0"		19.7	0	:royalblue
10	"P"		"0"		31.1	0	:royalblue
11	"P"		"0"		19.8	0	:royalblue
13	"P"		"0"		21.4	0	:royalblue
15	"P"		"0"		21.1	0	:royalblue
16	"P"		"0"		20.6	0	:royalblue
more
100	"A"		"6"		12.3	6	:darkorange

In short, I would like to display per individual the evolution of the variable of interest with regards to time, but having a different color for the individual with placebo and the ones with the active component.

I tried the suggestion above and unfortunately, it doesn't give adequate results, see below.

suggestion2021-05-03 18-45-09

I see what you're trying to do here; think it might be easier to do this by keeping data and representation separate. In this case, keep the original categorical variable (or turn it into an ordinal one) and then use a scale to bind colors to it, you can check out an example here:

https://www.queryverse.org/VegaLite.jl/stable/examples/examples_barcharts/

Thank you, I got the feeling it is progressing slightly.
However, I am missing something, as I am not sure that the keyword "color" should serve two purposes:

  • grouping by the value
  • indicating the color to use

In the plot example (@df s_treat plot(:time, :value, group=(:ID), legend=false, colour=:color)), the keyword: "group" enables the grouping on the ID so that there is a line per individual. And the keyword "colour" assigns colors.

In the Vega example, I do not see (I could not find an example in the bar-charts where the color was used for grouping by a field and set the color from another field) how to do these two actions through the color keyword.

In the below Vega tests, on the first one, it is grouped by "ID" which is good, but the color is not coming from the columns "Treatment" or "Colors". On the second one, the color is correct, but the grouping seems somehow done at the "Treatment" level and no at the "ID".

So I guess the question is: should I try to use "color" for grouping and coloring? And if so, how?
Screenshot from 2021-05-04 00-57-15

Apologies if I am not explaining it right, I am relatively new to it.

Try adding detail=:id to the bottom plot - that might do the trick.

Excellent! thank you, it does the trick.