本文介绍了在函数中使用ggplot2时出现ArrangeGrob错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下功能,它使用ggplot和gridExtra包:
I have the following function which uses the ggplot and gridExtra packages:
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)
我收到以下错误:
有没有方式来解决这个问题,同时保留我在 new_func
中的功能。
Is there any way for me to solve this while retaining the functionality I have in 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")
推荐答案
环境
你传递给 grid.arrange
似乎被解释为非Grob。当我发表评论时,(还有dev.off注释掉),你的 new_func
功能起作用。
The environment
that you are passing to grid.arrange
seems to be getting interpreted as a non-grob. When I comment that out, (with also the dev.off commented out) your new_func
function works.
grid.arrange(arrangeGrob(plot1, plot2, ncol=2, nrow=1),
plot3, nrow=2
#, environment = environment()
)
环境注释掉,它产生:
With the environment commented out, it produces:
这篇关于在函数中使用ggplot2时出现ArrangeGrob错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!