删除图例''填充框''长方形

时间:2018-05-13 16:03:10

标签: r plot legend

par(mar=c(10.1,4.1,4.1,8.1), xpd =TRUE)
barplot(cp_ct2, beside = TRUE, col = c("darkblue","darkcyan"),las =2)

legend("topright",inset = c(-0.6,0), legend = c("Slight","Serious","",
                                            "Chi2: 226","P.Val:0.000"),
fill = c("darkblue","darkcyan"))

实际上,这个情节在我的传奇中产生了5个矩形框。我只想要两个。执行此代码时,每个框都会填充深蓝色,深蓝色的颜色。我只需要轻微的因素'''''''''''''''''填充并且chi-pvalue不存在任何矩形。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用NA而不是填充/颜色等来省略图例中的对象

所以对你的例子来说:

legend("topright", 
       legend = c("Slight","Serious","","Chi2: 226","P.Val:0.000"),
       fill = c( "darkblue","darkcyan",NA, NA, NA), 
       border= c( "darkblue","darkcyan", NA, NA, NA ))

在这种情况下,使用text函数添加额外的统计信息可能会更好/更容易,而不是强制进入图例。

一个类似的例子,

par(xpd =TRUE)
cp_ct2 <- with(mtcars, table(vs, am))
X <- chisq.test(cp_ct2)

barplot(cp_ct2, beside = TRUE, col = c("darkblue","darkcyan"),las=1)

legend("topright", 
       legend = c("Slight","Serious"),
       fill = c( "darkblue","darkcyan"), 
       border= c( "darkblue","darkcyan"))

# add text to margin
mtext(side=1, line=3, substitute(chi^2==x~';'~~p==y, 
                                list(x=round(X$statistic, 2), y=round(X$p.value, 3))))