[R] how to group smooth line by two groups?

Rui Barradas ruipbarradas at sapo.pt
Mon Oct 22 17:47:19 CEST 2012


Hello,

I'm not the greatest expert in ggplto2 but you're overcomplicating (if 
the word exists).

1. In a black&white plot don't use argument color nor scale_color_manual().
2. your second plot object, p0, is completely unnecessary.
3. if you want it to look like the graph in the link, it's geom_point, 
_not_ geom_smooth.

pb <- ggplot(dat, aes(x = a, y = b,
                        shape = char, linetype = factor(sex)))
pb + geom_point() + geom_line() + facet_grid(sex ~ .)


Note that what gives you the second plot is facet_grid(). Try reversing 
the formula for a vertical orientation.

Hope this helps,

Rui Barradas
Em 22-10-2012 15:31, autumn escreveu:
> Hello, I have the following sample dataset.
>
> sex <- as.factor(c(rep(0,12),rep(1,12)))
> char <-
> c("x","x","x","x","y","y","y","y","z","z","z","z","x","x","x","x","y","y","y","y","z","z","z","z")
> a <- runif(24,0,2)
> b <- runif(24,4,10)
> data <- data.frame(cbind(sex,char,a,b))
>
> I would like to generate a blak and white plot with two groups of three
> smooth lines. linetypes are recognized by sex, and each line with dots,
> squares and triangles according to the group of char. the following is one
> group, i need to plot two, one type is solid, the other type is dotted. how
> to do it?
>
> I tried a color plot with the following code. what should i change "color"
> to?
>
> pb <- ggplot(data, aes(x = a, y = b,
>                         color = char, linetype = factor(sex)))
>
> p0 <- pb +
>    scale_color_manual(values = c("#00FFFF", "#FFFF00", "#00FF00")) +
>    scale_linetype_manual(breaks=c("0","1"), values=c(1,2), labels = c("male",
> "female")) +
>    geom_smooth(method = "lm",
>                se = FALSE, # Don't add shaded confidence region
>                size = 1.2, aes(group = char : sex))
> <http://r.789695.n4.nabble.com/file/n4647019/1.bmp>
> Thanks a looooot!
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/how-to-group-smooth-line-by-two-groups-tp4647019.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list