Dotplot in R with bars: display of the vertical axis and error bars

I am trying to make dotplot with the lattice and latticeExtra in R However, the correct representation of the values ​​on the vertical y axis is not performed. Instead of selecting the actual values ​​of a numeric variable, R displays the rank of the value. That is, there are values [375, 500, 625, 750, ..., 3000] , and R displays their ranks [1,2,3,4,...23] and accordingly selects the scale. Does anyone have such a problem? How can I get the correct representation with ticks like (0, 500, 1000, 1500, ...) on the vertical y scale?

Here is the program code:

 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 the comment below): In addition, can error bars appear on the chart? I thought of the following (to add to the call), but it does not 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 is my details: https://www.dropbox.com/s/egy25cj00rhum40/data.csv

Added: here is the corresponding piece of 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) 
+4
source share
3 answers

You need to make the z factor: dotplot(factor(z) ~ ...

Also, you probably want some jitter in the plot to prevent overlapping; try adding jitter.x=TRUE or jitter.y=TRUE , or both.

Judging by your comment below and looking at the data again, I think you are drawing the point incorrectly. I think you want the rows to be for sample sizes, not for z . If you really want z along the vertical axis, you need to add horizontal=TRUE . You can also swap what is on the horizontal and vertical axis.

 useOuterStrips(dotplot(z ~ factor(sample.size) | as.factor(effect.size)*as.factor(true.dose), groups=as.factor(type), data=df.dose, as.table=TRUE, horizontal=FALSE, jitter.x=TRUE)) 

To add an error panel is a bit more complicated because you have groups inside the panels, so you need to use the panel.groups function; in addition, so that the lines do not overlap, you probably want to shake them a little from side to side, which is best done in the user-defined panel function.

 df.dose$se <- 200 df.dose$type <- factor(df.dose$type) df.dose$sample.size <- factor(df.dose$sample.size) panel.groups.mydotplot <- function(x, y, subscripts, up, lo, col=NA, col.line=NA, ...) { panel.points(x, y, ...) panel.segments(x, lo[subscripts], x, up[subscripts], col=col.line, ...) } panel.mydotplot <- function(x, y, subscripts, groups, ..., jitter=0.1) { jitter <- seq(-1,1,len=nlevels(groups))*jitter xx <- as.numeric(x) + jitter[as.numeric(groups[subscripts])] panel.dotplot(x, y, groups=groups, subscripts=subscripts, pch=NA, ...) panel.superpose(xx, y, groups=groups, subscripts=subscripts, panel.groups=panel.groups.mydotplot, ...) } pp <- dotplot(z ~ sample.size | as.factor(effect.size)*as.factor(true.dose), groups=type, data=df.dose, as.table=TRUE, horizontal=FALSE, up=df.dose$z + df.dose$se, lo=df.dose$z - df.dose$se, panel=panel.mydotplot, auto.key=list(space="right")) useOuterStrips(pp) 

enter image description here

+6
source

I'm not sure if I understood this problem and you asked for a solution with a grid, but I thought it might be useful to see how this is done with ggplot2:

 ggplot(data=df.dose, aes(x=sample.size, y=as.factor(z), colour=type)) + geom_point() + facet_grid(true.dose~effect.size) 

Productivity: enter image description here

Or we can free the scales with:

 ggplot(data=df.dose, aes(x=sample.size, y=as.factor(z), colour=type)) + geom_point() + facet_grid(true.dose~effect.size, scales="free") 

Yielding:

enter image description here

+3
source

You can also use xYplot from the xYplot package to achieve a solution similar to @Aaron, although it can be a bit difficult to get the same jitter as it is:

 a <- xYplot(Cbind(z, z-se, z+se) ~ sample.size | as.factor(effect.size) * as.factor(true.dose), groups=as.factor(type), data=df.dose, as.table=TRUE, auto.key=list(space="top")) useOuterStrips(a) 

enter image description here

But is there really an informative plot? It shows your data well, emphasizes your comparisons? Does he study any trends in the data? To better see all the factors that you want to build, I would first draw lines of communication between your groups to better see individual effects in different sample.size .

 key.variety <- list(space = "top", text = list(levels(df.dose$type)), points = list(pch = 0:3, col = "black")) a <- xyplot(z ~ as.factor(sample.size) | as.factor(effect.size)*as.factor(true.dose), df.dose, type = "o", as.table=TRUE, groups = type, key = key.variety, lty = 1, pch = 0:3, col.line = "darkgrey", col.symbol = "black") useOuterStrips(a) 

enter image description here

But something is hiding there, and because of the density of the data, there is still too much noise. Let me get rid of the effect.size line and the plot regression line, although it is probably a sin that there is so little data.

 a <- xyplot(z ~ as.factor(sample.size) | as.factor(type)*as.factor(true.dose), data=df.dose, as.table=TRUE, panel = function(x, y){ panel.xyplot(x, y, jitter.x = T, col=1); panel.lmline(x, y, col=1, lwd=1.5); }) useOuterStrips(a) 

enter image description here

I know that you may not have been convinced, but sometimes it’s better to unload the plot from too many factors to better look at the data. Sometimes it may be more accessible visually if you show shared factors.

+3
source

Source: https://habr.com/ru/post/1439171/


All Articles