简体   繁体   English

R晶格X轴刻度线间距

[英]R lattice x-axis tick marks spacing

Plotting some values against date like 根据日期绘制一些值,例如

01/31/1996      -7.3667
02/29/1996       0.9413
03/31/1996       9.3252
04/30/1996       0.3147
05/31/1996       8.8593
06/30/1996      10.0297
07/31/1996       0.64i3
08/31/1996       2.1397
09/30/1996       0.6759
10/31/1996      -1.5579
11/30/1996       2.9493
....             ....
goes on till 2012 Nov

using barchart in R lattice, I got a reasonable looking graph but the labels on the x axis are all squished together and just looks like a huge glob of text. 使用R格中的条形图,我得到了一个看起来合理的图形,但是x轴上的标签都被挤压在一起,看起来就像是一大堆文本。 I think it's outputting every date from above in literal form and trying to fit it all into the space but what I'd really like is just 4 or 5 tickmarks increments of 4 years like 我认为它以文字形式从上方输出每个日期,并试图将其全部放入空间,但是我真正想要的只是4年的4或5个刻度

1996            20000            2004            2008            2012 

evenly spaced out 均匀间隔

I'm not sure how these dates are being interpreted so using just tick.marks=5 or scales=list(x=list(at=seq(1996,2012,4) ) is not working. I also tried the following but the tick marks are uneven (sometimes in 4 years, sometimes 5) and do not really correspond to the exact positions of the actual data. 我不确定如何解释这些日期,因此仅使用tick.marks = 5或scales = list(x = list(at = seq(1996,2012,4))不能正常工作。我也尝试了以下方法,但刻度线是不均匀的(有时是4年,有时是5年),并不真正对应于实际数据的确切位置。

> df <- data.frame(x=paste0(rep(1996:2012, each=12), paste0("Q", 1:12)), y=1:204)
> x.tick.number <- 10
> x.tick.number <- 8
> at <- seq(1, nrow(df), length.out=x.tick.number)
> labels <- round(seq(1996, 2012, length.out=x.tick.number))
> barchart(V3~V1,  data = differences,col=c("green3"),origin=0,box.ratio=40, lty=0,reference=TRUE,stack=TRUE,main ="testing",ylab = list("values", cex=1.3), xlab = list
("dates",cex=1.3),par.settings=list (axis.text=list(cex=1.5), axis.line = list(lwd=3)),scales=list(y=list(tick.number=8, rot=90 ), x=list(at=at, labels=labels)))

Any other suggestions? 还有其他建议吗? Also I have date ranges from -25 to 20 on the y axis with tick marks at increments of 5 but I'd like 5 smaller tickmarks within each. 另外,我在y轴上的日期范围是-25至20,刻度线的增量为5,但我希望每个刻度线内有5个较小的刻度线。 How do I control this. 我该如何控制。 Thanks. 谢谢。

I assume that you are plotting each month as a different bar and that every month is in hte data. 我假设您将每个月都绘制为不同的条形,并且每个月都在此数据中。 If so, I think you can do this: 如果是这样,我认为您可以这样做:

My_at<-c(6,54,102,150,198)

This puts the tick in June of 1996, 2000, 2004, 2008 and 2012 这使1996年6月,2000年,2004年,2008年和2012年6月

then in the barchart function: 然后在barchart函数中:

scales=list(x=list(at=at,labels=c("1996","2000","2004","2008","2012")))

Without a reproducible example I can't test this - but this is the basic idea. 没有可复制的示例,我将无法测试-但这是基本思想。

If you don't have data for every month you will need to change the values in at accordingly. 如果您没有每个月的数据,则需要相应地更改at的值。

Edit in response to comments 编辑以回应评论

The numbers in the labels arguement (eg "2000", "2004" etc) are just the text to use for the tick labels, and can be changed as desired. labels争论中的数字(例如“ 2000”,“ 2004”等)只是用于刻度标签的文本,可以根据需要进行更改。 The values placed in 'My_at` are just the bar number - so the 6 means put the tick label below the 6th bar, and the 54 is the 54th bar and so on. 放置在“ My_at”中的值只是小节编号-所以6表示将刻度标签放在第6小节的下方,而54是第54小节,依此类推。 You can put a label beyond where you have data (such as the 500th bar) if you wanted. 如果需要,您可以在数据之外放置标签(例如第500条)。 To move the last tick to the left, just decrease the last number. 要将最后一个刻度移动到左侧,只需减小最后一个数字即可。 I you make it 192 instead of 198 that will correspond to January 2012. 我将其改为192,而不是198(对应于2012年1月)。

Finally to draw the ticks use: 最后绘制刻度线使用:

scales=list(x=list(at=at,labels=c("1996","2000","2004","2008","2012"), draw=TRUE,
              tck=2 ))

The draw=TRUE indicates that labels and tick marks should be drawn, and tck=2 controls the length of the tick marks. draw=TRUE表示应绘制标签和刻度线, tck=2控制刻度线的长度。 You can try different values to find one you like. 您可以尝试不同的值来找到自己喜欢的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM