不确定我是否正确理解了这个问题,但也许绘制“a”和“b”的交互会起作用?
a <- c(1,1,1,1,1,1,1, 2,2,2,2,2,2,2, 3,3,3,3,3,3,3)
b <- c(1,4,5,7,8,10,13, 5,7,10,11,14,17,23, 3,7,11,16,19,26,29)
d <- c(.4,.15,.76,.07,.18,.11,.12, .23,.45,.25,.11,.16,.2,.5, .48,.9,.13,.75,.4,.98,.3)
df <- data.frame("a" = a, "b" = interaction(b, a), "d" = d)
df$b <- factor(df$b, levels = df$b)
png("test.png", width = 900, height = 400, units = "px")
plot(df$b, df$d)
dev.off()
编辑
或者也许删除 x 轴标签并“手动”添加它们?
a <- c(1,1,1,1,1,1,1, 2,2,2,2,2,2,2, 3,3,3,3,3,3,3)
b <- c(1,4,5,7,8,10,13, 5,7,10,11,14,17,23, 3,7,11,16,19,26,29)
d <- c(.4,.15,.76,.07,.18,.11,.12, .23,.45,.25,.11,.16,.2,.5, .48,.9,.13,.75,.4,.98,.3)
df <- data.frame("a" = a, "b" = b, "d" = d)
png("test.png", width = 900, height = 400, units = "px")
plot(seq_along(df$b), df$d, xaxt = "none")
axis(1, at = seq_along(df$b), labels = df$b)
dev.off()
编辑 2
另一种可能更好地“保留 x 轴间距”的方法是拆分数据框并分别绘制每个组:
a <- c(1,1,1,1,1,1,1, 2,2,2,2,2,2,2, 3,3,3,3,3,3,3)
b <- c(1,4,5,7,8,10,13, 5,7,10,11,14,17,23, 3,7,11,16,19,26,29)
d <- c(.4,.15,.76,.07,.18,.11,.12, .23,.45,.25,.11,.16,.2,.5, .48,.9,.13,.75,.4,.98,.3)
df <- data.frame("a" = a, "b" = b, "d" = d)
split_df <- split(df, df$a)
par(mfrow = c(1, 3), mar = c(2, 2, 1, 1))
plot(split_df$`1`$b, split_df$`1`$d, ylim = c(0, 1))
plot(split_df$`2`$b, split_df$`2`$d, ylim = c(0, 1))
plot(split_df$`3`$b, split_df$`3`$d, ylim = c(0, 1))
由reprex package (v2.0.1) 于 2021 年 10 月 15 日创建