I almost got this working (thought it was going to be easy), see
question on scale_fill_manual below.
Given this data in a file:
name, grade, section
student1, 56, A
student2, 68, A
student3, 58, A
student4, 99, B
student5, 46, C
student6, 44, C
student7, 34, C
student8, 56, C
student9, 76, C
Where 'section' is the categorical variable which is going to control
the color of the bars, this code is a start for you:
testdata <- read.csv("test-data.csv")
p <- ggplot(data=testdata,aes(x=name, y=grade,fill=section))
p + geom_bar()
So far so good.
Now the question is, how do I specifically control the colors used for
the bars? You want to map A to blue, B to read and C to grey.
I tried this, based on the documentation of scale_manual, but
disappointingly it didn't work (got a nice legend, but no bars!)
p + geom_bar() + scale_fill_manual(values = c("A" = "blue","B" =
"red","C" = "grey"))
perhaps this has something to do with the section values being factors
whereas in the example the values are continuous? Anyone finish this
off?
Now it may be that you have to add a column to your data, or reshape
it (see ?melt) so that it matches this type of data table, so that you
have a variable to control the color of your bars.
If you need to manually control the order of your bars then you need
to treat testdata$name as an ordered factor, eg moving student7 to the
left:
testdata$name <- factor(testdata$name,
levels
=
c
("student7
","student1","student2","student3","student4","student5","student6",
"student8","student9",ordered=T))
hth,
James
ps. To get better answers try to provide some sample data and the code
that you've already tried, plus the documentation you read and why it
didn't help; that way everyone can learn something from your issues,
even if it's just how to explain things better :)
Look at levels(df$section) - there's a leading space in front of each
of the labels.
Hadley
Ahah, that makes sense. My fault for manually creating the csv file :)
--J