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

时间:2019-04-02 03:31:52

标签: r ggplot2 lattice

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

问:如何在y轴上添加自定义标签标签?

我阅读了手册文档,并在没有找到解决方案的情况下用谷歌搜索了此问题。我期望获得的是这样的东西

enter image description here

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

更新

实际上,ggplot2可能是一个不错的选择。但是,通过y〜x1 | x2这样的公式,xyplot似乎更方便,而我不知道如何绘制该图。

这是一个可重复的例子

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)

enter image description here

1 个答案:

答案 0 :(得分:0)

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)

useOuterStrips example