1

Hi have an experiment which consists of three variables, and I would like to plot them all on a single plot.

This is my df:

AB <- data.frame(block=c("A", "A", "A", "A", "B", "B", "B", "B" ),
  familiarity=c("fam", "fam", "unfam", "unfam" ),
  prime=c("P", "UP" ),
  RT=c("570.6929", "628.7446", "644.6268", "607.4312", "556.3581", "645.4821", "623.5624", "604.4113"))

Right now I can only break one of the variables into two separate plots, like this where A and B are the two levels of the third variable:

A <- AB[which(AB$block == "A"),]
B <- AB[which(AB$block == "B"),]

pa <- ggplot(data=A, aes(x=prime, y=RT, group=familiarity)) +
  geom_line(aes(linetype=familiarity), size=1) +
  expand_limits(y=c(500,650))

pb <- ggplot(data=B, aes(x=prime, y=RT, group=familiarity)) +
  geom_line(aes(linetype=familiarity), size=1) +
  expand_limits(y=c(500,650))

I would like to superimpose plot A over plot B, and have this third variables to be identified by color.

Any ideas?

9
  • Please provide a minimal reproducible example including sample data. Commented Nov 12, 2015 at 13:43
  • Many, but it's very hard to help you without seeing what your data looks like. Please provide a reproducible example. Commented Nov 12, 2015 at 13:43
  • Can't you just replace the values in third column by the colors and plot it in 2D. Again an example of your data would be great. Commented Nov 12, 2015 at 13:53
  • @Heroka the question has been edited Commented Nov 12, 2015 at 13:55
  • @stevezissou that makes solving things easier. Talking about solutions, are you aware that the rate at which you accept answers is very low, without any indications why the provided answers weren't appropriate. Commented Nov 12, 2015 at 14:14

2 Answers 2

6

Is this what you mean?

p_all <- ggplot(AB, aes(x=prime,y=RT,group=interaction(familiarity,block))) +
  geom_line(aes(linetype=familiarity,color=block)) 

enter image description here

Data used:

AB <- structure(list(block = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L), .Label = c("A", "B"), class = "factor"), familiarity = structure(c(1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L), class = "factor", .Label = c("fam", 
"unfam")), prime = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
), class = "factor", .Label = c("P", "UP")), RT = c(570.6929, 
628.7446, 644.6268, 607.4312, 556.3581, 645.4821, 623.5624, 604.4113
)), .Names = c("block", "familiarity", "prime", "RT"), row.names = c(NA, 
-8L), class = "data.frame")
Sign up to request clarification or add additional context in comments.

Comments

0

IF you have different datasets for those variables, then you can specify the data

ggplot()+
  geom_line(data=A, aes(x=prime, y=RT, group=familiarity,linetype=familiarity), size=1) +
  geom_line(data=B, aes(x=prime, y=RT, group=familiarity,linetype=familiarity), size=1)+
  expand_limits(y=c(500,650))

1 Comment

Thanks, but I need the two levels of the variable to be identified by color.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.