【问题标题】:Change ggplot legends更改 ggplot 图例
【发布时间】:2015-04-16 09:46:37
【问题描述】:

我对使用 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"))

我的目标是摆脱名为“因子(Sp)”的图例,并使轴的数字变为黑色而不是灰色。

提前致谢!请找到附上的示例图

【问题讨论】:

  • 欢迎来到 SO!您的任务看起来很简单,但如果您想要一个完整的解决方案,请通过添加您的数据的最小样本来创建 reproducible example
  • 能否提供给我们(部分)covartimesfinal2.txt?
  • 对不起!这是我的第一篇文章。有没有办法上传txt文件?

标签: r ggplot2 legend legend-properties


【解决方案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"))

【讨论】:

  • 谢谢!这正是我所需要的。只是为了好奇,有没有办法改变线条样式?
猜你喜欢
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
相关资源
最近更新 更多