How do I get rid of grids when using plot() in R?

血红的双手。 提交于 2021-02-09 09:13:47

问题


So I'm using R to perform a DCA (detrended correspondence analysis) through Vegan package and everytime I plot my results I get a grid in the middle of the plot.

I want to get rid of it.

Here is my code:

dca <- decorana(dados)

plot(dca, type = "n", ann = FALSE, origin = TRUE)
        text(dca, display = "sites")
        points(dca, display = "species", pch = 21, col="red", bg = "red", cex=0.5)
        mtext(side = 3, text = "Análise de Correspondência Retificada (DCA)", font=2, 
              line = 2.5, cex=1.8, font.lab=6, cex.lab=2, cex.axis=1.5)
        mtext(side = 1, text = "Eixo I", font=2, line = 2.5, cex=1.5, 
              font.lab=6, cex.lab=2, cex.axis=2)
        mtext(side = 2, text = "Eixo II", font=2, line = 2.5, cex=1.5, 
              font.lab=6, cex.lab=2, cex.axis=2)

And here is the result: DCA plot

Do you see that grid line from x=0 and y=0? That's the problem.


回答1:


As is mentioned frequently in the various documentation and tutorials in existence, if you don't like the default plots produced by plot() you are free to build your own from either the low level plotting functions supplied by R or the intermediate functions we provide in vegan.

The main complication here is that, for one reason or another, we can't not plot the origin lines with type = "none" in the plot method. But even that is solvable by plotting the scores yourself.

library("vegan")
data(dune)
dca <- decorana(dune)
spp <- scores(dca, 1:2, display = "species")
sit <- scores(dca, 1:2, display = "sites")
xlim <- range(spp[,1], sit[,1])
ylim <- range(spp[,2], sit[,2])
plot(spp, type = "n", xlim = xlim, ylim = ylim, asp = 1, ann = FALSE)
text(sit[,1], sit[,2], labels = rownames(sit))
points(spp, pch = 21, col = "red", bg = "red", cex = 0.5)
title(xlab = "DCA 1", ylab = "DCA 2")

Here's a side-by-side comparison of the default plot.decorana and what the above draws




回答2:


It looks like that dotted line is created by the plot function from looking at lines 66 and 67 of the source code:

abline(h = 0, lty = 3)
abline(v = 0, lty = 3)

The only way to get rid of it is if you copy the function code and remove those two lines.




回答3:


Draw two white lines to cover them up, then draw your points on top.

abline(h=0, col="white")
abline(v=0, col="white")

Then draw a box to remove the gaps created in the border by the ablines

box()


来源:https://stackoverflow.com/questions/35186441/how-do-i-get-rid-of-grids-when-using-plot-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!