Hi:
First of all, you shouldn't be using boxplots when you have four observations per group - a strip chart or a dot plot would be a better choice in such a situation. (You should have a bare minimum of 20 observations per group for a boxplot, and some would suggest 30 or 40.) However, to answer your question, you need an indicator variable for the two groups of variable that you want to distinguish by color.
Let's try an example where boxplots would be more likely to apply.
library(reshape2)
library(ggplot2)
nms <- c(paste0("AB", 1:3), paste0("CD", 1:3))
gp <- c("AB", "CD")
DF <- data.frame(vgroup = rep(gp, each = 90),
var = rep(nms, each = 30),
y = c(rnorm(30, 20, 5), rnorm(30, 22.5, 6), rnorm(30, 20, 8),
rnorm(30, 35, 7), rnorm(30, 38, 10), rnorm(30, 40, 6))
ggplot(DF, aes(x = var, y = y, fill = vgroup)) +
geom_boxplot(alpha = 0.4) +
scale_fill_manual(values = c("orange", "blue"))
For your data, here are a few suggestions. Although they're not as 'glitzy' as a boxplot, they do show all the data, and transparency is a good thing.
DF0 <- read.table(header = TRUE, text = "
AB1 AB2 AB3 CD1 CD2 CD3
23 12 23 65 34 45
10 45 12 45 12 34
3 6 3 32 12 4
45 8 34 67 3 23")
DFm <- melt(DF0)
ggplot(DFm, aes(x = variable, y = value)) +
geom_point(size = 3)
# More like a Cleveland dot chart in appearance,
# but the same thing:
last_plot() + coord_flip()
# Color the points associated with the variable groups differently
DFm$vgrp <- rep(c("AB", "CD"), each = 12)
ggplot(DFm, aes(x = variable, y = value, color = vgrp)) +
geom_point(size = 3)
# Use faceting instead of color as a grouping device
ggplot(DFm, aes(x = variable, y = value)) +
geom_point() +
facet_wrap(~ vgrp, nrow = 1, scales = "free_x")
Dennis
On Wed, Mar 20, 2013 at 8:24 AM, Tauqeer Alam
<tauq...@gmail.com> wrote:
Hi AllI have a table with 4 rows and 6 columns and it looks like this-
AB1 AB2 AB3 CD1 CD2 CD3
10 45 12 45 12 34
3 6 3 32 12 4
I am trying to make a boxplot with AB1,....CD3 on the x-axis. Here are my steps-
> myData <- read.table("exam.dat",header=TRUE, sep=" ")
> attach(myData)
> x <- melt(myData)
> ggplot(data = x, aes(x=variable,y=value)) + geom_boxplot(colour="black",fill="aquamarine",outlier.shape = NA)
The above steps successfully produces a graph, but all boxes are in same color as provided in the "fill" option. How can I change the box colors? I want AB1, AB2 and AB3 in blue color, and CD1, CD2 and CD3 in red color.

Appreciate your help.
Tauqeer
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.