【问题标题】:Make certain lines invisible in lattice plot使格子图中的某些线不可见
【发布时间】:2019-09-12 22:15:33
【问题描述】:

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

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)我想删除左和上轴线,以便图形只有底轴和右轴。提前感谢您的帮助。

【问题讨论】:

    标签: r plot lattice


    【解决方案1】:

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

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

    消除使用lattice 并不是那么简单。您可以创建自定义轴功能来替换axis.default() 提供的左、右、上、下组件。这已在Hide top x-axis in doubleYScale plot in R 中进行了描述。我是 lattice 的忠实粉丝,但我会倾向于使用基本图形来满足您的需求:

    # 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多