将具有不同y轴的2个点图组合会导致x轴重叠

时间:2018-10-07 19:11:53

标签: r plot lattice

我正在尝试使用latticelatticeExtra软件包来组合两个点图,但是发现x轴上的数据组在组合图中重叠。这是一个可重现的示例:

首先,我创建2个可再现的数据集并将其融化,以使它们变长而不是变宽:

require(lattice)

df1 <- data.frame(Treatment = rep(c("B", "C"), each = 6),
                  LocB = sample(1:100, 12), 
                  LocC = sample(1:100, 12))

dftwo <- data.frame(Treatment = rep(c("A"), each = 6),
                    LocA = sample(1:100, 6))

dat.reprod1 <- melt(df1, id.vars = 'Treatment')

dat.reprod2 <- melt(dftwo, id.vars = 'Treatment')

然后为每个数据集创建一个点图:

dotreprod1 <- dotplot(value ~ Treatment, data = dat.reprod1,
                      par.strip.text = list(cex = 3),
                      cex = 2)

enter image description here

dotreprod2 <- dotplot(value ~ Treatment, data = dat.reprod2,
                      par.strip.text = list(cex = 3), col = "orange",
                      cex = 2)

enter image description here

然后我将它们组合起来,为dotreprod2添加一个新的Y轴:

require(latticeExtra)
doubleYScale(dotreprod1, dotreprod2, add.ylab2 = TRUE, use.style = F)

enter image description here

不幸的是,组合图的x轴上没有用于“ A”的空间,因此橙色点与蓝色点重叠。是否可以在X轴上创建空间,以使“ A”,“ B”和“ C”彼此相邻并且点不重叠?

1 个答案:

答案 0 :(得分:1)

在两个单独的图中,将x变量指定为factor与合并数据的levels,然后设置drop.unused.levels = FALSE

dotreprod1 <- dotplot(value ~ factor(Treatment, levels = LETTERS[1:3]),
                      data = dat.reprod1,
                      drop.unused.levels = FALSE)

dotreprod2 <- dotplot(value ~ factor(Treatment, levels = LETTERS[1:3]),
                      data = dat.reprod2,
                      col = "orange",
                      drop.unused.levels = FALSE)

doubleYScale(dotreprod1, dotreprod2, add.ylab2 = TRUE, use.style = FALSE)

enter image description here