简体   繁体   中英

How to add strips labels to y axis using xyplot

I am a beginner of xyplot in lattice package, and I am trying to visualize my data frame. I would like to present values of some continuous variable on x-axis and a couple of factor variables on y axis (In other word, nested variable on y-axis)

Q: How can I add custom strips labels to y-axis?

I read the manual document and googled this question while no solution was found. What I expect to obtain is something like this

在此处输入图片说明

Is that possible to get this kind of plot using xyplot?

Update

Indeed, ggplot2 might be a good option. However, xyplot seems to be more convenient, through formula as y~x1|x2, while I do not know how to make that plot.

Here is a reproducible example

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 ...

measure: circles in the plot and values should be placed on x axis.

method: strip labels on the top

Scenario: strip labels on the left

scrooter: the value placed on y axis

Update 2

Here is a solution using ggplot but I am looking for the one using 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)

在此处输入图片说明

The key function is useOuterStrips() available in the latticeExtra package. It works on a trellis object with exactly two conditioning variables, as you have here. You can wrap it around a function call as shown in the first example or, as is more typical, apply the function to the saved the trellis object as shown in the second example.

# 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示例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM