2015-04-16 58 views
-1

我相对较新使用ggplot包。我想使用名称“Sp1”和“Sp2”来重命名图的图例。我试图使用下面的代码,但我一直无法做到这一点。更改ggplot传说

这是代码:

t<-read.table ("covartimesfinal2.txt", header=T) 

attach(t) 

p <- ggplot(t,aes(x=Ratio,y=Time)) + geom_point(aes(shape=factor(Sp))) 

p + geom_smooth(aes(linetype=factor(Sp),),colour="black", method='lm', 

se=F)+theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),panel.background = element_blank(), axis.line = 

element_line(colour = "black"))+ 

scale_shape_discrete(name ="Species",labels=c("Sp1", "Sp2")) 

我的目的就是要摆脱名为“因子(SP)”的传说,使轴的数量黑色和灰色没有。

在此先感谢!附上一个样地

enter image description here

+1

欢迎来到SO!你的任务看起来很容易,但如果你想要一个完整的解决方案,请做一个[可重现的例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-例如)通过添加一个最小的数据样本。 – tonytonov

+1

是否可以向我们提供(部分)covartimesfinal2.txt? –

+0

对不起!这是我的第一篇文章。有没有办法上传txt文件? – user12257

回答

1

下丢弃不必要的图例标签,我创建了一个自己的数据例如:

数据例如

t<-data.frame(Ratio=c(1:10,1:10), Time=c(1:10,11:20), Sp=as.factor(c(rep("H", 10), rep("N", 10)))) 

Ggplot

library(ggplot2) 

p <- ggplot(t,aes(x=Ratio,y=Time, group=Sp, shape=Sp)) + geom_point() + geom_line() 

p <- p + scale_shape_discrete(name="Species",labels=c("Sp1", "Sp2")) 

p <- p + theme(axis.line=element_line(colour = "black"), axis.text=element_text(colour="black")) 

enter image description here

+0

谢谢!这正是我所需要的。只是为了好奇,有没有办法改变线条样式呢? – user12257

+1

http://www.cookbook-r.com/Graphs/Shapes_and_line_types/ – MichaelVE