如何更改分组格子 xyplot 中的 x-axis 值

How do I change x-axis values in a grouped lattice xyplot

我在 R 中有以下示例数据。

d <- read.table(text = 
"Grp     Var    Col1  Col2    Col3
grp_1   8      46.8  50.0   50.6
grp_1   16     95.6  47.4   48.0
grp_1   24     45.1  45.6   46.4
grp_1   32     68.8  44.3   58.2
grp_1   40     44.6  52.2   44.3
grp_1   48     86.5  42.2   68.6
grp_2   40     63.2  95.6   63.0
grp_2   60     66.7  67.5   65.6
grp_2   80     69.6  70.7   67.9
grp_2   100    71.9  73.4   69.3
grp_2   120    73.8  75.7   48.0
grp_3   500    51.9  50.0   50.5
grp_3   1000   65.5  53.0   53.4
grp_3   5000   61.2  99.0   59.9
grp_3   10000  80.1  63.0   62.8
grp_3   30000  25.9  33.8   14.2
  ", header=T
)

每组的 Col1Col2Col3 列需要根据 Var. 进行绘制 第 1 组的 Var、Col1 各有 6 个值- Col3,第 2 组和第 3 组有 5 个值。我正在使用 Lattice 的 xyplot() 函数将它们绘制在网格中。代码如下:

#Groups as factors
d$Grp <- factor(d$Grp, levels=c("grp_1","grp_2","grp_3"), order=TRUE)
#Plot the data
xyplot(
  Col1 + Col2 + Col3 ~ Var | Grp,
  data=d,
  superpose=T,
  as.table=TRUE,
  col=c("#cc0000","#008000", "#0073e6"),
  lwd=1.5, ylim = c(0,120),
  ylab = list(label=expression("My Values"), fontsize=15),
  xlab = list(label="Var",fontsize=15),
  key=list(
    text  = list(c("Col1", "Col2", "Col3")),
    lines = list( lwd=1.5, col=c("#cc0000","#008000", "#0073e6")),
    corner = c(1, 0),
    x = 0.90, 
    y = 0.17),
  par.settings = list(strip.background=list(col="white")),
  scales=list(cex=1.2),
  type=c("l","g")
)

剧情是这样的:

显然,第 1 组和第 2 组绘制的值看起来很乱,因为 x-axis 值是 500,1000,5000,10000,30000 而不是 8,16,24,32,40,4840,60,80,100,120。有没有什么方法可以使用 xyplot()(最好)来修复第 1 组和第 2 组的 x-axis 值?我也愿意接受其他很棒的建议。例如,预期的输出可能是这样的:

因为你也标记了ggplot2,这里是ggplot2解决方案。

我个人认为这比基本情节容易得多,但我的偏见令人难以置信。

library(tidyverse)

d_long <- 
  d %>%
    pivot_longer(names_to = "key", values_to = 'value', contains("Col"))

ggplot(d_long, aes(Var, value, color = key)) +
  geom_line() +
  facet_grid(~Grp, scales = "free_x") +
  scale_color_manual(values =c("#cc0000","#008000", "#0073e6"))

reprex package (v0.3.0)

于 2020-04-28 创建

您将 scales 选项与 relation="free" 一起使用,我得到的图与上面的 @Tjebo 类似。不是 3 个相同的图:

xyplot(
  Col1 + Col2 + Col3 ~ Var | Grp,
  data=d,
  superpose=T,
  as.table=TRUE,
  col=c("#cc0000","#008000", "#0073e6"),
  lwd=1.5, ylim = c(0,120),
  ylab = list(label=expression("My Values"), fontsize=15),
  xlab = list(label="Var",fontsize=15),
  key=list(
    text  = list(c("Col1", "Col2", "Col3")),
    lines = list( lwd=1.5, col=c("#cc0000","#008000", "#0073e6")),
    corner = c(1, 0),
    x = 0.90, 
    y = 0.17),
  par.settings = list(strip.background=list(col="white")),
  scales=list(cex=1.2,x=list(relation="free")),
  type=c("l","g")
)

plot.trellis 方法允许在网格上绘制 lattice 对象。通过将其与 update.trellis 方法相结合,可以创建任意间距的布局。

我 post 这个答案(有点晚)在 lattice 中突出显示这些选项。我的回答也本着“tidyverse”的精神,为数据创建了一个长格式。

# first update the lattice parameters in 'tp'
    library(lattice)
    tp <- trellis.par.get() # retrieve all parameters
    tp$strip.background$col <- "white"
    tp$superpose.line$col <- c("#CC0000","#008000", "#0073E6")
    tp$superpose.line$lwd <- 1.5

# data from example already 'd' then convert with simple base functions
    myDat <- cbind(d[1:2], stack(d[3:5])) # new variables: "values" & "ind"

# create single plot object
    obj <- xyplot(values ~ Var | Grp, df, groups = ind, type = c("g", "l"),
        par.settings = tp, scales = list(x = list(relation = "free")),
        ylab = "My Values", xlab = "Place holder", as.table = TRUE)

点阵对象 (obj) 具有所需的格式,但只有一个 x-axis 标签。它可以简单地通过 objplot(obj)almost 中绘制成所需的形式。但是,由于它实际上是三个格子图,因此可以使用 update.trellisplot.trellis 方法在网格中提取、更新和绘制它们,如此处所示。

# axis labels and key 
    xlabs <- paste("My x-axis title", 1:3)
    myKey = list(corner = c(5, 0.5), points = FALSE, lines = TRUE)

# assemble final plot
    plot(update(o[1], xlab = xlabs[1]), split = c(1,1,2,2), more = TRUE)
    plot(update(o[2], xlab = xlabs[2]), split = c(2,1,2,2), more = TRUE)
    plot(update(o[3], xlab = xlabs[1], auto.key = myKey),
        split = c(1,2,2,2), more = FALSE)

通过使用 position 选项而不是 split 选项,可以更好地控制每个绘图的位置。