I'm plotting data by two categories, first the underlying data:
library(plyr)
library(ggplot2)
d1 <- data.frame(v1 = rnorm(30),
x = rep(letters[1:6],
c(6, 3, 8, 3, 4, 6)),
group = rep(paste("g", 1:3, sep = ""),
c(9, 11, 10)))
Then I order factors
of <- ddply(d1, .(x), function(i) mean(i$v1))
of <- of[order(-of$V1),]
d1$x <- factor(d1$x, levels = of$x)
And this provides the following plot
ggplot(d1) + geom_point(aes( x = v1, y = x)) +
facet_grid(group ~ .,
scales = "free_y",
space = "free_y")
But thne I also want to add a separate horizontal boxplot for each group, depicting the relevant distribution. I know the first step is to provide a "Total" space in the y axis:
d2 <- rbind(d1,
data.frame(v1 = NA,
x = "Total",
group = unique(d1$group)))
ggplot(d2) + geom_point(aes( x = v1, y = x)) +
facet_grid(group ~ .,
scales = "free_y",
space = "free_y")
Which provides the following, and here I'm stuck
I just want a horizontal boxplot next to the "Total" axis break, summarizing each group's data.
Thanks in advance.
Starting point would be to make two data frame - d1
as your original data frame and d2
where x values all are Total
.
d2 <- d1
d2$x <- "Total"
Then use d1
to make points and d2
to make boxplots. Here is example with vertical alignment of boxplots where x
values are used as x. Problem is that to have horizontal boxplot you have to use coord_flip()
. Flipped axis and free scales can't be used together.
ggplot()+geom_point(data=d1,aes(x=x,y=v1))+
geom_boxplot(data=d2,aes(x=x,y=v1))+
facet_wrap(~group ,
scales = "free_x")
Workaround to get in direction you need would be to make three separate plots (one for each group) and then put them together with grid.arrange()
for library gridExtra
. For the first and second plot I removed all ticks, texts and titles for x axis.
library(gridExtra)
p1<-ggplot()+geom_point(data=subset(d1,group=="g1"),aes(x=x,y=v1))+
geom_boxplot(data=subset(d2,group=="g1"),aes(x=x,y=v1))+
coord_flip()+
facet_grid(group~.)+
scale_y_continuous(limits=c(-2,2.5))+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())
p2<-ggplot()+geom_point(data=subset(d1,group=="g2"),aes(x=x,y=v1))+
geom_boxplot(data=subset(d2,group=="g2"),aes(x=x,y=v1))+
coord_flip()+
facet_grid(group~.)+
scale_y_continuous(limits=c(-2,2.5))+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())
p3<-ggplot()+geom_point(data=subset(d1,group=="g3"),aes(x=x,y=v1))+
geom_boxplot(data=subset(d2,group=="g3"),aes(x=x,y=v1))+
coord_flip()+
facet_grid(group~.)+
scale_y_continuous(limits=c(-2,2.5))
grid.arrange(p1,p2,p3)