2016-04-27 8 views
1

Ich möchte ein horizontales Diagramm mit Fehlerbalken mit einer log x-Achse erzeugen.Unterdrücken von Achsenbezeichnungen in R Hmisc errbar

Ich habe ein paar Optionen erkundet und kam am ehesten mit was ich will mit errbar aus dem Paket Hmisc. Aber ich möchte die y-Achse nicht für jeden Datenpunkt kennzeichnen. Ich habe versucht, yaxt="n" hinzuzufügen, aber das scheint es nicht zu ändern.

Irgendwelche Vorschläge, um dies zu unterdrücken? Oder alternative Wege, um einen solchen Dotplot zu erreichen? Vielen Dank im Voraus.

Beispielcode die mtcars Datensatz mit:

mtcars$model <-as.factor(rownames(mtcars)) 
mtcars$model <- reorder(mtcars$model, mtcars$mpg) 

plot(mtcars$model, mtcars$mpg, type="n",yaxt="n") 

with (
    data = mtcars, 
    expr = errbar(mtcars$model, mtcars$mpg,mtcars$mpg-mtcars$drat,mtcars$mpg+mtcars$drat,log='x', add=T, pch=16, cap=.5,xlab="MPG", yaxt="n") 
) 

(Zum ersten Mal fragte ich habe eine Frage auf Stack-Überlauf, so hoffe ich habe alle notwendigen Informationen hinzugefügt)

Dank

+0

Wenn Sie sich den Code ansehen, können Sie leicht erkennen, dass die 'plot' Funktionen mit' axes = FALSE' spezifiziert sind und dann 'axis (2, ...) 'heißt, also zeichnet die Funktion ihre eigene Achse und es wird nicht durch irgendein boolesches Flag konditioniert. Warum würden Sie nicht die "Dotplot" -Funktion im Gitter verwenden? –

+0

Hallo, ich kann nicht herausfinden, wie man die Fehlerbalken im Gitter macht sonst würde ich das tun @ 42- Jede Hilfe wäre sehr willkommen –

Antwort

3

Scheint, als ob das etwas ist, das man leicht mit Basisplotting-Primitiven konstruieren kann. Mit den Daten können

x<-mtcars$mpg 
x1<-mtcars$mpg-mtcars$drat 
x2<-mtcars$mpg+mtcars$drat 
y<-seq_along(x) 

Sie plotten

par(mar=c(5,2,3,2)+0.1) 
plot(c(x1,x2), c(y,y), type="n", axes=F, log="x", xlab="", ylab="") 
segments(x1, y, x2, y) 
points(x, y, pch=20) 
axis(1) 

Dies führt zu

simple dot plot with no y axis

0

Isolieren Sie die axis(2, ...) Funktion und davon abhängig machen, eine benannte Argument:

myerrbar <- 
function (x, y, yplus, yminus, cap = 0.015, main = NULL, sub = NULL, 
    xlab = as.character(substitute(x)), ylab = if (is.factor(x) || 
     is.character(x)) "" else as.character(substitute(y)), 
    add = FALSE, lty = 1, type = "p", ylim = NULL, lwd = 1, pch = 16, 
    errbar.col = par("fg"), Type = rep(1, length(y)), ..., yax=TRUE) 
{ 
    if (is.null(ylim)) 
     ylim <- range(y[Type == 1], yplus[Type == 1], yminus[Type == 
      1], na.rm = TRUE) 
    if (is.factor(x) || is.character(x)) { 
     x <- as.character(x) 
     n <- length(x) 
     t1 <- Type == 1 
     t2 <- Type == 2 
     n1 <- sum(t1) 
     n2 <- sum(t2) 
     omai <- par("mai") 
     mai <- omai 
     mai[2] <- max(strwidth(x, "inches")) + 0.25 
     par(mai = mai) 
     on.exit(par(mai = omai)) 
     plot(NA, NA, xlab = ylab, ylab = "", xlim = ylim, ylim = c(1, 
      n + 1), axes = FALSE, ...) 
     axis(1) 
     w <- if (any(t2)) 
      n1 + (1:n2) + 1 
     else numeric(0) 
     if (yax) {axis(2, at = c(seq.int(length.out = n1), w), labels = c(x[t1], 
      x[t2]), las = 1, adj = 1)} 
      points(y[t1], seq.int(length.out = n1), pch = pch, type = type, 
      ...) 
      segments(yplus[t1], seq.int(length.out = n1), yminus[t1], 
      seq.int(length.out = n1), lwd = lwd, lty = lty, col = errbar.col) 
      if (any(Type == 2)) { 
      abline(h = n1 + 1, lty = 2, ...) 
      offset <- mean(y[t1]) - mean(y[t2]) 
      if (min(yminus[t2]) < 0 & max(yplus[t2]) > 0) 
       lines(c(0, 0) + offset, c(n1 + 1, par("usr")[4]), 
        lty = 2, ...) 
      points(y[t2] + offset, w, pch = pch, type = type, 
       ...) 
      segments(yminus[t2] + offset, w, yplus[t2] + offset, 
       w, lwd = lwd, lty = lty, col = errbar.col) 
      at <- pretty(range(y[t2], yplus[t2], yminus[t2])) 
      axis(side = 3, at = at + offset, labels = format(round(at, 
       6))) 
     } 
     return(invisible()) 
    } 
    if (add) 
     points(x, y, pch = pch, type = type, ...) 
    else plot(x, y, ylim = ylim, xlab = xlab, ylab = ylab, pch = pch, 
     type = type, ...) 
    xcoord <- par()$usr[1:2] 
    smidge <- cap * (xcoord[2] - xcoord[1])/2 
    segments(x, yminus, x, yplus, lty = lty, lwd = lwd, col = errbar.col) 
    if (par()$xlog) { 
     xstart <- x * 10^(-smidge) 
     xend <- x * 10^(smidge) 
    } 
    else { 
     xstart <- x - smidge 
     xend <- x + smidge 
    } 
    segments(xstart, yminus, xend, yminus, lwd = lwd, lty = lty, 
     col = errbar.col) 
    segments(xstart, yplus, xend, yplus, lwd = lwd, lty = lty, 
     col = errbar.col) 
    return(invisible()) 
} 

und ignoriert den waring:

myexpr = myerrbar(mtcars$model, mtcars$mpg,mtcars$mpg-mtcars$drat,mtcars$mpg+mtcars$drat,log='x', pch=16, cap=.5,xlab="MPG", yax=FALSE) 

Keine Notwendigkeit für with, wenn Sie den Namen des Datenrahmen für jeden arument anbieten werden.