2015-04-16 94 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