Exactly right :) However, that's not actually needed for this chart:
note that the scales hardly overlap so you don't lose anything but
having two separate plots on top of each other.
# Restructure data into a single data frame
RT = matrix(c(814, 500, 424, 394, 967, 574, 472, 446),4,2)
colnames(RT) = c('repetition','alternation')
rownames(RT) = c('7-yrs','11-yrs','15-yrs','21-yrs')
library(ggplot2)
rt <- melt(RT)
names(rt) <- c("age", "trt", "value")
rt$variable <- "rt"
errors <- melt(as.matrix(error))
names(errors) <- c("age", "trt", "value")
errors$variable <- "error"
df <- rbind(rt, errors)
# Make sure factors are ordered correctly
df$age <- factor(df$age, levels = c("7-yrs", "11-yrs", "15-yrs", "21-yrs"))
# Which results in:
structure(list(age = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("7-yrs", "11-yrs",
"15-yrs", "21-yrs"), class = "factor"), trt = structure(c(2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label =
c("alternation",
"repetition"), class = "factor"), value = c(814, 500, 424, 394,
967, 574, 472, 446, 10, 6, 6, 4, 13, 7, 6, 3), variable = c("rt",
"rt", "rt", "rt", "rt", "rt", "rt", "rt", "error", "error", "error",
"error", "error", "error", "error", "error")), .Names = c("age",
"trt", "value", "variable"), row.names = c(NA, 16L), class = "data.frame")
# It's now straightforward to create recreate the plot with lines
# or with bars:
plot <- ggplot(df, aes(age, value, colour = trt, fill = trt)) +
facet_grid(variable ~ . , scale = "free")
plot + geom_bar(position = "dodge")
plot + geom_line(aes(group = trt)) + geom_point()
# It's then just a matter of combining the two:
plot +
geom_bar(position = "dodge", data = subset(df, variable == "rt")) +
geom_line(aes(group = trt), data = subset(df, variable == "error"))
# Unfortunately this currently results in an error, which
# I've added to my to do list to fix
library(ggplot2)
RT = matrix(c(814, 500, 424, 394, 967, 574, 472, 446),4,2)
colnames(RT) = c('repetition','alternation')
rownames(RT) = c('7-yrs','11-yrs','15-yrs','21-yrs')
rt <- melt(RT)
names(rt) <- c("age", "trt", "value")
rt$variable <- "rt"
#this next block was missing in Hadley's previous example
error = matrix(c(10,6,6,4,13,7,6,3),4,2)
colnames(error) = c('repetition','alternation')
rownames(error) = c('7-yrs','11-yrs','15-yrs','21-yrs')
errors <- melt(error)
names(errors) <- c("age", "trt", "value")
errors$variable <- "error"
df <- rbind(rt, errors)
# Make sure factors are ordered correctly
df$age <- factor(df$age, levels = c("7-yrs", "11-yrs", "15-yrs", "21-yrs"))
# Which results in:
structure(list(age = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("7-yrs", "11-yrs",
"15-yrs", "21-yrs"), class = "factor"), trt = structure(c(2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label =
c("alternation",
"repetition"), class = "factor"), value = c(814, 500, 424, 394,
967, 574, 472, 446, 10, 6, 6, 4, 13, 7, 6, 3), variable = c("rt",
"rt", "rt", "rt", "rt", "rt", "rt", "rt", "error", "error", "error",
"error", "error", "error", "error", "error")), .Names = c("age",
"trt", "value", "variable"), row.names = c(NA, 16L), class = "data.frame")
# It's now straightforward to create recreate the plot with lines
# or with bars:
plot <- ggplot(df, aes(x=age, y=value, colour = trt, fill = trt)) +
Not at the moment. I have some changes further down in the pipeline
that should make this easier.
Hadley