简体   繁体   English

R Lattice xyplot中的精确轴刻度和标签

[英]Exact axis ticks and labels in R Lattice xyplot

I'm creating many plots (each as a separate image) that all must have identical axis limits. 我正在创建许多图(每个图作为单独的图像),所有图必须具有相同的轴限制。 When using ylim, the tick marks are awkwardly placed at the extreme edges and tick labels are omitted for the extreme values. 当使用ylim时,刻度标记位于极端边缘处,并且对于极值而省略刻度标签。

library(lattice)
x=1:100
y=10+20*(runif(100))
xyplot(y~x)                 # Case 1 - automatic ylim
xyplot(y~x, ylim=c(10,20))  # Case 2 - specified ylim

In Case 1, the axis ticks and labels are automatically generated at (y=10,15,20,25,30). 在案例1中,轴刻度和标签在(y = 10,15,20,25,30)处自动生成。 All tick marks are labeled, and there is some vertical padding for the extreme tick marks (y=10 and y=30) in the plot rectangle. 标记了所有刻度线,并且绘图矩形中的极端刻度线(y = 10和y = 30)有一些垂直填充。

In Case 2, when I specify ylim values, the tick marks are generated at (y=10,12,14,16,18,20) but labels appear only for (y=12,14,16,18). 在案例2中,当我指定ylim值时,刻度标记在(y = 10,12,14,16,18,20)处生成,但标签仅出现(y = 12,14,16,18)。 Tick labels are missing at the extremes. 勾选标签在极端情况下缺失。 Also, there is no vertical padding for the extreme tick marks in the plot rectangle. 此外,绘图矩形中的极端刻度标记没有垂直填充。

Is there a way to specify ylim and still have the tick marks and labels come out similarly to Case 1? 有没有办法指定ylim,仍然有刻度线和标签出现类似于案例1?

More generally, when specifying ylim: 更一般地说,在指定ylim时:
1. how can you specify exactly where each tick mark goes? 1.你怎么能准确指出每个刻度标记的位置?
2. how can you specify exactly which tick marks get labeled? 2.如何准确指定哪些刻度标记?

To get your tick marks and labels where you want, you can do something along the lines of: 要获得所需的刻度线和标签,您可以执行以下操作:

xyplot(
  y~x,
  ylim=c(10,20),
  scales=list(
    y=list(
      at=seq(10,20,2),
      labels=c("a","","b","","c","") )
    )
)  

The padding issue, I'm not sure about how to address except for manually adjusting the ylim= arguments. 填充问题,除了手动调整ylim=参数之外,我不确定如何解决。

Padding is controlled in lattice.options with axis.padding. 使用axis.padding在lattice.options中控制填充。 See ?lattice.options 请参阅?lattice.options

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

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