how to group smooth line by two groups?

90 views
Skip to first unread message

Lydia Jia

unread,
Oct 22, 2012, 10:27:16 AM10/22/12
to ggp...@googlegroups.com


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

Thanks a looooot!



Brandon Hurr

unread,
Oct 22, 2012, 10:45:26 AM10/22/12
to Lydia Jia, ggp...@googlegroups.com
Something like this?

ggplot(data, aes(x = a, y = b, linetype = sex, color=char, group = sex:char))+ 
scale_color_grey() +
scale_linetype_manual(breaks=c("male","female"), values=c(1,2), labels = c("male", "female")) +
geom_line()+
geom_point()+
theme_bw()


--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Lydia Jia

unread,
Oct 22, 2012, 10:52:20 AM10/22/12
to ggp...@googlegroups.com, Lydia Jia
this is nice! but what i need is regression lines instead of lines connecting scatter points. what should i do?

Lydia Jia

unread,
Oct 22, 2012, 10:53:32 AM10/22/12
to ggp...@googlegroups.com, Lydia Jia
and how should i change the shape of the points instead of the color of them?


On Monday, October 22, 2012 10:45:47 AM UTC-4, Brandon Hurr wrote:

Brandon Hurr

unread,
Oct 22, 2012, 10:58:59 AM10/22/12
to Lydia Jia, ggp...@googlegroups.com
I was going on the picture and not your code so I didn't use geom_smooth(). The idea is similar though... 

ggplot(data, aes(x = a, y = b, color = char, linetype = factor(sex), shape=char, group=char:sex))+ 
  geom_point(color="black")+
  scale_color_grey() +
  scale_linetype_manual(breaks=c("male","female"), values=c(1,2), labels = c("male", "female")) +
  geom_smooth(method = "lm", 
              se = FALSE, # Don't add shaded confidence region
              size = 1.2)+
  theme_bw()

Lydia Jia

unread,
Oct 22, 2012, 11:09:22 AM10/22/12
to ggp...@googlegroups.com, Lydia Jia
sorry i think i didn't make my question clear. 
the points should be on regression lines, just for identity. this means there should be six different line types. but there is no lines type with points that can be shaped. so i need to add points to them.
is there any commands that enable me to add points (shaped by char) onto the regression lines if i specify x value. thanks.

Brandon Hurr

unread,
Oct 22, 2012, 1:23:30 PM10/22/12
to Lydia Jia, ggp...@googlegroups.com
Not that I know of. You'll have to use lm() and predict() to make your points on the linear model output of geom_smooth(). 

Is the reason that you're doing that just for identification? Isn't that what the color x linetype tells you? 

Lydia

unread,
Oct 22, 2012, 1:48:37 PM10/22/12
to ggp...@googlegroups.com, Lydia Jia
Yes, just for identification. the three colors are great, but i have to identify lines by shape.......

Lydia

unread,
Oct 22, 2012, 2:13:51 PM10/22/12
to ggp...@googlegroups.com, Lydia Jia
could you please explain how to use predict() to make the points on the regression line? thanks.


On Monday, October 22, 2012 1:23:52 PM UTC-4, Brandon Hurr wrote:

Ben Bond-Lamberty

unread,
Oct 22, 2012, 2:22:57 PM10/22/12
to ggp...@googlegroups.com
m <- lm( y ~ x, data=d )
d$predictions <- predict( m )

Ben

Lydia

unread,
Oct 23, 2012, 4:56:16 PM10/23/12
to ggp...@googlegroups.com, Lydia Jia
Thanks a lot for answering my question. i will go with scale_color_grey()
but the color of the linetype(solid - male, dotted - female) in the legend is blue.. i think it is default. 
how can i make it black?
thanks!



On Monday, October 22, 2012 1:23:52 PM UTC-4, Brandon Hurr wrote:

Ben Bond-Lamberty

unread,
Oct 23, 2012, 5:56:59 PM10/23/12
to ggp...@googlegroups.com
It should be simply +geom_line(..., color="black" ) or +geom_smooth( ..., color="black" ) ?

Ben
Message has been deleted

Lydia

unread,
Oct 24, 2012, 9:28:35 AM10/24/12
to ggp...@googlegroups.com
but the color is defined by scale_color_manual or scale_color_grey 
so there is a problem: if i add color = black to geom_smooth, it would only give out black lines, not grey and light grey lines. 

Ben Bond-Lamberty

unread,
Oct 24, 2012, 10:02:46 AM10/24/12
to ggp...@googlegroups.com
Can you provide the exact code you're trying to run? I know you did originally, but as things have changed...

Thanks,
Ben

Lydia

unread,
Oct 24, 2012, 10:35:26 AM10/24/12
to ggp...@googlegroups.com
    pbaseline <- ggplot(data, aes(x = a, y = b, 
                                            color = char, linetype = sex))
    
    p0 <- pbaseline + 
      scale_color_manual(values = c("#000000", "#666666", "#CCCCCC")) +
      scale_linetype_manual(breaks=c("0","1"), values=c(1,2), labels = c("male", "female")) +
      geom_smooth(method = "lm", se = FALSE, size = 1.2) +  
      labs(linetype = "sex") +
      opts(title = expression("Title", face = "bold", size=20),
           panel.background = theme_rect(fill='white', colour='black'),
           axis.title.x = theme_text(size = 16, colour = 'black'),
           axis.title.y = theme_text(size = 16, colour = 'black', angle = 90),
           legend.text = theme_text(size = 14),
           legend.title = theme_text(face = "bold", size = 16),
           plot.title = theme_text(face = "bold", size = 20)) 

Brandon Hurr

unread,
Oct 24, 2012, 11:17:14 AM10/24/12
to Lydia, ggp...@googlegroups.com
Your code wasn't reproducible on my machine and you are using "old" stuff with opts()... The code below works for me, but I do not know how to change the default colors in the legends like you are requesting. I know there is a way as I have seen someone do it before by hacking the legend, but I can't find it in my searches. Hopefully someone who knows can chime in. 

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))
ggplot(data, aes(x = a, y = b, color = char, linetype = sex, group=char:sex))+
      scale_color_manual(values = c("#000000", "#666666", "#CCCCCC")) +
      #scale_linetype_manual(labels = c("male", "female"), guide="legend") +
      geom_smooth(method = "lm", se = FALSE, size = 1.2) +  
      labs(linetype = "sex") + ggtitle(expression("Title", face = "bold", size=20))+
      theme(
           panel.background = element_rect(fill='white', colour='black'),
           axis.title.x = element_text(size = 16, colour = 'black'),
           axis.title.y = element_text(size = 16, colour = 'black', angle = 90),
           legend.text = element_text(size = 14),
           legend.title = element_text(face = "bold", size = 16),
           plot.title = element_text(face = "bold", size = 20))

Ben Bond-Lamberty

unread,
Oct 24, 2012, 11:17:52 AM10/24/12
to ggp...@googlegroups.com
Simplifying for the moment, this:

pbaseline+geom_smooth(method = "lm", se = FALSE, size =
1.2)+scale_color_manual(values = c("#000000", "#666666", "#CCCCCC"))

produces a plot with the geom_smooth lines black, grey, etc., colored
by 'char'. Is this what you want?

Ben

Lydia

unread,
Oct 24, 2012, 11:27:20 AM10/24/12
to ggp...@googlegroups.com
this is nice. what i need more is to change the default line color in the "linetype" legend. do you have any idea how to do it? thanks.

Lydia

unread,
Oct 24, 2012, 11:28:14 AM10/24/12
to ggp...@googlegroups.com, Lydia
orz.... i didn't suppose it would be so hard... but thanks a lot!!

Ben Bond-Lamberty

unread,
Oct 24, 2012, 11:49:16 AM10/24/12
to ggp...@googlegroups.com
OK, I didn't expect this to be hard...but don't know, sorry. Maybe
someone more knowledgeable will weigh in.

One workaround would be to get rid of geom_smooth: fit the 'lm'
smoothing lines yourself, building a new data frame based on the model
fits, and then use geom_line to plot it. Presumably geom_line will
respect your color choices better.

Ben

Charlotte Wickham

unread,
Oct 24, 2012, 4:19:47 PM10/24/12
to Ben Bond-Lamberty, ggp...@googlegroups.com
You want to change the aesthetics but only in the guide,

pbaseline +
geom_smooth(method = "lm", se = FALSE, size =1.2) +
scale_color_manual(values = c("#000000", "#666666", "#CCCCCC")) +
scale_linetype(guide = guide_legend(override.aes = list(colour = "black")))


Charlotte

Lydia

unread,
Oct 24, 2012, 4:54:02 PM10/24/12
to ggp...@googlegroups.com, Ben Bond-Lamberty
Yeah, it works!!! Thank you soooooo much!!!!
Reply all
Reply to author
Forward
0 new messages