我有以下使用ggplot和gridExtra包的函数:
new_func <- function(df, df2, df3, output.proj, typenames) {
require(ggplot2)
require(gridExtra)
plot1 <- ggplot(aes(x=Procs2, y=Procs1,
fill=P),environment = environment(),data=df) + geom_tile() +
xlab(colnames(df)[2]) +
ylab(colnames(df)[1]) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
plot.margin=unit(c(0.5,0.5,-0.5,1.5), "cm")) +
geom_text(aes(fill = P,
label = P),size=3) +
theme(axis.text.x = element_text(angle=30, vjust=0.5)) +
scale_fill_gradient(low = "gray37", high = "white")
plot2 <- ggplot(df2, aes(Proc, Loc)) + geom_boxplot(aes(fill=Proc),
environment = environment()) +
stat_boxplot(geom ='errorbar') +
scale_colour_manual(breaks = sort(typenames),
labels = sort(typenames),
values = colorRampPalette(c("blue","goldenrod1"))(6) ) +
scale_fill_manual(breaks = sort(typenames),
labels = sort(typenames),
values = colorRampPalette(c("blue","goldenrod1"))(6) ) +
theme(axis.text.x = element_text(angle=45, vjust=0.5)) +
geom_text(aes(y=max(Loc)+0.5,
label =lab ),size=2.5,
col=rep(colorRampPalette(c("blue","goldenrod1"))(6),each=22) ) +
theme(axis.line=element_line(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.position="none")
plot3 <- ggplot(df3, aes(x=Proc, y=types),environment = environment()) +
geom_bar(stat="identity", aes(fill=Proc),width=0.5) +
facet_wrap(~Groc,nrow=2, scales="fixed") + # fixed
theme(axis.text.x = element_blank() )+#element_text(angle=90, vjust=0)) +
theme(axis.line=element_line(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank()) +
scale_fill_manual( breaks = df3$Proc,
labels = df3$Proc,
values = c("red", colorRampPalette(c("blue","yellow"))(6)))
pdf(paste(output.proj,".pdf",sep=""),width=10)
grid.arrange(arrangeGrob(plot1, plot2,ncol=2, nrow=1), plot3, nrow=2,
environment = environment())
dev.off()
}
当我调用此函数时:
new_func(df,df2,df3,"test",typenames)
我收到以下错误:
arrangeGrob错误(...,as.table = as.table,clip = clip,main = main,:输入必须是grobs!
在保留new_func
中的功能时,有什么方法可以解决这个问题吗?
数据
df <- structure(list(Procs1 = structure(c(1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 5L), .Label = c("type1", "type2",
"type3", "type4", "type5", "type6"), class = "factor"),
Procs2 = structure(c(2L, 3L, 4L, 5L, 6L, 3L, 4L, 5L, 6L,
4L, 5L, 6L, 5L, 6L, 6L), .Label = c("type1", "type2",
"type3", "type4", "type5", "type6"), class = "factor"),
P = c(0.0057, 0.026, 6e-04, 0.046, 0.19, 0.14, 0.96, 0.06,
0.063, 0.033, 0.56, 0.85, 0.011, 0.054, 0.76)), .Names = c("Procs1",
"Procs2", "P"), row.names = c(NA, -15L), class = "data.frame")
df2 <- structure(list(Loc = c(1, 1, 0, 2, 0, -1, 2, 1, 0, 1, 3, 0,
0, 0, -1, 0, -1, 1, -1, 1, -1, -2, 1, 0, -2, 1, 0, -1, -2, 2,
0, -1, 1, 0, -1, 0, -1, -1, -1, 1, -1, 0, -4, -2, 1, 1, 0, 1,
0, -1, 2, 1, -2, -1, 3, 0, 0, 0, -1, -1, -1, 1, -1, 0, -1, -2,
0, 0, 0, 1, 0, -1, 1, 1, -1, -1, 1, 0, 0, -1, -1, -1, -1, 0,
-1, 0, -2, -2, 1, 1, 0, 1, 0, -1, 2, 1, -2, -1, 2, 0, 0, 0, -1,
0, -1, 1, -1, 1, -1, -2, 2, 0, 0, 1, 0, 0, 2, 2, -2, -1, 1, 0,
-1, 0, -1, 0, 0, 0, 0, 0, -2, -2), Proc = structure(c(2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L), .Label = c("ref", "type1", "type2", "type3", "type4",
"type5", "type6"), class = "factor"), lab = c("1.21", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "1.4", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "1.22", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "0.953", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "1.13", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "1.15", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "")), .Names = c("Loc", "Proc",
"lab"), row.names = c(NA, -132L), class = "data.frame")
df3 <- structure(list(Groc = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 22L, 22L, 22L, 22L, 22L, 22L, 22L), .Label = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20", "21", "22"), class = "factor"),
Proc = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("ref",
"type1", "type2", "type3", "type4", "type5", "type6"
), class = "factor"), types = structure(c(3L, 2L, 2L, 2L,
3L, 2L, 1L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 3L, 3L, 5L, 3L, 3L,
3L, 3L, 3L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 1L, 5L, 1L, 2L, 1L, 1L,
5L, 4L, 3L, 4L, 4L, 4L, 3L, 2L, 2L, 2L, 4L, 3L, 4L, 4L, 3L,
2L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 3L, 1L, 3L, 2L, 3L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 3L,
3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 2L, 3L,
2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 4L, 3L, 4L, 4L, 4L, 3L,
4L, 1L, 2L, 5L, 2L, 3L, 2L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 3L
), .Label = c("2", "3", "4", "5", "6"), class = "factor")), .Names = c("Groc",
"Proc", "types"), row.names = c(NA, -154L), class = "data.frame")
typenames <- c("type1", "type2", "type3", "type4", "type5", "type6")
答案 0 :(得分:3)
您传递给environment
的{{1}}似乎被解释为非grob。当我发表评论时,(同时dev.off也注释掉了)你的grid.arrange
功能正常工作。
new_func
随着环境被注释掉,它产生: