2

I have the following code which plots points and draw a line between them.

ggplot (data = subset(df, vowel == "O" & gender == "f"), aes (x = time, y = val, color = formant)) +
      geom_point()+
      geom_line(aes(group=interaction(formant, number)))

It produces this:

enter image description here

Is there a way to group these by color/line type for negative slopes vs. positive slopes of these lines?

edit: Here is my data:

number <- c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)
formant <- c("F2", "F2", "F2", "F2", "F2", "F2", "F3", "F3", "F3", "F3", "F3", "F3")
time <- c(50, 50, 50, 99, 99, 99, 50, 50, 50, 99, 99, 99)
val <- c(400, 500, 600, 450, 550, 650, 300, 400, 500, 250, 350, 450)

I want to show movement of in the value of val over time grouped by formant and number. So when I implement the answer, it tells me I have an incompatible size, which I think has something to do with the fact that it's grouped by number.

Lisa
  • 909
  • 2
  • 13
  • 31

1 Answers1

12

You haven't provided sample data, so here's a stylized example. The general idea is that you create a variable that tests whether the slope is greater than zero and then map that to a colour aesthetic. In this case, I use the dplyr chaining operator (%>%) in order to add the slope on the fly within the call to ggplot. (I went to the trouble of calculating the slope, but you could just as well test whether value[t==2] > value[t==1] instead.)

library(dplyr)

# Fake data
set.seed(205)
dat = data.frame(t=rep(1:2, each=10), 
                 pairs=rep(1:10,2), 
                 value=rnorm(20), 
                 group=rep(c("A","B"), 10))

dat$value[dat$group=="A"] = dat$value[dat$group=="A"] + 6

ggplot(dat %>% group_by(pairs) %>%
         mutate(slope = (value[t==2] - value[t==1])/(2-1)),
       aes(t, value, group=pairs, linetype=group, colour=slope > 0)) +
  geom_point() +
  geom_line()

enter image description here

UPDATE: Based on your comment, it sounds like you just need to map number to an aesthetic or use faceting. Here's a facetted version using your sample data:

df = data.frame(number, formant, time, val)

# Shift val a bit
set.seed(1095)
df$val = df$val + rnorm(nrow(df), 0, 10)

ggplot (df %>% group_by(formant, number) %>%
          mutate(slope=(val[time==99] - val[time==50])/(99-50)), 
        aes (x = time, y = val, linetype = formant, colour=slope > 0)) +
  geom_point()+
  geom_line(aes(group=interaction(formant, number))) +
  facet_grid(. ~ number)

enter image description here

Here's another option that maps number to the size of the point markers. This doesn't look very good, but is just for illustration to show how to map variables to different "aesthetics" (colour, shape, size, etc.) in the graph.

ggplot (df %>% group_by(formant, number) %>% 
          mutate(slope=(val[time==99] - val[time==50])/(99-50)), 
        aes (x = time, y = val, linetype = formant, colour=slope > 0)) +
  geom_point(aes(size=number))+
  geom_line(aes(group=interaction(formant, number)))
eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Thank you! But, this doesn't end up working with my data, since it has to be grouped by an arbitrary number. Sorry I didn't post my data originally, I will write the code recreating it. – Lisa Dec 06 '15 at 14:52
  • Is there a shorter way because if i make my database dynamic like with time-series, the 2 - 1 in the slope calculation part will become useless as it is too small(as it shall have many consecutive line) – rahul yadav Jul 06 '18 at 05:54