About Bar chart

13 views
Skip to first unread message

teragene

unread,
Feb 25, 2009, 7:53:12 PM2/25/09
to ggplot2
I am just a beginner of ggplot2.
I want fill color to specific bar (different color for only some
groups).
I mean I have 9 bar on the graph. I want fill blue to 3 bars and fill
red to 1 bar, rest of bars grey.
Would you advise me what option I can apply?

THANK YOU.

James Howison

unread,
Feb 26, 2009, 10:14:34 AM2/26/09
to ggplot2

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 :)

David Kahle

unread,
Feb 26, 2009, 10:44:05 AM2/26/09
to James Howison, ggplot2
James' answer works with a very slight tweek -

ggplot(data=df, aes(x=name, y=grade, fill=section)) +
  geom_bar() + 
  scale_fill_manual(values = c('Red','Blue','Gray'))

where the data set is the one he suggested,

df <- data.frame(
name = c('student1','student2','student3','student4','student5','student6','student7','student8','student9'),
grade = c(56,68,58,99,46,44,34,56,76),
section = factor(c('A', 'A', 'A', 'B', 'C', 'C', 'C', 'C', 'C'))
)

The order of the colors will follow the order of the levels in the factor vector section.  How is that?

Cheers
david.

hadley wickham

unread,
Feb 27, 2009, 11:08:04 AM2/27/09
to James Howison, ggplot2
> 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"))

Look at levels(df$section) - there's a leading space in front of each
of the labels.

Hadley

--
http://had.co.nz/

James Howison

unread,
Feb 27, 2009, 12:00:45 PM2/27/09
to ggplot2

Ahah, that makes sense. My fault for manually creating the csv file :)

--J

Reply all
Reply to author
Forward
0 new messages