I’m trying to do a dotplot with the libraries lattice and latticeExtra in R. However, no proper representation of the values on the vertical y-axis is done. Instead of choosing the actual values of the numeric variable, R plots the rank of the value. That is, there are values [375, 500, 625, 750, ..., 3000] and R plots their ranks [1,2,3,4,...23] and chooses the scale accordingly. Has someone experienced a problem like this? How can I manage the get a proper representation with ticks like (0, 500, 1000, 1500, ...) on the vertical y-scale?
Here the program code so far:
df.dose <- read.table("data.csv", sep=",", header=TRUE)
library(lattice); library(latticeExtra)
useOuterStrips(dotplot(z ~ sample.size | as.factor(effect.size)*as.factor(true.dose),
groups=as.factor(type), data=df.dose, as.table=TRUE))
(Added from comment below): Also, can error bars be added to the graph? I thought of the following (to be added to the call), but it doesn’t seem to work. Is it possible somehow?
up=z+se, lo=z-se, panel.groups=function(x,y,..., up, lo, subscripts){
up <- up[subscripts]
lo <- lo[subscripts]
panel.segments(lo, as.numeric(y), up, as.numeric(y), ...)
}
Here’s my data: https://www.dropbox.com/s/egy25cj00rhum40/data.csv
Added: here’s the relevant portion of the data using expand.grid and dput:
df.dose <- expand.grid(effect.size=c(-.5, -.625, -0.75),
sample.size=c(40L, 60L, 80L),
true.dose=c(375L, 500L, 750L, 1125L),
type=c("dose", "categ", "FP2", "FP1"))
df.dose$z <- c(875L, 875L, 750L, 750L, 750L, 625L, 625L, 625L, 625L, 875L,
875L, 750L, 1000L, 1000L, 1000L, 1125L, 1000L, 875L, 1000L, 1000L,
875L, 1000L, 1000L, 875L, 1125L, 1000L, 1000L, 1250L, 1125L,
1000L, 1250L, 1250L, 1125L, 1250L, 1000L, 1000L, 500L, 500L,
500L, 500L, 500L, 500L, 500L, 500L, 500L, 625L, 625L, 625L, 625L,
625L, 625L, 625L, 625L, 625L, 750L, 750L, 625L, 750L, 750L, 750L,
750L, 750L, 750L, 875L, 875L, 750L, 750L, 875L, 875L, 875L, 875L,
875L, 2500L, 1500L, 1125L, 2000L, 1000L, 1750L, 250L, 500L, 500L,
1250L, 750L, 625L, 875L, 500L, 500L, 875L, 500L, 375L, 1250L,
875L, 750L, 1000L, 625L, 625L, 875L, 500L, 500L, 1125L, 1000L,
875L, 1125L, 875L, 625L, 1125L, 1000L, 625L, 2500L, 2125L, 2375L,
2000L, 750L, 2625L, 250L, 625L, 250L, 875L, 875L, 500L, 625L,
500L, 625L, 1000L, 500L, 375L, 1000L, 875L, 625L, 875L, 500L,
500L, 875L, 500L, 500L, 1250L, 1125L, 875L, 1125L, 875L, 750L,
1250L, 1000L, 625L)
You need to make
za factor:dotplot(factor(z) ~ ...Also you probably want some jitter in the plot to prevent overlap; try adding
jitter.x=TRUEorjitter.y=TRUE, or both.Judging by your comment below and looking at the data again, I think you’re plotting the dotplot the wrong way. I think you want the lines to be for the sample sizes, not for the
z‘s. If you really wantzon the vertical axis, you then need to addhorizontal=TRUE. You could also swap what is on the horizontal and vertical axes.To add an error bar, it’s a little more complicated because you have groups within the panels, so you need to use a
panel.groupsfunction; additionally, so that the lines don’t overlap, you probably want to jitter them from side to side a little, which is best done in a custompanelfunction.