Здесь вы должны использовать пакет grid
, чтобы решить вашу проблему, поместив ваш ggplot и таблицу в два стекированных окна просмотра
#your code, use a variable p to hold the ggplot object
p <- ggplot(df, aes(x=avisit, y=mean, group=Type, color=Type, shape=Type)) +
scale_y_continuous(breaks=seq(0,18,2), limits=c(0, 18)) +
geom_point(position=pd, cex=2) +
xlab("") +
ylab("Mean") +
scale_colour_manual(values=c("blue", "red")) +
scale_shape_manual(values=c("triangle", "circle")) +
coord_cartesian(ylim = c(0, 18)) +
facet_grid(.~factor(visit), scales = "free_x", space ="free_x",switch = "both") +
theme_bw() +
theme(axis.text.y = element_text(margin = margin(r = 0)),
panel.spacing = unit(0, "mm"),
strip.background = element_blank(),
legend.title=element_blank(),
strip.placement = "outside",
legend.background = element_rect(color="black", fill="white", size=0.5, linetype="solid"),
legend.direction = "horizontal",
panel.grid.minor = element_line(colour="white", linetype="dashed"),
panel.grid.major = element_line(colour = "white",linetype="dashed"))
#--my code starts here--
#create a new blank viewport
grid.newpage()
#creating a viewport 5rows by 1col
pushViewport(viewport(layout = grid.layout(5, 1)))
#put the ggplot object into the first to fourth layout
print(p, vp = viewport(layout.pos.row = 1:4, layout.pos.col = 1))
grid.text(label = "TypeA 60 60 60 59 59 58 58 57 57 57 57 57", vp = viewport(layout.pos.row = 5, layout.pos.col = 1), x = unit(0.01, "npc"), y = unit(0.7, "npc"), just = "left", gp = gpar(fontsize = 10))
grid.text(label = "TypeB 59 59 59 58 58 58 58 57 57 56 56 56", vp = viewport(layout.pos.row = 5, layout.pos.col = 1), x = unit(0.01, "npc"), y = unit(0.45, "npc"), just = "left", gp = gpar(fontsize = 10))
grid.text(label = "No. of Subject", x = unit(0.01, "npc"), y = unit(0.9, "npc"), just = "left", gp = gpar(fontsize = 10), vp = viewport(layout.pos.row = 5, layout.pos.col = 1))