在 R 的 doubleYScale 图中隐藏顶部 x 轴
Hide top x-axis in doubleYScale plot in R
我正在用 doubleYScale 图绘制两个 xyplot。我想隐藏顶部的 x 轴,但到目前为止我尝试的所有操作要么什么都不做,要么隐藏所有轴。这甚至可能吗?
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)))
正如您从下面建议的解决方案中看到的那样,lattice 并没有真正设置为轻松执行此特定操作。也就是说,它是完全可定制的,通过一些工作你可以获得你想要的东西。在这里,只有一些内联注释,是完全抑制顶轴的代码:
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)
我正在用 doubleYScale 图绘制两个 xyplot。我想隐藏顶部的 x 轴,但到目前为止我尝试的所有操作要么什么都不做,要么隐藏所有轴。这甚至可能吗?
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)))
正如您从下面建议的解决方案中看到的那样,lattice 并没有真正设置为轻松执行此特定操作。也就是说,它是完全可定制的,通过一些工作你可以获得你想要的东西。在这里,只有一些内联注释,是完全抑制顶轴的代码:
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)