Articles

phantom text in graphics

Image
  The title function allows you to change the color of the text using the col argument, but that color is applied to the entire text string -- there's no obvious way to set the color of individual words. Or is there?  Barry Rowlingson offers an  elegant solution  that uses the "overhead transparency" principle of R graphics: you can overlay additional graphical elements one atop another, to build up your graph layer by layer.  So you could add the title  Hair color  in red on the left, and  Eye color  in blue on the right, and put a black "and" in the middle.  The trick is in the positioning -- it could take a lot of trial and error to get the  x  position of each element correct.  But if you plot the same text three times in three different colors, but leave some words blank (so they won't overlay previously plotted elements) you don't have to worry about positioning at all.  The  phantom  notation allows y...

Force system messages to be in English

 > warning("Essai") Message d'avis : Essai  > Sys.setenv(LANG = "en") > warning("Essai") Warning message: Essai 

Distribution when only quantiles are known

Imagine that you have a credible interval (0.01, 0.3) for (0.025, 0.975) probabilities for one proportion value and you would like to use this variable. The solution is to search for the beta distribution that gives this credible interval: library("HelpersMG") best <- fitdistrquantiles(quantiles = c(0.01, 0.3), probs=c(0.025, 0.975), scaled=FALSE, distribution = "beta") Then it is possible to try the result: rd10000 <- rbeta(10000, shape1 = best["shape1"], shape2 = best["shape2"]) quantile(rd10000, probs=c(0.025, 0.975)) It works ! This function can fit also more than 2 quantiles and can be used for gamma and normal distributions. Now let do a simple exercise Let p and q two independent proportions being known only from their confidence interval and median: p <- c("2.5%"=0.012, "50%"=0.14, "97.5%"=0.25) q <-  c("2.5%"=0.37, "50%"=0.52, "97.5%"=0.75) I want the confidence inte...

utf8 code

 http://www.ltg.ed.ac.uk/~richard/utf-8.cgi Choose Character, enter the characters to convert in the text box; Example •, will give you: Character • Character name BULLET Hex code point 2022 Decimal code point 8226 Hex UTF-8 bytes E2 80 A2  Octal UTF-8 bytes 342 200 242  UTF-8 bytes as Latin-1 characters bytes â <80> ¢ Then: x <- "\u2022" print(x) [1] "•"

Angle of line linking two points

 Let center.x and center.y be the center of the Cartesian plan. Let x1 and y1 be the coordinate of the point. The angle between (center.x, center.y) and (x1, y1) is: (atan((y1-center.y)/(x1-center.x))+pi*(as.numeric((x1-center.x)<0))) %% (2*pi) The 0 is at right of the center, pi is at left, pi/2 at the top and 3 pi/2 at the bottom.

Convert a matrix to vector and back

  You must use byrow = FALSE (the default) to convert the vector back to a matrix. > (d <- matrix(1:6, nrow=2, ncol=3))      [,1] [,2] [,3] [1,]    1    3    5 [2,]    2    4    6 > (v <- as.vector(d)) [1] 1 2 3 4 5 6 > matrix(v, nrow=2, ncol=3, byrow = FALSE)      [,1] [,2] [,3] [1,]    1    3    5 [2,]    2    4    6

An example of barplot with ggplot2

  From R-help discussion list; answer by Rui Barradas. I like this answer and I copy it here: h <- c(574,557,544,535,534,532,531,527,526,525,         524,520,518,512,507,504,504,489,488,488,         487,484,484,474,472,455,444,420)  nms <- c("Fribourg(f)","Valais(d)","Appenzell Rhodes Intérieures",           "Fribourg(d)","Jura","Schwyz","Schaffhouse","Berne(f)",           "Thurgovie","Valais(f)","Argovie","Appenzell Rhodes Extérieures",           "Genève","Zoug","Tessin","Neuchâtel","Vaud","Uri","Nidwald",           "Berne(d)","Zurich","Obwald","Saint-Gall","Soleure","Lucerne",           "Glaris","Bâle-Campagne","Bâle-Ville")  nms <- factor(nms, levels = nms)  library...