r - Adding mean line and text label to lattice histogram -
aloha -
i creating series of histograms , add vertical line indicating mean value of distribution text label indicating sample size. code follows:
bin_width <- 1 #desired bin width print(histogram(~ length..cm. | method, #create , print histogram , save variable "graph" data = hist.data[hist.data$scientific_name == "pristipomoides filamentosus",], nint = (max(hist.data$length..cm.) - min(hist.data$length..cm.)+1)/bin_width, layout = c(1,2), type = "density", main = "length-frequency of pristipomoides filamentosus gear", xlab = "length (cm)", panel = function(x, ...){ panel.histogram(x,...) panel.mathdensity(dmath = dnorm, col = "red", args = list(mean = mean(x), sd= sd(x)), ...) } ))
i assumed done using panel.abline
, panel.text
functions inserted after panel.histogram
, not seem working. doing wrong? if can give me code dummy vertical line (e.g. x=10) , dummy text, can insert equation mean , sample size.
you'll need panel.lines()
, panel.text()
. instance, following function puts horizontal line on barchart , annotates text, putting in customised legend:
function () { trellis.device(width = 5, height = 5, new = f) xx <- gerd95.06 xx$country <- c("usa", "singapore", "denmark", "australia", "nz") barchart(x1995 + x2006 ~ reorder(country, xx$x2006), data = xx, ylab = "gerd per capita, nominal $us ppp", cex = 0.8, panel = function(...) { panel.lines(c(0.7, 5), c(720, 720), col = "gray", lwd = 4) panel.text(lab = "oecd avg 2006", x = 1, y = 750, adj = c(0.4, 0), cex = 0.7) panel.text(lab = "nz @ 2.5% of gdp", x = 1, y = 630, adj = c(0.4, 0), cex = 0.7) panel.text(lab = "1995", x = 1.5, y = 900, adj = c(1, 0.5)) panel.rect(xleft = 1.6, xright = 2, ybottom = 870, ytop = 930, col = 3) panel.text(lab = "2006", x = 1.5, y = 1000, adj = c(1, 0.5)) panel.rect(xleft = 1.6, xright = 2, ybottom = 970, ytop = 1030, col = 8) panel.barchart(..., col = c(3, 8)) panel.rect(xleft = 1, xright = 1.3333, ybottom = xx$x2006[xx$country == "nz"], ytop = 2.5/1.206 * xx$x2006[xx$country == "nz"]) }, ylim = c(0, 1200)) }
... produces graph:
Comments
Post a Comment