[英]Cannot Create Legend in R for Simple Scatterplot: Will not accept legend=c(“name”, “name”)
有人可以帮我弄清楚为什么我不能在R中创建图例吗? 我一直在尝试DAYS。 谷歌搜索和观看视频。 我知道此代码适用于其他人,但不适用于我。 我正在使用CSV文件。 无法使用TXT文件,它将无法正确导入。 (我有Mac OS)
一直在说
“ as.graphicsAnnot(legend)中的错误:缺少参数“ 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
使用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"
)
请尝试以下操作:
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.