使某些线在点阵图中不可见

时间:2019-04-23 16:59:49

标签: r plot lattice

我有一个用以下代码生成的图形:

require(lattice)
mydata <- data.frame(y = c(0.1, 0.4, 0.3, 0.23, 0.17, 0.27), x = c(1,2,3,4,5,6))
ticks_at <- seq(-0.5, 0, 0.1)
barchart(-y ~ x, 
         mydata, 
         horizontal = FALSE, 
         origin=0, 
         scales = list(y=list(at = ticks_at, 
                              labels = -1 * (ticks_at)),
                       alternating = 2,
                       tck = c(0,1)),
         xlab = "x",
         ylab ="y")

我想做两件事:a)我希望x轴上有刻度(如您所见,现在只有y轴上有刻度),b)我想删除左轴和上轴线,以便图形仅具有下轴和右轴。预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

通过在代码的scales = list(...)部分添加以下内容,可以在底部x轴而不是顶部x轴添加刻度线。

x = list(tck = c(1, 0))

消除使用lattice并不是那么简单。您可以创建一个自定义轴函数来替换axis.default()提供的左,右,顶部和底部组件。 Hide top x-axis in doubleYScale plot in R中对此进行了描述。我是格子的忠实拥护者,但倾向于将基本图形用于所需的内容:

# Starting with values in mydata and ticks_at
  opar <- par(mar = c(5,2,2,5) + 0.1)
  mp <- barplot(-mydata$y, axes = FALSE, col = "cyan", xlab = "x")
  axis(1, at = mp, line = 1, labels = mydata$x) # or explicit labels if needed
  axis(4, las = 1, at = ticks_at, labels = -ticks_at)
  title(ylab = "y", line = 0) # mtext could be used to place "y" on the right
  par(opar)