How to plot data grouped by a factor, but not as a boxplot

I find the following solution:

stripchart(casp6~trans.factor,data.frame(casp6,trans.factor),pch=1,vertical=T)

simple and direct.

(Refer eg to http://www.mail-archive.com/[email protected]/msg34176.html)


You can do it with ggplot2, using facets. When I read "I want to create a plot where the data points are grouped as defined by the factor", the first thing that came to my mind was facets.

But in this particular case, faster alternative should be:

plot(as.numeric(trans.factor), casp6)

And you can play with plot options afterwards (type, fg, bg...), but I recommend sticking with ggplot2, since it has much cleaner code, great functionality, you can avoid overplotting... etc. etc.

Learn how to deal with factors. You got barplot when evaluating plot(trans.factor, casp6) 'cause trans.factor was class of factor (ironically, you even named it in such manor)... and trans.factor, as such, was declared before a continuous (numeric) variable within plot() function... hence plot() "feels" the need to subset data and draw boxplot based on each part (if you declare continuous variable first, you'll get an ordinary graph, right?). ggplot2, on the other hand, interprets factor in a different way... as "an ordinary", numeric variable (this stands for syntax provided by Jonathan Chang, you must specify geom when doing something more complex in ggplot2).

But, let's presuppose that you have one continuous variable and a factor, and you want to apply histogram on each part of continuous variable, defined by factor levels. This is where the things become complicated with base graph capabilities.

# create dummy data
> set.seed(23)
> x <- rnorm(200, 23, 2.3)
> g <- factor(round(runif(200, 1, 4)))

By using base graphs (package:graphics):

par(mfrow = c(1, 4))
tapply(x, g, hist)

ggplot2 way:

qplot(x, facets = . ~ g)

Try to do this with graphics in one line of code (semicolons and custom functions are considered cheating!):

qplot(x, log(x), facets = . ~ g)

Let's hope that I haven't bored you to death, but helped you!

Kind regards,
aL3xa


require(ggplot2)
qplot(trans.factor, casp6)

Tags:

Plot

R