1

I'm a beginner at ggplot, and I tried to use it to draw some timeserie data. I want to draw bound_transporter_in_evolution.mean as a function of time, in different conditions where the attribute p_off (float) varies.

p4 <- ggplot(data=df, aes(x=timesteps.mean)) +
geom_line(aes(y=bound_transporter_in_evolution.mean, color=p_off)) +
xlab(label="Time (s)") +
ylab(label="Number of bound 'in' transporters")

ggsave("p4.pdf", width=8, height=3.3)

I get the following plot:

enter image description here

I expected this result, but with a line instead of points:

enter image description here

Thank you

jazzurro
  • 23,179
  • 35
  • 66
  • 76
  • can you post the output of `str(df)`? – agenis Mar 19 '15 at 13:46
  • since p_off is a numeric variable, ggplot will create only one line connecting all the dots and color it. If you want separated lines, you have do do `color=factor(p_off))` (assuming you have a limited number of different values). What do you get? – agenis Mar 19 '15 at 14:11
  • I obtain this: http://img42.com/wp3TO+. And when I try to have a gradient of color using scale_color_gradient(), I get the following error message: Discrete value supplied to continuous scale – user3488737 Mar 19 '15 at 14:20
  • well you cannot have it both ways! :-) you have to use a discrete scale and mesh a little bit with the scale values to get it right – agenis Mar 19 '15 at 14:27

2 Answers2

0

Here is the output of str(df):

'data.frame':   150010 obs. of  34 variables:
$ bound_transporter_evolution.low     : num [1:150010(1d)] 0 11.4 26.1 41.8 48.2 ...
$ bound_transporter_evolution.mean    : num [1:150010(1d)] 0 15 28.2 45 53.8 63.8 71.6 77.8 86.2 91.2 ...
$ bound_transporter_evolution.up      : num [1:150010(1d)] 0 18.6 30.3 48.2 59.4 ...
$ bound_transporter_in_evolution.low  : num [1:150010(1d)] 0 11.4 26.1 41.8 48.2 ...
$ bound_transporter_in_evolution.mean : num [1:150010(1d)] 0 15 28.2 45 53.8 63.8 71.6 77.8 86.2 91.2 ...
$ bound_transporter_in_evolution.up   : num [1:150010(1d)] 0 18.6 30.3 48.2 59.4 ...
$ bound_transporter_out_evolution.low : num [1:150010(1d)] 0 0 0 0 0 0 0 0 0 0 ...
$ bound_transporter_out_evolution.mean: num [1:150010(1d)] 0 0 0 0 0 0 0 0 0 0 ...
$ bound_transporter_out_evolution.up  : num [1:150010(1d)] 0 0 0 0 0 0 0 0 0 0 ...
$ free_transporter_evolution.low      : num [1:150010(1d)] 200 181 170 152 141 ...
$ free_transporter_evolution.mean     : num [1:150010(1d)] 200 185 172 155 146 ...
$ free_transporter_evolution.up       : num [1:150010(1d)] 200 189 174 158 152 ...
$ free_transporter_in_evolution.low   : num [1:150010(1d)] 186 172 158 139 127 ...
$ free_transporter_in_evolution.mean  : num [1:150010(1d)] 188 173 160 143 135 ...
$ free_transporter_in_evolution.up    : num [1:150010(1d)] 191 175 162 148 142 ...
$ free_transporter_out_evolution.low  : num [1:150010(1d)] 9.18 9.18 9.18 9.18 9.18 ...
$ free_transporter_out_evolution.mean : num [1:150010(1d)] 11.6 11.6 11.6 11.6 11.6 11.6 11.6 11.6 11.6 11.6 ...
$ free_transporter_out_evolution.up   : num [1:150010(1d)] 14 14 14 14 14 ...
$ glutamate_evolution.low             : num [1:150010(1d)] 2000 1981 1970 1951 1939 ...
$ glutamate_evolution.mean            : num [1:150010(1d)] 2000 1985 1971 1954 1943 ...
$ glutamate_evolution.up              : num [1:150010(1d)] 2000 1989 1973 1957 1948 ...
$ p_off                               : num [1:150010(1d)] 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 ...
$ simulation_name                     : Factor w/ 1 level "Variable p-off large diffusion-limited area": 1 1 1 1 1 1 1 1 1 1 ...
$ timesteps.low                       : num [1:150010(1d)] 0e+00 1e-06 2e-06 3e-06 4e-06 5e-06 6e-06 7e-06 8e-06 9e-06 ...
$ timesteps.mean                      : num [1:150010(1d)] 0e+00 1e-06 2e-06 3e-06 4e-06 5e-06 6e-06 7e-06 8e-06 9e-06 ...
$ timesteps.up                        : num [1:150010(1d)] 0e+00 1e-06 2e-06 3e-06 4e-06 5e-06 6e-06 7e-06 8e-06 9e-06 ...
$ transporter_in_evolution.low        : num [1:150010(1d)] 186 186 186 186 186 ...
$ transporter_in_evolution.mean       : num [1:150010(1d)] 188 188 188 188 188 ...
$ transporter_in_evolution.up         : num [1:150010(1d)] 191 191 191 191 191 ...
$ transporter_out_evolution.low       : num [1:150010(1d)] 9.18 9.18 9.18 9.18 9.18 ...
$ transporter_out_evolution.mean      : num [1:150010(1d)] 11.6 11.6 11.6 11.6 11.6 11.6 11.6 11.6 11.6 11.6 ...
$ transporter_out_evolution.up        : num [1:150010(1d)] 14 14 14 14 14 ...
$ variable_parameter                  : Factor w/ 1 level "p_off": 1 1 1 1 1 1 1 1 1 1 ...
$ variable_value                      : num [1:150010(1d)] 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 0.02 ...
0

since p_off is a numeric variable, ggplot will create only one line connecting all the dots and color it along the values. If you want separated lines, you have do transform your colouring variable into a factor(assuming you have a limited number of different values). Let's take an example with a numeric color variable:

df=data.frame(x=c(1:5, 1:5), y=rnorm(10), z=c(1,1,1,1,1,2,2,2,2,2))
ggplot(data=df, aes(x=x)) + geom_line(aes(x=x, y=y, color=z))

enter image description here

Which doesn't make any sense since consecutive points come from different categories. And now turn it into a factor:

ggplot(data=df, aes(x=x)) + geom_line(aes(x=x, y=y, color=factor(z)))

enter image description here

In your first graph, the line constantly goes from one p_off value to another, and since you have a really big dataset it quickly saturates the screen.

agenis
  • 8,069
  • 5
  • 53
  • 102
  • Thank you for your answer. But how can I have a gradient legend such as the one on your first plot, while using factor() ? – user3488737 Mar 19 '15 at 14:25
  • you can't. However, there is a workaround that is described in this answer: http://stackoverflow.com/a/15204709/3871924 (but it's kind of a hack:) Or you can also build a whole separate legend object and replace the initial legend with it, but it gets complicated... – agenis Mar 19 '15 at 14:32
  • Maybe try this: `p4 + scale_colour_manual(values=colorRampPalette(c("white", "blue"))(length(unique(df$z))))` – agenis Mar 19 '15 at 14:41