如何使用 xyplot 将条带标签添加到 y 轴

How to add strips labels to y axis using xyplot

我是 lattice 包中 xyplot 的初学者,我正在尝试可视化我的数据框。我想在 x 轴上显示一些连续变量的值,在 y 轴上显示几个因子变量(换句话说,y 轴上的嵌套变量)

问:如何将自定义条带标签添加到 y 轴?

我阅读了手册文档并用谷歌搜索了这个问题,但没有找到解决方案。我希望得到的是这样的

是否可以使用 xyplot 得到这种图?

更新

确实,ggplot2 可能是一个不错的选择。不过xyplot好像更方便,公式为y~x1|x2,虽然我不知道怎么画。

这是一个可重现的例子

method<-rep(c("XGBM", "LGBM", "WGBM", "NNGBM"), times = 20)
Scenario<-as.factor(sort(rep(1:5, times = 16)))
scrooter<-as.factor(rep(c(50, 5, 0.5, 0.05), times = 5, each = 4))
measure<-rnorm(80)
data<-data.frame(measure, method, Scenario, scrooter)


str(data)

'data.frame':   80 obs. of  4 variables:
 $ measure : num  -1.181 -2.595 1.114 0.178 -1.246 ...
 $ method  : Factor w/ 4 levels "LGBM","NNGBM",..: 4 1 3 2 4 1 3 2 4 1 ...
 $ Scenario: Factor w/ 5 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ scrooter: Factor w/ 4 levels "0.05","0.5","5",..: 4 4 4 4 3 3 3 3 2 2 ...

测量:图中的圆圈和值应放在 x 轴上。

方法:去掉顶部的标签

场景:剥离左侧的标签

scrooter: y轴上的值

更新 2

这是一个使用 ggplot 的解决方案,但我正在寻找使用 xyplot 的解决方案

cust_theme <- theme_bw() + 
  theme(legend.position="none", 
        panel.spacing.x = unit(0, "lines"), 
        panel.spacing.y = unit(0, "lines"),
        panel.grid = element_blank(),
        panel.border = element_rect(size = 0.25, color = "black"),
        strip.background.x = element_rect(fill = "wheat1"),
        strip.background.y = element_rect(fill = "lightgreen"),
        strip.text.x = element_text(margin = margin(1,0,1,0, "mm")),
        strip.text.y = element_text(margin = margin(0,1,0,1, "mm")),
        axis.title=element_text(size=14,face="plain"),
        axis.text.x = element_text(angle = 45, hjust = 1)
  )
point.plot<-ggplot(data = data, aes(x = measure, y = scrooter)) +
  geom_point(size = 3) +
  facet_grid(Scenario ~ method, switch = "y") +
  cust_theme +
  geom_vline(xintercept = 0, linetype="dotted", color = "black", size=0.5) +
  ylab("Scrooter") +
  geom_point(color = "white", size = 1.5)

关键函数是 useOuterStrips()latticeExtra 包中可用。它适用于具有恰好 两个 条件变量的网格对象,正如您在此处所具有的那样。您可以像第一个示例中所示将其包装在函数调用周围,或者更典型地,将函数应用于已保存的网格对象,如第二个示例中所示。

# Starting with data as specified above
# Need latticeExtra, use two steps to build plot
  require(latticeExtra)

# Wrapped around a call to xyplot with two conditioning factors
  useOuterStrips(xyplot(scrooter ~ measure | method + Scenario, data,
    panel = function(...) {panel.refline(v = 0); panel.xyplot(...)}))

# More typically, and to more closely reproduce the example here
  obj <- xyplot(scrooter ~ measure | method + Scenario, data,
    panel = function(...) {panel.refline(v = 0); panel.xyplot(...)},
    scales = list(alternating = FALSE, tck = c(0.5, 0), col = "gray40",
      x = list(rot = 45)),
    as.table = TRUE, ylab = "Scrooter", col = 1)

# Apply useOuterStrips function
  useOuterStrips(obj)