[R] Problem to sort factor

hadley wickham h.wickham at gmail.com
Tue Jul 17 06:35:39 CEST 2007


On 7/16/07, Arne Brutschy <abr-r-project at xylon.de> wrote:
> Hello,
>
> I'm having a problem renaming and sorting the underlying factor of a
> ggplot2 based plot. Here's my code:
>
> ---8<----------
> > delta <- ggplot(subset(data, Model==c("dyn", "dl4", "dl3")), aes(x=Problemsize, y=Fitness)) +
>   geom_smooth(size=1, color="black", fill=alpha("blue", 0.2))+
>   geom_point(size=0.5, aes(colour=DeltaConfig))+
>   scale_colour_gradient2(expression(bold(paste(Delta,"Config"))), limits=c(0,10), midpoint=5,
>                          low="green", mid="yellow", high="red")
> > delta <- delta + facet_grid(Model ~ . , margins = TRUE)
> > delta
> ---8<----------
>
> and the data
> ---8<----------
> > data
>    Model Problemsize Fitness DeltaConfig
> 1    dl1           3 81.5271      2.4495
> 2    dl1           3 83.1999      2.4495
>      ...
> ---8<----------
>
> I want to select a subset of the possible models and display the
> resulting three plots in a column. This works fine, but: the displayed
> names and order is wrong. Instead of the models "dl3","dl4","dyn"
> I want to change dl* to rm* and the order to "dyn","rm3","rm4".
>
> I hope it's understandable what I want. I tried to use factor()
> function in a thousand combinations, but I don't seem to get it. Can
> someone help me?

Does this get you started?

x <- factor(c("a", "b", "c"))
factor(x, levels=c("c","b","a"), labels=c("cc","bb", "aa"))

Hadley



More information about the R-help mailing list