无法在R中为简单散点图创建图例:不接受legend = c(“name”,“name”)

时间:2017-05-11 14:53:32

标签: r legend

有人可以帮我弄清楚为什么我不能在R中创建一个传奇?我一直在尝试DAYS。谷歌搜索和观看视频。我知道这段代码适用于其他人,但不适合我。我正在使用CSV文件。无法使用TXT文件,它将无法正确导入。 (我有Mac OS)

一直在说

  

“as.graphicsAnnot(图例)中的错误:缺少参数”legend“,没有默认值”

我尝试了很多东西。我只想用颜色来表示治疗和控制。 (绿色=治疗,黑色=控制)。形状为pch=19(以圆圈填充)。它显然与legend=c("Treatment", "Control")有误,但我不知道为什么。

这是我整个情节的代码:

plot(DEPTH, FISH, main="Fish < 3cm vs. Depth", xlab="Depth", ylab="Fish >3cm CPUE ", pch=19, abline(lm(FISH~DEPTH), col="black"), col=ifelse(CSV_Fish_3_vs_Depth_Minnow$SEP== 2, "green", "black"), legend(20,35), legend=c("Treatment","Control"), col=c("green", "black"), pch=19:19, cex=.8, box.col="darkgreen")

这是我的传奇代码(不起作用的部分):

legend(20,35), legend=c("Treatment","Control"), col=c("green", "black"), pch=19:19, cex=.8, box.col="darkgreen")

我的数据如下。非常简单。 SEP列用于在ifelse参数中分隔处理和控制。不知道为什么不接受它:

MACHOL  DEPTH   FISH    SEP
M1E     59      4.5      1
M1W     45      5.5      1
M2E     42      5.25     2
M2W     25      1.5      2
M3E     20      2.25     2
M3W     43      8.75     2
M4E     35      1.25     1
M4W     30      0.5      1

2 个答案:

答案 0 :(得分:1)

使用legend()作为函数,而不是plot()。

试试这个:

plot(...)   #plot your data

legend("left", c("Treatment", "Control"), 
       col = c("green", "black"),
       pch = c(19, 19), cex = 0.8, 
       box.col = "darkgreen"
       )

答案 1 :(得分:0)

尝试以下方法:

df = read.table(text="
MACHOL DEPTH FISH SEP 
M1E 59 4.5 1 
M1W 45 5.5 1 
M2E 42 5.25 2 
M2W 25 1.5 2 
M3E 20 2.25 2 
M3W 43 8.75 2 
M4E 35 1.25 1 
M4W 30 0.5 1",header=T)

plot(df$DEPTH, df$FISH, main="Fish < 3cm vs. Depth", xlab="Depth",
 ylab="Fish >3cm CPUE ", pch=19, abline(lm(df$FISH~df$DEPTH), col="black"), 
 col=ifelse(df$SEP== 2, "green", "black")) 

legend(30,8,legend=c("Treatment","Control"), col=c("green", "black"), pch=19, cex=.8, box.col="darkgreen")