我有三个网站收集的数据,其中每个这些数据收集几个主题的几次。ggplot一组直方图与直方图作为subplots
这里的数据是如何模样:
set.seed(1)
df <- data.frame(site = c(rep("AA",1000),rep("BB",500),rep("CC",750)),
y = c(rnorm(1000,1,2),runif(500,1,3),rgamma(750,shape=1)))
#add subjects - using a function that randomly generates
#a number of subjects that adds up to their total at that site
site_a_subjects <- diff(c(0, sort(20*sample(19)), 1000))
site_b_subjects <- diff(c(0, sort(30*sample(9)), 500))
site_c_subjects <- diff(c(0, sort(40*sample(4)), 750))
#add these subjects
df$site_subjects <- c(unlist(sapply(1:20, function(x) rep(letters[x], site_a_subjects[x]))),
unlist(sapply(1:10, function(x) rep(letters[x], site_b_subjects[x]))),
unlist(sapply(1:5, function(x) rep(letters[x], site_c_subjects[x]))))
我想绘制的y
每个每个站点的直方图。 这ggplot2
简单的线条实现了轻松:
ggplot(df, aes(x=y)) + geom_histogram(colour="black", fill="white") + facet_grid(. ~ site)
不过,我想另外在每个站点直方图绘制,一个插曲是在该网站每个受试者观察数的计数的直方图。 喜欢的东西加入:
hist(table(df$site_subjects[which(df$site == "AA")]))
hist(table(df$site_subjects[which(df$site == "BB")]))
hist(table(df$site_subjects[which(df$site == "CC")]))
分别
三个网站直方图。
任何想法如何做到这一点?
我不知道是否可以调整annotation_custom
来达到这个目的吗?
此代码将工作,但前提是:
ggplotGrob(ggplot(df, aes(x=site_subjects)) + geom_bar() + theme_bw(base_size=9))
命令可以接受ggplot
的list
一个对象或类似的东西。
这里是'几乎;解决方案: 首先要弄清楚什么是所有方面中的最大栏高度直方图
ymax <- max(sapply(unique(df$site), function(x) max(hist(df$y[which(df$site == x)],plot=FALSE)$counts)))
然后:
main.plot <- ggplot(df, aes(x=y)) + geom_histogram(colour="black", fill="gray") + facet_grid(~site) + scale_y_continuous(limits=c(0,1.2*ymax))
main.plot.info <- ggplot_build(main.plot)
xmin <- min(main.plot.info$data[[1]]$x[which(main.plot.info$data[[1]]$PANEL == 1)])
xmax <- max(main.plot.info$data[[1]]$x[which(main.plot.info$data[[1]]$PANEL == 1)])
main.plot <- main.plot + annotation_custom(grob = grid::roundrectGrob(),xmin = xmin, xmax = xmax, ymin=ymax, ymax=1.2*ymax)
sub.plot <- ggplotGrob(ggplot(df, aes(x=site_subjects)) + geom_bar() + theme_bw(base_size=9))
combined.plot <- main.plot + annotation_custom(grob = sub.plot, xmin = xmin, xmax = xmax, ymin=ymax, ymax=1.2*ymax)
尼斯!有没有办法预先确定主图的facet的x位置,以便将它们传递给my_vp而不是对值进行硬编码? – user1701545
此外,我将如何更改此代码,以便我可以将图形绘制到文件中? – user1701545
对于第二个问题,在开始主要绘图之前,执行'tiff(“myplot.tiff”,1000,800)'(或任何文件名和分辨率)然后在添加所有插入图之后执行'dev.off ()'。 – eipi10