R图中的轴格式和网格线

时间:2013-04-13 21:02:42

标签: r plot data-visualization linegraph

我有一个类似下面的数据集:

data<-data.frame(x=c(50,100,250,400),y1=c(0.74,0.75,0.82,0.79),y2=c(0.81,0.83,0.87,0.88)) 

我生成这样的情节:

plot(data$x,data$y1,type='l',col="red",xaxs='i',yaxs='i',ylim=c(0.4,1),xlim=c(50,500))
lines(data$x, data$y2, type='l',col="blue")

生成的数字是:

enter image description here

如何在此图上进行以下修改?

  • 1)当前X轴以50开头。但它没有明确标记50。

  • 2)当前X轴标记为100, 200, 300, 400, 500,我可以将其标记为50,100, 250,500吗?

  • 3)当前Y轴标记为0.5, 0.6,0.7.0.8,0.9,1.0。我可以将其标记为0.5, 0.55, 0.6, 0.65, 0.7,0.75, 0.8, 0.85, 0.9, 0.95, 1吗?

  • 4)我想添加一些与X轴平行的网格线。这些线应从沿Y轴的0.5, 0.55, 0.6, 0.65, 0.7,0.75, 0.8, 0.85, 0.9, 0.95, 1点开始。

1 个答案:

答案 0 :(得分:4)

data<-data.frame(x=c(50,100,250,400),y1=c(0.74,0.75,0.82,0.79),y2=c(0.81,0.83,0.87,0.88))

windows()
plot(data$x,data$y1,type="l",col="red", ylim=c(0.5,1),xlim= c(50,500),col.axis = "white")
axis(1, xaxp=c(50,500,9))
axis(2, yaxp=c(0.5,1,10)) 
lines(data$x, data$y2, type='l',col="blue")

for(i in c(0.55,0.6,0.65,0.7,0.75,0.8,0.85,0.9,0.95)) {
lines(c(50,500),c(i,i),type="l",lty=2,lwd=0.5, col="black")
rm(i)
}

enter image description here