R 添加额外的图例到绘图

R 添加额外的图例到绘图,r,plot,ggplot2,R,Plot,Ggplot2,嗨,我可以在ggplot中添加一个额外的图例吗。 喜欢 下面的代码 d <- melt(as.matrix(data.frame(y1=1/(1:10),y2=1/(10:1)))) ggplot(d, aes(x=Var1, y=value,fill=Var2)) + geom_bar(stat="identity",position='dodge') d这似乎适合我 plot.new() d <- melt(as.matrix(data.frame(y1=1/(

嗨,我可以在ggplot中添加一个额外的图例吗。 喜欢 下面的代码

d <- melt(as.matrix(data.frame(y1=1/(1:10),y2=1/(10:1))))
ggplot(d, aes(x=Var1, y=value,fill=Var2)) + geom_bar(stat="identity",position='dodge')

d这似乎适合我

    plot.new()
    d <- melt(as.matrix(data.frame(y1=1/(1:10),y2=1/(10:1))))
    ggplot(d, aes(x=Var1, y=value,fill=Var2)) + 
    geom_bar(stat="identity",position='dodge')
我更改了apply语句以处理数据


我不知道ggplot解决方案,但我希望看到一个。

您可以在绘图面板中添加一些带有
注释的文本,或者在设备窗口的任何位置添加
网格。文本
。我无法使第二部分使用示例图例('top',paste(“sums:,tapply(d$value,d$Var2,sum)))strwidth(图例,单位=“user”,cex=cex,font=text.font):plot.new没有被调用yetSorry。我编辑了上面的帖子,调用“plot.new()”firstOk,我不知道我可以在ggplot上使用基础图形,只需调用plot.new first。如果你更改图例的坐标,你会看到这种方法的一大缺点。
    plot.new()
    d <- melt(as.matrix(data.frame(y1=1/(1:10),y2=1/(10:1))))
    ggplot(d, aes(x=Var1, y=value,fill=Var2)) + 
    geom_bar(stat="identity",position='dodge')
  legend('top',paste("sums:",tapply(d$value,d$Var2,sum)))