Carrie Perkins
Carrie Perkins

Reputation: 139

combining 2 dotplots with different y axis leads to overlap on x axis

I am trying to combine 2 dotplots using lattice and latticeExtra packages but am finding that the data groups on the x axis overlap in the combined plot. Here is a reproducible example:

First I create 2 reproducible data sets and melt them so that they are long instead of wide:

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')

And then I create a dotplot for each dataset:

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

And then I combine them, adding a new Y axis for dotreprod2:

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

enter image description here

Unfortunately there is no room on the x axis of the combined plot for "A" and so the orange points overlap with the blue ones. Is it possible to create space on the X axis so that "A","B", and "C" are next to one another and the points do not overlap?

Upvotes: 2

Views: 260

Answers (1)

Henrik
Henrik

Reputation: 67778

In both individual plots, specify the x variable as a factor with levels of the combined data, and set 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

Upvotes: 1

Related Questions