我正在尝试绘制一个条形图,但图例位于右上方,并且被条形图阴影。如何将图例移动到topleft或缩小其尺寸以使其正确适合右上侧。下面是代码和图片。
M<-read.delim("~/Desktop/sample.txt",row.names="Methods")
M<-as.matrix(M)
barplot(M, main="Obs by different methods at different replicates",
xlab="Number of observations", col=c("darkblue","red","green","yellow"),ylab="Number of stages",
legend = rownames(M), beside=TRUE)
如何更改代码以使其正确适合?
答案 0 :(得分:1)
barplot()
通过调用legend()
函数添加了一个图例,该函数非常灵活,您可以通过提供x
和{{1}来准确指定图例的绘制位置}坐标或描述性关键字(详情请查看y
)。
您可以直接致电?legend
:
legend()
或通过barplot(VADeaths, beside = TRUE, ylim = c(0, 100), col = 1:5)
legend("topleft", legend = rownames(VADeaths), fill = 1:5)
将参数传递给legend()
:
barplot()
答案 1 :(得分:0)
使用对legend()
功能的特定调用将其移至'topleft'
角落。例如:
M<-read.delim("~/Desktop/sample.txt",row.names="Methods")
M<-as.matrix(M)
barplot(M, main="Obs by different methods at different replicates",
xlab="Number of observations", col=c("darkblue","red","green","yellow"),ylab="Number of stages", beside=TRUE)
legend('topleft', rownames(M), fill=c("darkblue","red","green","yellow"))
这有帮助吗?
否则,您还可以使用图例的其他参数,例如在其调用中放置cex=
。我会通过在R控制台中输入?legend
来让您发现它们。