2

I made ton of figures with ggplot 0.8.9 (which is what I'm still running). Now I need to modify these figures to include legends. I am running across all sorts of problems that are hard to solve because I am getting really confused about theme and opts and many SO answers that apply to later versions.

At this point, it seems like I need to update ggplot2 and rewrite all of my code just so I can have legends on my figures. Is this true? I've read the ggplot2 transition guide, it makes it seem true.

Here's what the old code looks like (does not produce a legend): And here is the data for the sake of reproducibility: mean10v2 and stderr10.

me10<-read.table("mean10v2.txt", header=TRUE)
se10<-read.table("stderr10.txt", header=TRUE)

ggplot() +
geom_ribbon(aes(x = me10[me10$trt=="CC", "tu"], ymin=(me10[me10$trt=="CC", "biomassA"]-    
  se10[se10$trt=="CC", "biomassA"]), ymax=(me10[me10$trt=="CC",  
  "biomassA"]+se10[se10$trt=="CC", "biomassA"])), alpha=0.25) +
geom_line(aes(me10[me10$trt=="CC", "tu"], y=me10[me10$trt=="CC", "biomassA"]), size=1)+  
geom_ribbon(aes(x = me10[me10$trt=="PF", "tu"], ymin=(me10[me10$trt=="PF", "biomassA"]- 
  se10[se10$trt=="PF", "biomassA"]), ymax=(me10[me10$trt=="PF", 
  "biomassA"]+se10[se10$trt=="PF", "biomassA"])), alpha=0.25) +
geom_line(aes(me10[me10$trt=="PF", "tu"], y=me10[me10$trt=="PF", "biomassA"]), 
  colour="red2", linetype="dashed", size=1) + 
geom_ribbon(aes(x = me10[me10$trt=="P", "tu"], ymin=(me10[me10$trt=="P", "biomassA"]-
  se10[se10$trt=="P", "biomassA"]), ymax=(me10[me10$trt=="P",   
  "biomassA"]+se10[se10$trt=="P", "biomassA"])), alpha=0.25) +
geom_line(aes(me10[me10$trt=="P", "tu"], y=me10[me10$trt=="P", "biomassA"]),    
  colour="blue3", linetype="dotted", size=1) + 
opts(panel.grid.minor = theme_blank()) +
opts(panel.grid.major = theme_blank()) +
opts(panel.background = theme_blank()) +
opts(axis.line = theme_segment()) +
opts(legend.position=c(.5,.5)) +
opts(axis.title.x = theme_text(size=12,vjust=-0.5)) +
opts(axis.title.y = theme_text(size=12,angle=90)) +
opts(axis.text.x = theme_text(colour="black", size=16)) +
opts(axis.text.y = theme_text(colour="black", size=16)) +
annotate("text", x = -Inf, y = Inf, label = "a", face="bold", hjust = -5, vjust=2, size     
  = 9) + 
ylab("") +
xlab("") +
ylim(0,2200)

1 Answer 1

9

Updating the theme parts is actually quite simple. You really just need to change opts() to theme() and replace theme_* with element_*. Some other names have changed, like you'll use element_line instead of theme_segment.

But more generally, you're using ggplot all wrong:

my_df <- me10[,c('trt','tu','biomassA')]
my_se <- setNames(se10[,c('trt','tu','biomassA')],c('trt','tu','se'))

my_df <- merge(my_df,my_se)

ggplot(data = my_df,aes(x = tu,y = biomassA)) + 
    geom_ribbon(aes(group = trt,ymin = biomassA - se,ymax = biomassA + se),alpha = 0.25) + 
    geom_line(aes(group = trt,linetype = trt,colour = trt)) + 
    labs(x = "",y = "") + 
    ylim(0,2200) + 
    theme(panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.background = element_blank(),
          axis.line = element_line(),
          legend.position=c(.5,.5),
          axis.title.x = element_text(size=12,vjust=-0.5),
          axis.title.y = element_text(size=12,angle=90),
          axis.text.x = element_text(colour="black", size=16),
          axis.text.y = element_text(colour="black", size=16)) + 
    annotate("text", x = -Inf, y = Inf, label = "a", face="bold", hjust = -5, vjust=2, size = 9)

enter image description here

Notice how much cleaner that is, and putting the data into an appropriate form only took three lines. Also note that there is absolutely no need to keep repeating the opts() or theme() calls for every....single....thing...you....set.

And then if you want to choose specific colors/linetypes for each group, you do that using the scale functions, not by setting them individually:

+ scale_colour_manual(values = c('black','red2','blue3')) + 
  scale_linetype_manual(values = c('solid','dashed','dotted'))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, @joran, this is such a huge help with this specific problem and with understanding ggplot2 better in general. I bought the book, but I struggle with applying the examples to my own data. This is really valuable to me.
@Nazer No problem. Figuring out the switch from opts to theme should generally just involve carefully studying the options listed in ?theme. The switch to 0.9.0 had many big changes that were documented in some detail here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.