2016-06-06 4 views
0
speed = factor(rep(c(6,8,9),4)) 
carbon = factor(rep(c(0.77,0.78),6)) 
Torsion = factor(rep(c("OK", "NotOK"),c(8,4))) 
library(lattice) 
histogram(~Torsion|speed*carbon, layout = c(6,1),type = "count") 

Im obigen Beispiel möchte ich den folgenden Text "Häufigkeit jedes Balkens" oben auf jedem Balken des Histogramms hinzufügen. Wie mache ich das? Ich habe gegoogelt, konnte aber keine passende Lösung finden.Wie fügt man am oberen Rand jedes Balkens im Histogramm den Text der Anzahl oder Häufigkeit jedes Balkens hinzu?

+0

Einige Nicht-Gitter-Lösungen, die helfen könnten ... http://stackoverflow.com/q/ 33248627/2140956 –

+0

Nur um absolut sicher zu sein: Sie wollen die Frequenz von "Torsion" über jedem Balken, nicht wörtlich die Textzeichenfolge "Frequenz von jedem Balken", oder? – BenBarnes

+0

Ja. Ich möchte die Häufigkeit der Torsion und nicht die Textzeichenfolge. – JustCurious

Antwort

0

Ich habe die panel.histogram wie folgt umgeschrieben und die y-Achse manuell angepasst, um die Beschriftungen anzupassen (ich bin sicher, dass sie automatisch angepasst werden könnten).

panel_histtext <- function(x, breaks, equal.widths = TRUE, type = "density", 
          nint = round(log2(length(x)) + 1), 
          alpha = plot.polygon$alpha, col = plot.polygon$col, 
          border = plot.polygon$border,  
          lty = plot.polygon$lty, lwd = plot.polygon$lwd, 
          labels, ..., 
          identifier = "histogram") { 
    plot.polygon <- trellis.par.get("plot.polygon") 
    xscale <- current.panel.limits()$xlim 
    panel.lines(x = xscale[1] + diff(xscale) * c(0.05, 0.95), 
       y = c(0, 0), col = border, lty = lty, lwd = lwd, alpha = alpha, 
       identifier = paste(identifier, "baseline", sep = ".")) 
    if (length(x) > 0) { 
     if (is.null(breaks)) { 
     breaks <- if (is.factor(x)) 
      seq_len(1 + nlevels(x)) - 0.5 
     else if (equal.widths) 
      do.breaks(range(x, finite = TRUE), nint) 
     else quantile(x, 0:nint/nint, na.rm = TRUE) 
     } 
     h <- lattice:::hist.constructor(x, breaks = breaks, ...) 
     y <- switch(type, count = h$counts, percent = 100 * h$counts/length(x), 
        density = h$density) 
     breaks <- h$breaks 
     nb <- length(breaks) 
     if (length(y) != nb - 1) 
     warning("problem with 'hist' computations") 
     if (nb > 1) { 
     panel.rect(x = breaks[-nb], y = 0, height = y, width = diff(breaks), 
        col = col, alpha = alpha, border = border, lty = lty, 
        lwd = lwd, just = c("left", "bottom"), 
        identifier = identifier) 
     panel.text(x = breaks[-nb] + diff(breaks)/2, y = y, 
        pos = 3, labels = as.character(y)) 
     } 
    } 
    } 

Und aufgetragen dann das Diagramm mit dem neuen Panel-Funktion:

histogram(~ Torsion | speed * carbon, layout = c(6, 1), type = "count", 
      ylim = c(0, 2.3), 
      panel = function(x, ...) { 
      panel.histogram(x, ...) 
      panel_histtext(x, ...) 
      }) 

Imgur

Verwandte Themen