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)
4

1 回答 1

9

更新theme部件实际上非常简单。您真的只需要更改opts()theme()并替换theme_*element_*. 其他一些名称已更改,例如您将使用element_line而不是theme_segment.

但更一般地说,你使用 ggplot 都错了:

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)

在此处输入图像描述

请注意这是多么干净,并且将数据放入适当的形式只需要三行。另请注意,绝对没有必要为每个....单个....事物...您....设置重复opts()or调用。theme()

然后,如果您想为每个组选择特定的颜色/线型,您可以使用缩放功能,而不是单独设置它们:

+ scale_colour_manual(values = c('black','red2','blue3')) + 
  scale_linetype_manual(values = c('solid','dashed','dotted'))
于 2013-09-20T21:02:44.450 回答