0

Aloha -

I am creating a series of histograms and would like to add a vertical line indicating the mean value of the distribution as well as a text label indicating the sample size. My code is as follows:

BIN_WIDTH <- 1 #desired bin width
print(histogram(~ Length..cm. | Method, #create and print the histogram and save to 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 by 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 this would be done using the panel.abline and panel.text functions inserted just after panel.histogram, but this does not seem to be working. What am I doing wrong? If someone can give me the code for a dummy vertical line (e.g. x=10) and dummy text, I can easily insert the equation for the mean and sample size.

Ben
  • 167
  • 1
  • 3
  • 10

1 Answers1

0

You'll need panel.lines() and panel.text(). For instance, the following function puts a horizontal line on a barchart and annotates it with text, as well as putting in a 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 at 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 the graph:

enter image description here

Simon
  • 10,679
  • 1
  • 30
  • 44
  • I'm afraid that doesn't seem to be working for me. It seems that it should, but I see neither the lines nor the text when I try to apply it to my code. – Ben May 21 '13 at 06:09
  • If you could post a small sample data set that I could try running with your function and say what version of R you are using, we might be able to diagnose the problem. I'm using R version 2.15.3, 64-bit on Windows 7 but I generated the graph above originally on R version 2.10 or so, 32-bit on Windows XP. – Simon May 21 '13 at 06:21
  • As a further thought, it can be tricky to get the co-ordinates right in `panel.text()` and `panel.lines()` - is it possible that you are plotting the text and line in a location that is well outside the visible window? – Simon May 21 '13 at 06:24
  • I finally got `panel.abline` to work. I hadn't been using `v=` to specify the x coordinate. – Ben May 21 '13 at 06:29
  • yes, that was the problem. `panel.text` was being plotted outside the window. Now, since this is being used in a loop, and the values for the axes are different in each plot, I need to figure out a way to specify the co-ordinates as the min and max values of the data :-) – Ben May 21 '13 at 06:35
  • Glad to help. Please check the answer as accepted if it was helpful to you. – Simon May 21 '13 at 06:38