在数据后面而不是R前面绘制网格 [英] Plotting a grid behind data, not in front in R

查看:81
本文介绍了在数据后面而不是R前面绘制网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢在绘制时生成自己的网格线,以便可以控制刻度线等.我正在使用历史"绘制例程来对此进行挣扎.

I like to produce my own grid lines when plotting so I can control tick marks, etc. and I am struggling with this with the 'hist' plotting routine.

    hist(WindSpeed, breaks=c(0:31), freq=TRUE, col="blue", xaxt="n", yaxt="n", xlab="Wind Speed (m/s)",main="Foo", cex.main=1.5, cex.axis=1, cex.lab=1, tck=1, font.lab=2)
    axis(1, tck=1, ,col.ticks="light gray")
    axis(1, tck=-0.015, col.ticks="black")
    axis(2, tck=1, col.ticks="light gray", lwd.ticks="1")
    axis(2, tck=-0.015)
    minor.tick(nx=5, ny=2, tick.ratio=0.5)
    box()

图:

然后,我刚刚能够使用"lines"或"points"命令在其他类型的绘图上重新绘制数据,但是使用直方图并不是那么容易.

I have then just been able to use the 'lines' or 'points' command to replot the data over top for other types of plots, but with the histogram its not so easy.

任何帮助都会很棒.

我在下面添加了我的代码,并根据约翰的回应添加了图片...

I added my code below and image based upon John's response...

我在下面添加了我的代码,并根据约翰的回应添加了图片...

I added my code below and image based upon John's response...

hist(WindSpeed, breaks=30, freq=TRUE, col="blue", xaxt="n", yaxt="n", xlab="Wind Speed (m/s)",main="Foo", cex.main=1.5, cex.axis=1, cex.lab=1, font.lab=2)
axis(1, tck=1, col.ticks="light gray")
axis(1, tck=-0.015, col.ticks="black")
axis(2, tck=1, col.ticks="light gray", lwd.ticks="1")
axis(2, tck=-0.015)
minor.tick(nx=5, ny=2, tick.ratio=0.5)
box()
hist(WindSpeed, add=TRUE, breaks=30, freq=TRUE, col="blue", xaxt="n", yaxt="n", xlab="Wind Speed (m/s)", main="Foo", cex.main=1.5, cex.axis=1, cex.lab=1, font.lab=2)

推荐答案

这相对容易.

生成直方图,但不绘制它.

Generate the histogram but don't plot it.

h <- hist(y, plot = FALSE)

现在生成您的基础图...我添加了一些功能,使其看起来更像标准的历史图

Now generate your base plot... I've added some features to make it look more like a standard historgram

plot(h$mids, h$counts, ylim = c(0, max(h$counts)), xlim = range(h$mids)*1.1, 
    type = 'n', bty = 'n', xlab = 'y', ylab = 'Counts', main = 'Histogram of y')

添加网格

grid()

添加直方图

hist(y, add = TRUE)

或者,正如我在此过程中发现的那样……您可以更加轻松地

Or, as I discovered through this process... you can do it even easier

hist(y)
grid()
hist(y, add = TRUE, col = 'white')

最后一种方法只是在网格上重绘直方图.

This last method is just redrawing the histogram over the grid.

这篇关于在数据后面而不是R前面绘制网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆