在R中的doubleYScale图中隐藏顶部x轴 [英] Hide top x-axis in doubleYScale plot in R

查看:199
本文介绍了在R中的doubleYScale图中隐藏顶部x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用doubleYScale绘图绘制两个xyplot.我想隐藏顶部的x轴,但是到目前为止,我所做的所有操作都不起作用或隐藏了所有轴.这有可能吗?

I'm plotting two xyplots with a doubleYScale plot. I would like to hide the top x-axis, but everything I tried so far either does nothing or hides all axes. Is this even possible?

library(lattice)
library(latticeExtra)
x<-seq(1:10)
y<-x^2
y2<-x*2

plot1<-xyplot(y~x, col="black", type="l", ylab="Label1", xlab="") 

plot2<-xyplot(y2~x, col="red", type="l", ylab="Label2", xlab="", scales=list(y=list(col="red")))

doubleYScale(plot1, plot2, add.axis=TRUE, add.ylab2 = TRUE, scales=list(x=list(draw=FALSE)))  

update(trellis.last.object(),
par.settings = simpleTheme(col = c("black", "red"), lty=c(1,1)), horizontal=F, scales=list(x=list(draw=T)))

推荐答案

从下面的提议解决方案中可以看到,晶格并不是真正易于设置的功能.就是说,它是完全可定制的,并且通过一些工作,您可以得到想要的东西.在这里,仅需少量内联注释,便是可以完全隐藏顶部轴的代码:

As you can see from the proposed solution below, lattice isn't really set up to easily do this particular thing. That said, it's pretty fully customizable, and with some work you can get what you're after. Here, with just a few inline comments, is code that will fully suppress that top axis:

library(lattice)
library(latticeExtra)
library(grid)

## Sample data
x <- seq(1:10)
y <- x^2
y2 <- x*2

## Prepare list of scales setting that suppresses ticks on top axis
myScales <- list(x = list(tck = c(1,0)))

## Prepare parameter settings, including setting the color used in
## plotting axis line to "transparent"
myTheme <- simpleTheme(col = c("black", "red"),
                       lty = c(1,1))
myTheme <- c(myTheme, list(axis.line = list(col = "transparent")))

## Write a custom axis function that only plots axis lines on the
## left, right, and bottom sides
myAxisFun <- function(side, line.col, ...) {
    if (side == "left") {
        grid.lines(x = c(0, 0), y = c(0, 1),
                   default.units = "npc")
    } else if (side == "right") {
        grid.lines(x = c(1, 1), y = c(0, 1),
                   default.units = "npc")
    } else if (side == "bottom") {
        grid.lines(x = c(0, 1), y = c(0, 0),
                   default.units = "npc")
    }
    axis.default(side = side, line.col = "black", ...)
}


## Construct two component plots
plot1 <- xyplot(y ~ x, col="black", type = "l",
                ylab = "Label1", xlab = "",
                par.settings = myTheme,
                scales = myScales,
                axis = myAxisFun) 
plot2 <- xyplot(y2 ~ x, col="red", type = "l",
                ylab = "Label2", xlab = "",
                par.settings = myTheme,
                scales = myScales,
                axis = myAxisFun)

## Meld the two plots 
doubleYScale(plot1, plot2, add.ylab2 = TRUE)

这篇关于在R中的doubleYScale图中隐藏顶部x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆