R Plot multiple series with par(new=T) - axis labels are overlaying each other, making the plot unreadable

杀马特。学长 韩版系。学妹 提交于 2019-12-02 14:23:01

问题


I'm plotting multiple data series.

colos=c('red','green','purple','pink','brown')
par(new=F)
for (i in 1:5)
{
  plot(dat[[i+1]],col=colos[i],cex=marksize,xlab='Reading #',ylab = 'Current')
  par(new=T)
}

My plot looks like this:

Is there a way I can overwrite the plot axis with each iteration, but not overwrite the plotted points?


回答1:


You may want to use the lines or points function(s) instead. Here's an example of how I usually go about this problem. This way you only overlay points on top of the existing plot, instead of plotting one plot on top of another.

Plot the first one with your original plot call, then use lapply to overlay the other columns' points on top of that.

set.seed(1)
dat <- data.frame(replicate(5, sample(10)))
colos <- c('red','green','purple','pink','brown')
plot(dat[[1]], col = colos[1], xlab = 'Reading #',   
     ylab = 'Current', ylim = range(as.matrix(dat)))
invisible(lapply(2:ncol(dat), function(i) points(dat[[i]], col = colos[i])))




回答2:


Turn off the axes using xaxt and yaxt

E.g.:

plot(1:10)
par(new=TRUE)
plot(1:10, rnorm(10), xaxt="n", yaxt="n", xlab="", ylab="", type="l")
axis(side=4)



来源:https://stackoverflow.com/questions/26567196/r-plot-multiple-series-with-parnew-t-axis-labels-are-overlaying-each-other

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