How to label facets?

8 views
Skip to first unread message

Christian Lindig

unread,
Mar 4, 2009, 11:30:50 AM3/4/09
to ggplot2

This is probably a simple question for ggplot2 buffs:

library(ggplot2)
type <- floor(rnorm(50,3,1))
year <- floor(rnorm(50,2002,1))
df <- data.frame(year=factor(year),type=factor(type))
qplot(data=df,year,geom="bar",facets=.~type)

The plot above shows a bar graph facetted by a factor called type,
which is derived from an integer. I'd like to assign labels to these
numerical types like "one", "two" .. such that they show up instead of
the numerical values (1, 2, ..) of the factor.

Another problem is that I cannot get rid of the "count" label on the y
axis since setting ylab does not work. (I believe several people had
reported this problem).

-- Christian


David Kahle

unread,
Mar 4, 2009, 12:33:55 PM3/4/09
to Christian Lindig, ggplot2
It's not pretty, but this will clear up those problems.

library(ggplot2)
type <- floor(rnorm(50,3,1))
year <- floor(rnorm(50,2002,1))
df   <- data.frame(year=factor(year),type=factor(type))

df$type <- factor(df$type)
levels(df$type) <- c('zero','one','two','three',
  'four','five')

  

qplot(year, data=df, geom = "bar", facets = . ~ type) +
  scale_y_continuous('')  

Hope it helps
david.

David Kahle

unread,
Mar 4, 2009, 12:36:33 PM3/4/09
to Christian Lindig, ggplot2
Oh, and if it doesn't run the first time, just run it a few more times
(or tailor the code), the code I wrote works for the sample I got
which had a 5 and a 0 in it, so if yours doesn't have those...

But you get the idea.

david.


On Mar 4, 2009, at 10:30 AM, Christian Lindig wrote:

Reply all
Reply to author
Forward
0 new messages