MalteseUnderdog
MalteseUnderdog

Reputation: 2011

R ggplot2 how to plot a summary statistic (example within)

I have a data frame which can be generated with:

a <- c(rep("w",3), rep("x",3), rep("y",3), rep("z",3))
b <- rnorm(12)
c <- c(rep(rnorm(1), 3), rep(rnorm(1), 3),rep(rnorm(1), 3),rep(rnorm(1), 3))
d <- c(1,1,1,2,2,2,1,1,1,2,2,2)
test <- data.frame(a,b,c,d)

I want to generate boxplots for the "b" column, and on each boxplot show the value of the c column - which is a sort of reference value for each "a" (hence the repetitions). I am almost there with the following, which also shows how I want the data displayed (facet):

library(ggplot2)
p <- ggplot(test) 
p <- p + geom_boxplot(aes(a, b)) 
p <- p + geom_point(aes(a, c), colour="red", shape=4, size=4)
p <- p + facet_grid(. ~ d, , scales="free_x")
p

The only problem with this approach is that there are really 3 geom_points (on top of each other) for every "a" value - how do I remove this inefficiency? I just want one geom_point.

Many Thanks

Upvotes: 1

Views: 689

Answers (1)

joran
joran

Reputation: 173577

I think you just want to use the unique values as a separate data frame passed to geom_point:

p <- p + geom_point(data = unique(test[,c('a','c','d')]),
                    aes(a, c), colour="red", shape=4, size=4)

Upvotes: 2

Related Questions