Mittwoch, 18. August 2010

Barplot mit Fehlerbalken




hh <- t(VADeaths)[, 5:1]
ci.l <- hh * 0.85
ci.u <- hh * 1.15


mb <- barplot(hh, beside = TRUE, ylim = c(0, 100)
  , col = c("lightblue", "mistyrose","lightcyan", "lavender")
  , main = "Death Rates in Virginia", font.main = 4
  , sub = "Faked 95 percent error bars", col.sub = "gray20"
  , cex.names = 1.5
  , legend.text = colnames(VADeaths), args.legend = list( bg="white" )
  , panel.before = {
      rect( xleft=par()$usr[1], ybottom=par()$usr[3], xright=par()$usr[2], ytop=par()$usr[4]
        , col="gray99" )
      grid( nx=NA, ny=NULL ) # horiz grid only
      box()
  }
  , xpd=F )


arrows( x0=mb, y0=ci.l, y1 = ci.u, angle=90, code=3, length=0.05 )


mtext( side = 1, at = colMeans(mb), line = 2,
text = paste("Mean", formatC(colMeans(hh))), col = "red" )

Freitag, 6. August 2010

Simple textplot

Putting text on a plot is not that straight on, especially if there's more than one line of text.

data(iris)

# put the summary output into a variable
out <- capture.output
  summary(lm(Sepal.Length ~ Species + Petal.Width, iris)) )

cat( out, sep="\n" )

# create plot
plot.new()

# print text in mono font
text(labels=out, x=0
  , y=rev(1:length(out)) * strheight( "S", cex=0.8 ) * 1.3
  , adj=c(0,0), family="mono", cex=0.8 )



This is what I thought so far. Of course it is straight on to place bulk text on several lines, stupid...
Just collapse the text with newline as separator:

text(labels=paste(out, collapse="\n"), x=0, y=0
      , adj=c(0,0), family="mono", cex=0.8 )