改变ggplot传说

时间:2015-04-16 09:46:37

标签: r ggplot2 legend legend-properties

我使用ggplot包相对较新。我想使用名称" Sp1"重命名情节的图例。和" Sp2"。我试图使用以下代码制作它,但我无法做到。

这是代码:

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

我的目标是摆脱名为&#34; factor(Sp)&#34;并使轴的数字变黑而不是灰色。

提前致谢!请附上一份样本图

enter image description here

1 个答案:

答案 0 :(得分: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