tags:

views:

652

answers:

1

I have a plot (sample code pasted below) that I am trying to add by own labels for the series information. Instead of plotting "p1s1" "p1s2" "p3s4", I would like "treatment 1" "treatment 2" "treatment 3". I have used levels(series_id) to get the unique series names and used a lookup table to get the descriptions. (I think this gets them in the same order they are plotted?) and I have these descriptions in a vector called treatment_descriptions.

From the documentation I think that I should be using a scale here, but I can't figure out which one, or how to do it. Something like: scale_something(name="Treatment Descriptions", breaks=NULL, labels=treatment_descriptions, formatter=NULL) ? But where should this go?

library(ggplot2)

# Create a long data.frame to store data...
growth_series = data.frame ("read_day" = c(0, 3, 9, 0, 3, 9, 0, 2, 8), 
"series_id" = c("p1s1", "p1s1", "p1s1", "p1s2", "p1s2", "p1s2", "p3s4", "p3s4", "p3s4"),
"mean_od" = c(0.6, 0.9, 1.3, 0.3, 0.6, 1.0, 0.2, 0.5, 1.2),
"sd_od" = c(0.1, 0.2, 0.2, 0.1, 0.1, 0.3, 0.04, 0.1, 0.3),
"n_in_stat" = c(8, 8, 8, 8, 7, 5, 8, 7, 2)
)

> # Now gives us some example long form data...
> > growth_series 
>  read_day series_id mean_od sd_od        n_in_stat   
>   1       p1s1     0.6      0.10         8 2       
>   3       p1s1     0.9      0.20         8 3    
>   9       p1s1     1.3      0.20         8 4    
>   0       p1s2     0.3      0.10         8 5    
>   3       p1s2     0.6      0.10         7 6    
>   9       p1s2     1.0      0.30         5 7    
>   0       p3s4     0.2      0.04         8 8    
>   2       p3s4     0.5      0.10         7 9    
>   8       p3s4     1.2      0.30         2 2

# Plot using ggplot...
ggplot(data = growth_series, aes(x = read_day, y = mean_od, group = series_id, color = series_id)) +
geom_line(size = 1.5) +
geom_point(aes(size = n_in_stat)) +
geom_errorbar(aes(ymin = mean_od - sd_od, ymax = mean_od + sd_od), size = 1, width = 0.3) +
xlab("Days") + ylab("Log (O.D. 730 nm)") +
scale_y_log2() +
scale_colour_hue('my legend', breaks = levels(growth_series$series_id), labels=c('t1', 't2', 't3'))
+2  A: 

maybe you can relabel your factor?

growth_series$series_id <- factor(
     growth_series$series_id, 
     labels=c('treatment 1', 't2', 't3'))

Or if you are still looking for scale_something, it should be scale_colour_hue()

... + scale_colour_hue('my legend', 
   breaks = levels(growth_series$series_id), 
   labels=c('t1', 't2', 't3'))
xiechao
Thanks, the first suggestion worked, the second did not, I'll keep the question open for now, as I think there should be a better way to do it. My descriptions are quite long, so having them in the table like that looks really ugly, but I guess I don't have to put them into the table until just before I plot, so it is not big deal. Cheers, John
John
What is wrong with the second one? By the way, the "..." represents your group of ggplot() + geom_line + .. + scale_y_log2
xiechao
Sure, I tried that, but I get a syntax error, did it work for you?
John
it works for me. If no one share other solutions, do you mind append the commands that you have run to your question?
xiechao
Thanks, now works, must have been a hidden character before I think, when copy pasting from Stackoverflow. Many thanks
John