我用ggplot 0.8.9做了很多数字(这是我仍在运行的)。现在,我需要修改这些数字以包括图例。我遇到了各种难以解决的问题,因为我对themeopts以及许多适用于更高版本的SO答案感到非常困惑。

在这一点上,似乎我需要更新ggplot2并重写我的所有代码,以便在我的图上添加图例。这是真的?我已经阅读了ggplot2转换指南,它似乎是对的。

这是旧代码的样子(不产生图例):为了重复性,这里是数据:mean10v2stderr10

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)

最佳答案

实际上,更新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()theme()调用。

然后,如果要为每个组选择特定的颜色/线型,请使用缩放功能,而不要通过单独设置来进行设置:

+ scale_colour_manual(values = c('black','red2','blue3')) +
  scale_linetype_manual(values = c('solid','dashed','dotted'))

关于r - 使用“旧的” ggplot2代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18925434/

10-12 18:01