[R] ggplot2: labels points with different colors or idnumbers

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Feb 26 13:48:52 CET 2009


Dear Tom,

I think you better switch to ggplot() instead of qplot().

library(ggplot2)
dat <- expand.grid(id = 1:10, time = 1:3, group = 1:3)
dat$Freq <- rnorm(nrow(dat), mean = dat$id * dat$time)
ggplot(dat, aes(x = factor(time), y = Freq)) + geom_boxplot() + geom_jitter(aes(colour = factor(id)))

But you can do it with qplot()

qplot(factor(dat$time), dat$Freq, dat, geom = "boxplot") + geom_jitter(aes(colour = factor(dat$id))) 

IMO the ggplot version gives more readable code. Especially when you are doing complex plots.

Splitting plots according to a factor is easy with facet_grid() or facet_wrap().

dat <- expand.grid(id = 1:10, time = 1:3, Group = 1:3)
dat$Freq <- rnorm(nrow(dat), mean = dat$id * dat$time)
ggplot(dat, aes(x = factor(time), y = Freq)) + geom_boxplot() + geom_jitter(aes(colour = factor(id))) + facet_grid(. ~ Group)

If you want three different plots then you will need to do some reading on viewports, grobs and stuff. The ggplot2 book on Hadley's website give a good intro.

HTH,

Thierry

----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium 
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be 
www.inbo.be 

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Tom Cohen
Verzonden: donderdag 26 februari 2009 13:05
Aan: r-help at r-project.org
Onderwerp: [R] ggplot2: labels points with different colors or idnumbers

Dear list,
 
Using ggplot2 I could produce both boxplot and points in the same plot but instead of points I would like to label the different
subjects with different colors or their idnumbers. Is there away to do it? Also how can I put three plots on the same graph with ggplot2? 
mfrow=c(3,1) did not do the job. 
 
> dat
   group time   id  freq
1      1   00 0018  5.21
2      1   00 3026  3.13
3      1   00 5030  5.04
4      1   00 5108  3.23
5      1   00 5152  3.97
6      1   00 6080  0.16
7      1   01 0018  4.89
8      1   01 3026  6.58
9      1   01 5030  7.42
10     1   01 5108 10.10
11     1   01 5152  3.74
12     1   01 6080  0.81
 
library(ggplot2)
qplot(factor(dat$time),dat$freq,dat,geom=c("boxplot","jitter"),
    ylab=names(dat[,4]),xlab="time") 



      __________________________________________________________
Ta semester! - sök efter resor hos Kelkoo.
Jämför pris på flygbiljetter och hotellrum här:
http://www.kelkoo.se/c-169901-resor-biljetter.html?partnerId=96914052
	[[alternative HTML version deleted]]


Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.




More information about the R-help mailing list